import {
    calc, valueValue
} from "@/model/Calc";
import SwtionProcess from "./process";
import NodeHelper from "@/common/nodeHelper";
export default class SwtionCalc {
    private static calcList = new Array<ICalc>();
    public addCalc(name?: string, root?: Array<ICalc>): ICalc {
        root = root ? root : SwtionCalc.calcList;
        // let initCalc = new calc(this.createId());
        let initCalc = new calc(NodeHelper.createId());
        NodeHelper
        if (name) {
            initCalc.name = name;
        }
        root.push(initCalc);
        return initCalc;
    }
    public loadCalc(): Array<ICalc> {
        return SwtionCalc.calcList;
    }
    public initCalc(list: Array<ICalc>) {
        SwtionCalc.calcList = list;
    }
    // private createId(): string {
    //     return new Date().getTime().toString();
    // }
    public addCalcValue(id?: string, root?: ICalc): IValue {
        root = root ? root : this.findCalc(id ? id : '');
        let node = new valueValue(NodeHelper.createId());
        // let node = new valueValue(this.createId());
        root.result.push(node);
        return node;
    }
    public delCalc(id: string, root?: Array<ICalc>): boolean {
        root = root ? root : SwtionCalc.calcList;
        let swtionProc = new SwtionProcess();
        if (swtionProc.findEleInProcess(id)) {
            let node = this.findCalc(id, root);
            root.remove(node);
            return true;
        }
        return false;
    }
    public findCalc(id: string, root?: Array<ICalc>): ICalc {
        let node = new calc();
        root = root ? root : SwtionCalc.calcList;
        for (let i = 0; i < root.length; i++) {
            if (root[i].id == id) {
                node = root[i];
                break;
            }
        }
        return node as ICalc;
    }
}
