package cn.fateverse.query.handler.engine.impl;

import cn.fateverse.common.code.engine.JavaScriptEngine;
import cn.fateverse.query.entity.DataAdapter;
import cn.fateverse.query.enums.DataAdapterType;
import cn.fateverse.query.handler.engine.EngineExecuteHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * JavaScript 代码执行器
 *
 * @author Clay
 * @date 2023-10-29  19:35
 */
@Slf4j
@Component
public class JavaScriptEngineExecuteHandler implements EngineExecuteHandler {

    @Override
    public Object execute(DataAdapter dataAdapter, Object data, boolean development) {
        if (!DataAdapterType.JAVA_SCRIPT.equals(dataAdapter.getType())){
            return null;
        }
        return JavaScriptEngine.executeScript(dataAdapter.getExecuteCode(), "execute", data);
    }

    @Override
    public Boolean preconditioning(DataAdapter dataAdapter) {
        return Boolean.FALSE;
    }
}
