
import { actionAddins } from "../actions";
import '../actions/must-input';
import '../actions/auto-numbering';
import '../actions/field-shown';
import '../actions/error-show';
import '../actions/button-add';
import '../actions/condition-action';
import '../actions/data-processing';
import '../actions/data-mapping';
import '../actions/current-field-get';
import { ActionFlow,IActionFlow, IActionResult,IContext } from "./ActionTypes";

export class ActionProcess{
    eventId:string;
    flow:IActionFlow;
    event:any;
    context:IContext;
    constructor(eventId:string,flow:ActionFlow,event:any){
        this.eventId=eventId;
        this.flow=flow;
        this.event=event;
        this.context={
            record:this.event.record,
            variables:{}
        };
    }
    async exec(){
        const root = this.flow.getRoot();
        if(root===undefined || root.nextNodeIds.size===0){
            return;
        }
        let id=root.nextNodeIds.get('');
        if(id===undefined) return;
        let nextAction = this.flow.findNodeById(id); 
        let result:IActionResult={
            canNext:true
        };
        while(nextAction!==undefined && result.canNext){
           const action = actionAddins[nextAction.name];
           if(action!==undefined){
            result = await action.process(nextAction,this.event,this.context);
           }
           let nextInput = '';
           //outputPoints一つ以上の場合、次のInputPointは戻り値を設定する
           if(nextAction.outputPoints && nextAction.outputPoints.length>1){
             nextInput = result.result||'';
           }
           id=nextAction.nextNodeIds.get(nextInput);
           if(id===undefined) return;
           nextAction = this.flow.findNodeById(id); 
        }

    }
}