import { RuleExecutable } from "../../Common/Execution/RuleExecutable";
import { XExpressionDefinition } from "../../Expressions/XExpressionDefinition";
import { IfThenExpression } from "../../Expressions/ExpressionElements/Logic/IfThenExpression";
import { DataUtilities } from "../../Common/DataUtilities";
import { StatementRuleTestResult } from "./StatementRuleTestResult";
import { EventLog, EventLogType } from "../../Common/Event/EventLog";
export class StatementRuleDefinition extends RuleExecutable {
    constructor() {
        super();
        this.__id = DataUtilities.generateUniqueId();
        this.expression = new XExpressionDefinition();
        this.expression.expressionRoot.subExpression = new IfThenExpression();
        this.expression.executionResult = () => {
            return this.getRuleResult();
        };
        this.expression.inputData = () => {
            return this.getInputDefinition();
        };
    }
    set executionResult(res) {
        let executionResult;
        if (res instanceof Function) {
            executionResult = res();
        }
        else {
            executionResult = res;
        }
        this._executionResult = res;
        this.expression.executionResult = () => {
            return executionResult;
        };
    }
    get executionResult() {
        return this._executionResult;
    }
    run(data, testContext) {
        let subContext;
        if (testContext) {
            subContext = new StatementRuleTestResult(this.__id);
            testContext.addTestResultItem(subContext);
            subContext.addLogItem(new EventLog("StatementRule: start evaluating"));
        }
        try {
            const res = this.expression.run(data, subContext);
            if (subContext) {
                subContext.result = res !== undefined ? res.toString() : "undefined";
                subContext.addLogItem(new EventLog(`ExpressionDefinition: end evaluating with result [${res}]`));
            }
            return res;
        }
        catch (e) {
            if (subContext) {
                subContext.addLogItem(new EventLog(e.message, EventLogType.ERROR));
                subContext.isError = true;
                subContext.result = e.message;
            }
            throw e;
        }
    }
    getDesignValidationIssues() {
        return this.expression.getDesignValidationIssues();
    }
}
//# sourceMappingURL=StatementRuleDefinition.js.map