import { BooleanDataDefinition } from "../../../Common/Data/Definitions/BooleanDataDefinition";
import { DataUtilities } from "../../../Common/DataUtilities";
import { ValidationIssue } from "../../../Common/IDesignValidationProvider";
import { ExpressionValueType } from "../../../Expressions/ExpressionValueType";
import { XExpressionDefinition } from "../../../Expressions/XExpressionDefinition";
import { TreeRuleConstant } from "../Common/TreeRuleConstant";
export var TreeRuleNodeType;
(function (TreeRuleNodeType) {
    TreeRuleNodeType["EVALUATION"] = "evaluation";
    TreeRuleNodeType["RESULT"] = "result";
    TreeRuleNodeType["UNDEFINED"] = "undefined";
})(TreeRuleNodeType || (TreeRuleNodeType = {}));
export class TreeRuleNode {
    constructor() {
        this._id = DataUtilities.generateUniqueId();
        this.nodeType = TreeRuleNodeType.UNDEFINED;
        this.color = "#fff";
        // expression
        // if it is a evaluation node, expression returns true or false.  if it is a result node, expression returns the type expected
        this.expression = new XExpressionDefinition();
    }
    setupEvaluation(datadef) {
        this.nodeType = TreeRuleNodeType.EVALUATION;
        let booleandef = new BooleanDataDefinition();
        this.expression.executionResult = () => {
            return booleandef;
        };
        this.expression.inputData = () => {
            return datadef;
        };
        this.expression.valueType = ExpressionValueType.SUBEXPRESSION;
    }
    setupResult(idatadef, outdef) {
        this.nodeType = TreeRuleNodeType.RESULT;
        this.trueNode = undefined;
        this.falseNode = undefined;
        this.expression.valueType = ExpressionValueType.VALUE;
        this.expression.executionResult = () => {
            return outdef;
        };
        this.expression.inputData = () => {
            return idatadef;
        };
        this.color = TreeRuleConstant.DEFAULT_RESULT_NODE_COLOR;
    }
    delete() {
        var _a, _b, _c, _d;
        if (this.trueNode &&
            (((_a = this.trueNode) === null || _a === void 0 ? void 0 : _a.nodeType) === TreeRuleNodeType.RESULT ||
                ((_b = this.trueNode) === null || _b === void 0 ? void 0 : _b.nodeType) === TreeRuleNodeType.EVALUATION)) {
            this.trueNode.delete();
            delete this.trueNode;
        }
        if (this.falseNode &&
            (((_c = this.falseNode) === null || _c === void 0 ? void 0 : _c.nodeType) === TreeRuleNodeType.RESULT ||
                ((_d = this.falseNode) === null || _d === void 0 ? void 0 : _d.nodeType) === TreeRuleNodeType.EVALUATION)) {
            this.falseNode.delete();
            delete this.falseNode;
        }
        this.nodeType = TreeRuleNodeType.UNDEFINED;
        this.falseNode = undefined;
        this.trueNode = undefined;
    }
    get trueNode() {
        if (this.nodeType === TreeRuleNodeType.EVALUATION && !this._trueNode)
            this._trueNode = new TreeRuleNode();
        return this._trueNode;
    }
    set trueNode(treeNode) {
        this._trueNode = treeNode;
    }
    get falseNode() {
        if (this.nodeType === TreeRuleNodeType.EVALUATION && !this._falseNode)
            this._falseNode = new TreeRuleNode();
        return this._falseNode;
    }
    set falseNode(treeNode) {
        this._falseNode = treeNode;
    }
    set missingDataNode(value) {
        if (this.nodeType === TreeRuleNodeType.EVALUATION && !this._missingDataNode)
            this._missingDataNode = new TreeRuleNode();
        this._missingDataNode = value;
    }
    getDesignValidationIssues() {
        if (this.nodeType === TreeRuleNodeType.UNDEFINED) {
            return [new ValidationIssue("Node is required.")];
        }
        return this.expression.getDesignValidationIssues();
    }
    getStorageData() {
        let data = {};
        Reflect.set(data, "_id", this._id);
        Reflect.set(data, "nodeType", this.nodeType);
        if (this.trueNode) {
            Reflect.set(data, "trueNode", this.trueNode.getStorageData());
        }
        if (this.falseNode) {
            Reflect.set(data, "falseNode", this.falseNode.getStorageData());
        }
        if (this.missingDataNode) {
            Reflect.set(data, "missingDataNode", this.missingDataNode.getStorageData());
        }
        Reflect.set(data, "expression", this.expression.getStorageData());
        return data;
    }
    setStorageData(data) {
        this._id = Reflect.get(data, "_id");
        this.nodeType = Reflect.get(data, "nodeType");
        let trueNodeData = Reflect.get(data, "trueNode");
        if (trueNodeData) {
            let node = new TreeRuleNode();
            node.setStorageData(trueNodeData);
            this.trueNode = node;
        }
        let falseNodeData = Reflect.get(data, "falseNode");
        if (falseNodeData) {
            let node = new TreeRuleNode();
            node.setStorageData(falseNodeData);
            this.falseNode = node;
        }
        let missingDataNodeData = Reflect.get(data, "missingDataNode");
        if (missingDataNodeData) {
            let node = new TreeRuleNode();
            node.setStorageData(missingDataNodeData);
            this.missingDataNode = node;
        }
        let expressionData = Reflect.get(data, "expression");
        this.expression.setStorageData(expressionData);
    }
}
//# sourceMappingURL=TreeRuleNode.js.map