package com.cmvr.test.flow.runtime.dispatcher;

import com.cmvr.test.enums.ActionEnum;
import com.cmvr.test.flow.runtime.message.TaskNodeExecuteMessage;
import com.cmvr.test.flow.runtime.message.TaskNodeExecuteResult;
import com.cmvr.test.flow.runtime.operator.NodeOperateHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component("FUNCTION")
@RequiredArgsConstructor
public class FlowFunctionNodeHandler implements FlowNodeTypeHandler {

    private final Map<String, NodeOperateHandler> operateHandlerMap;

    @Override
    public TaskNodeExecuteResult handle(TaskNodeExecuteMessage message) {
        ActionEnum action = message.getAction();

        NodeOperateHandler handler = operateHandlerMap.get(action.getOperate());

        TaskNodeExecuteResult execute = handler.execute(message);
        return TaskNodeExecuteResult.success(execute.getOutputParams());
    }
}
