import { __decorate, __metadata } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { ToggleButton } from "@material-ui/lab";
import { BaseLogicExpression } from "./BaseLogic";
import { ExpressionValue } from "../../ExpressionValue";
import { ExpressionValueRenderer } from "../../ExpressionValueRenderer";
import styled from "styled-components";
import { IconButton } from "@material-ui/core";
import { AutoBind } from "../../../Common/AutoBind";
import { InlineTextEdit } from "../../../CommonUI/InlineTextEdit";
import { PlusIcon } from "../../../CommonUI/Icon/PlusIcon";
import { LeftAlignIcon } from "../../../CommonUI/Icon/LeftAlignIcon";
import { ActiveLeftAlignIcon } from "../../../CommonUI/Icon/ActiveLeftAlignIcon";
import { RightAlignIcon } from "../../../CommonUI/Icon/RightAlignIcon";
import { ActiveRightAlignIcon } from "../../../CommonUI/Icon/ActiveRightAlignIcon";
import { LogicalIcon } from "../../../CommonUI/Icon/LogicalIcon";
import { ActiveLogicalIcon } from "../../../CommonUI/Icon/ActiveLogicalIcon";
import { ExpressionLineDiv, RuleChangeToggleButtonGroup } from "../../ExpressionStyles";
import { AndGroup } from "./Grouping";
import { DataUtilities } from "../../../Common/DataUtilities";
import { DeleteIcon } from "../../../CommonUI/Icon/DeleteIcon";
import { StateManager } from "../../../CommonUI/StateManagement/StateManager";
import { ObserveState } from "../../../CommonUI/StateManagement/ObserveState";
import { ExpressionContext, ExpressionRenderType } from "../../ExpressionContext";
import { NumberDataDefinition } from "../../../Common/Data/Definitions/NumberDataDefinition";
import { BooleanDataDefinition } from "../../../Common/Data/Definitions/BooleanDataDefinition";
import { XDataType } from "../../../Common/Data/XDataType";
import { ExpressionValueType } from "../../ExpressionValueType";
import { ExpressionTestResult, ExpressionValueTestResult } from "../../Testing/ExpressionTestResult";
import { TestContext } from "../../../Tester/TestContext";
import { ExpressionTestResultColorContext } from "../../Test/ExpressionTestResultColorContext";
import { TestingConstants } from "../../../CommonUI/Testing/TestingConstants";
import { ExpressionConstants } from "../../ExpressionContants";
const AllIfDiv = styled.div `
    font-size: 18px;
    width: 100%;
`;
const StyledIfSection = styled.div `
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    width: 100%;

    ${(props) => props.renderType === ExpressionRenderType.FULL
    ? `
            position: relative;
            margin: 2px 0;
            padding-left: 8px;

            &::before {
                content: '';
                width: 4px;
                position: absolute;
                top: 0;
                left: 0;
                bottom: 0;
                border: 1px solid ${props.bracketcolor || "#93A9BF"};
                border-right: none;
                border-top-left-radius: 3px;
                border-bottom-left-radius: 3px;
            }
        
            &::after {
                content: '';
                width: 4px;
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                border: 1px solid ${props.bracketcolor || "#93A9BF"};
                border-left: none;
                border-top-right-radius: 3px;
                border-bottom-right-radius: 3px;
            }
        `
    : ``}
`;
export const IfSection = (props) => {
    const context = React.useContext(ExpressionContext);
    const colorcontext = React.useContext(ExpressionTestResultColorContext);
    return _jsx(StyledIfSection, Object.assign({}, props, { renderType: context.renderType, bracketcolor: colorcontext }), void 0);
};
const StyledIfIndentedSection = styled.div `
    display: flex;
    align-items: center;
    width: 100%;
    flex-wrap: wrap;
    margin-left: 15px;

    ${(props) => props.renderType === ExpressionRenderType.FULL
    ? `
            padding-left: 13px;
            margin-right: 15px;
            margin-top: 5px;
            margin-bottom: 10px;
            position: relative;
        
            &::before {
                content: '';
                width: 4px;
                position: absolute;
                top: 0;
                left: 0;
                bottom: 0;
                border: 1px solid #93A9BF;
                border-right: none;
                border-top-left-radius: 3px;
                border-bottom-left-radius: 3px;
            }
        
            &::after {
                content: '';
                width: 4px;
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                border: 1px solid #93A9BF;
                border-left: none;
                border-top-right-radius: 3px;
                border-bottom-right-radius: 3px;
            }
        `
    : ``}
`;
export const IfIndentedSection = (props) => {
    const context = React.useContext(ExpressionContext);
    return _jsx(StyledIfIndentedSection, Object.assign({}, props, { renderType: context.renderType }), void 0);
};
const StyledActionButtons = styled.div `
    ${(props) => props.renderType === ExpressionRenderType.FULL
    ? `
                    display: flex;
                    margin-left: auto;
                    justify-content: flex-end;
                    padding-right: 15px;
                    align-items: center;
                `
    : `display: none;`}
`;
export const ActionButtons = (props) => {
    const context = React.useContext(ExpressionContext);
    return _jsx(StyledActionButtons, Object.assign({}, props, { renderType: context.renderType }), void 0);
};
const StyledFloatRight = styled.div `
    ${(props) => props.renderType === ExpressionRenderType.FULL
    ? `
                display: flex;
                margin-left: auto;
                justify-content: flex-end;
                padding-right: 15px;
                padding-top: 5px;
                padding-bottom: 5px;
                align-items: center;
            `
    : `
                display: none;
            `}
`;
export const FloatRight = (props) => {
    const context = React.useContext(ExpressionContext);
    return _jsx(StyledFloatRight, Object.assign({}, props, { renderType: context.renderType }), void 0);
};
export var IfThenStyle;
(function (IfThenStyle) {
    IfThenStyle["DATA_RETURN"] = "data_return";
    IfThenStyle["BRANCH_DATA_RETURN"] = "branch_data_return";
    IfThenStyle["LOGICAL_RETURN"] = "logical_return";
})(IfThenStyle || (IfThenStyle = {}));
export class IfThenPair {
    constructor() {
        this.__type = "If Then Pair";
        this.canSelect = false;
        this.ifExpression = new AndGroup();
        this.thenExpression = new ExpressionValue();
        this._id = DataUtilities.generateUniqueId();
    }
    setEditContext(editContext, owner) {
        this.editContext = editContext;
        editContext.register(this, owner);
        if (editContext) {
            this.ifExpression.setEditContext(editContext, this);
            this.thenExpression.setEditContext(editContext, this);
        }
    }
    getStorageData() {
        let data = {};
        Reflect.set(data, "_id", this._id);
        Reflect.set(data, "ifExpression", this.ifExpression.getStorageData());
        Reflect.set(data, "thenExpression", this.thenExpression.getStorageData());
        return data;
    }
    setStorageData(data) {
        this._id = Reflect.get(data, "_id");
        this.ifExpression.setStorageData(Reflect.get(data, "ifExpression"));
        this.thenExpression.setStorageData(Reflect.get(data, "thenExpression"));
    }
    checkPassedInTest(testcontext) {
        if (!testcontext)
            return null;
        let testitem = testcontext.findItem(this._id);
        if (!testitem)
            return null;
        if (testitem.result === "true")
            return true;
        if (testitem.result === "false")
            return false;
        return null;
    }
    getTestValueColor(testcontext) {
        let result = this.checkPassedInTest(testcontext);
        if (result === null)
            return undefined;
        let color = result ? TestingConstants.TEXT_SUCCESS : TestingConstants.TEXT_FAIL;
        return color;
    }
    renderTestResult(testcontext) {
        let result = this.checkPassedInTest(testcontext);
        let color = result ? TestingConstants.TEXT_SUCCESS : TestingConstants.TEXT_FAIL;
        if (result === null)
            return null;
        return _jsx(TestValueRender, Object.assign({ color: color }, { children: result.toString() }), void 0);
    }
}
// todo.  this is one where we can change output
export class IfThenExpression extends BaseLogicExpression {
    constructor(outputType = new NumberDataDefinition()) {
        super();
        this.__type = ExpressionConstants.IfThenExpressionType;
        // serialized by base class
        this.trueOutput = new ExpressionValue();
        // serialized by base class
        this.defaultOutput = new ExpressionValue();
        this._ifBranches = [];
        this.showElseAsDefault = false;
        this.showSimpleReturn = true;
        this._ifThenStyle = IfThenStyle.DATA_RETURN;
        this.thenText = "result is";
        this.defaultText = "default result is";
        if (outputType)
            this.expressionValueType = outputType;
        if (this._ifBranches.length === 0) {
            this.addIfBranch();
        }
        // set name and register expression values
        this.trueOutput = ExpressionValue.createExpressionValue(this, "trueOutput", this.expressionValueType);
        this.defaultOutput = ExpressionValue.createExpressionValue(this, "defaultOutput", this.expressionValueType);
    }
    set expressionValueType(outputType) {
        if (this._ifThenStyle === IfThenStyle.LOGICAL_RETURN) {
            outputType = new BooleanDataDefinition();
        }
        this._expressionValueType = outputType;
        for (let ifBranch of this._ifBranches) {
            ifBranch.thenExpression.expectedType = outputType;
        }
        this.trueOutput.expectedType = outputType;
        this.defaultOutput.expectedType = outputType;
    }
    get expressionValueType() {
        return this._expressionValueType;
    }
    getExpressionValues() {
        let values = [];
        this._ifBranches.forEach((value) => {
            values.push(...value.ifExpression.getExpressionValues());
            if (this._ifThenStyle === IfThenStyle.BRANCH_DATA_RETURN) {
                values.push(value.thenExpression);
            }
        });
        if (this._ifThenStyle !== IfThenStyle.LOGICAL_RETURN) {
            if (this._ifThenStyle === IfThenStyle.DATA_RETURN) {
                values.push(this.trueOutput);
            }
            values.push(this.defaultOutput);
        }
        return values;
    }
    getAllVariables() {
        let output = [];
        for (let f of this.getExpressionValues()) {
            output.push(...f.getAllVariables());
        }
        return output;
    }
    addIfBranch(pair) {
        if (!pair)
            pair = new IfThenPair();
        pair.thenExpression.expectedType = this.expressionValueType;
        this._ifBranches.push(pair);
        StateManager.changed(this);
        if (this.editContext) {
            pair.setEditContext(this.editContext, this);
            this.onUpdate();
        }
        return pair;
    }
    deleteIfBranch(index) {
        this._ifBranches.splice(index, 1);
        this.onUpdate();
        StateManager.changed(this);
    }
    get ifBranches() {
        return this._ifBranches;
    }
    setEditContext(editContext, owner) {
        super.setEditContext(editContext, owner);
        for (let p of this._ifBranches) {
            p.setEditContext(editContext, this);
        }
    }
    render() {
        return (_jsx(ObserveState, { listenTo: this, control: () => (_jsx(TestContext.Consumer, { children: (testcontext) => (_jsxs(AllIfDiv, { children: [this._ifBranches.map((value, index) => {
                            if (this._ifThenStyle === IfThenStyle.BRANCH_DATA_RETURN) {
                                if (index === 0) {
                                    return (_jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: value.getTestValueColor(testcontext) }, { children: _jsxs(IfSection, { children: [_jsxs(ExpressionLineDiv, { children: ["if ", value.ifExpression.render(), _jsx(FloatRight, { children: this.renderRuleChange() }, void 0), value.renderTestResult(testcontext)] }, void 0),
                                                _jsxs(IfIndentedSection, { children: [_jsx(ExpressionLineDiv, { children: this.renderThen() }, void 0),
                                                        _jsxs(ExpressionLineDiv, { children: ["\u00A0\u00A0", _jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: undefined }, { children: _jsx(ExpressionValueRenderer, { el: value.thenExpression }, void 0) }), void 0), this.renderBranchActions(index)] }, void 0)] }, void 0)] }, value._id) }), value._id));
                                }
                                else {
                                    return (_jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: value.getTestValueColor(testcontext) }, { children: _jsxs(IfSection, { children: [_jsxs(ExpressionLineDiv, { children: ["else if ", value.ifExpression.render(), this.renderBranchActions(index), value.renderTestResult(testcontext)] }, void 0),
                                                _jsxs(IfIndentedSection, { children: [_jsx(ExpressionLineDiv, { children: this.renderThen() }, void 0),
                                                        _jsxs(ExpressionLineDiv, { children: ["\u00A0\u00A0", _jsx(ExpressionValueRenderer, { el: value.thenExpression }, void 0)] }, void 0)] }, void 0)] }, value._id) }), value._id));
                                }
                            }
                            else {
                                if (index === 0) {
                                    return (_jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: value.getTestValueColor(testcontext) }, { children: _jsx(IfSection, { children: _jsxs(ExpressionLineDiv, { children: ["if ", value.ifExpression.render(), this.renderBranchActions(index, true), value.renderTestResult(testcontext)] }, void 0) }, value._id) }), value._id));
                                }
                                else {
                                    return (_jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: value.getTestValueColor(testcontext) }, { children: _jsx(IfSection, { children: _jsxs(ExpressionLineDiv, { children: ["else if ", value.ifExpression.render(), this.renderBranchActions(index), value.renderTestResult(testcontext)] }, void 0) }, value._id) }), value._id));
                                }
                            }
                        }), this.renderEnd()] }, void 0)) }, void 0)) }, void 0));
    }
    renderThen() {
        return (_jsx(ExpressionContext.Consumer, { children: (context) => (_jsx(InlineTextEdit, { text: this.thenText, onEdit: (newVal) => {
                    var _a;
                    this.thenText = newVal;
                    (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.refresh();
                }, readOnly: context.renderType === ExpressionRenderType.COMPACT ||
                    context.renderType === ExpressionRenderType.TEXT }, void 0)) }, void 0));
    }
    renderBranchActions(index, renderRuleChange = false) {
        return (_jsx(TestContext.Consumer, { children: (testcontext) => {
                if (testcontext)
                    return null;
                let isLast = index + 1 === this._ifBranches.length;
                if (isLast) {
                    if (index === 0) {
                        if (renderRuleChange) {
                            return (_jsxs(FloatRight, { children: [_jsx(IconButton, Object.assign({ size: "small", onClick: this.addIfBranchFromAction }, { children: _jsx(PlusIcon, {}, void 0) }), void 0), "\u00A0", this.renderRuleChange()] }, void 0));
                        }
                        else {
                            return (_jsx(ActionButtons, { children: _jsx(IconButton, Object.assign({ size: "small", onClick: this.addIfBranchFromAction }, { children: _jsx(PlusIcon, {}, void 0) }), void 0) }, void 0));
                        }
                    }
                    else {
                        return (_jsxs(ActionButtons, { children: [_jsx(IconButton, Object.assign({ size: "small", "aria-label": "delete", onClick: (event) => {
                                        event.stopPropagation();
                                        this.deleteIfBranchFromAction(index);
                                    } }, { children: _jsx(DeleteIcon, {}, void 0) }), void 0),
                                _jsx(IconButton, Object.assign({ size: "small", onClick: this.addIfBranchFromAction }, { children: _jsx(PlusIcon, {}, void 0) }), void 0)] }, void 0));
                    }
                }
                else if (index !== 0) {
                    return (_jsx(ActionButtons, { children: _jsx(IconButton, Object.assign({ size: "small", "aria-label": "delete", onClick: (event) => {
                                event.stopPropagation();
                                this.deleteIfBranchFromAction(index);
                            } }, { children: _jsx(DeleteIcon, {}, void 0) }), void 0) }, void 0));
                }
                else if (renderRuleChange) {
                    return _jsx(FloatRight, { children: this.renderRuleChange() }, void 0);
                }
            } }, void 0));
    }
    getTestValueColor(el, testcontext) {
        if (!testcontext)
            return undefined;
        if (!testcontext)
            return undefined;
        let testitem = testcontext.findItem(el._id);
        if (testitem) {
            return TestingConstants.TEXT_SUCCESS;
        }
        return undefined;
    }
    getLogicDataTestColor(testcontext, val) {
        if (!testcontext)
            return undefined;
        if (!testcontext)
            return undefined;
        let testitem = testcontext.findItem(this._id);
        if ((testitem === null || testitem === void 0 ? void 0 : testitem.result) === val)
            return "#35B687";
        if ((testitem === null || testitem === void 0 ? void 0 : testitem.result) !== val)
            return "#EE3E54";
        return undefined;
    }
    renderEnd() {
        if (this._ifThenStyle === IfThenStyle.LOGICAL_RETURN && this.showSimpleReturn) {
            return (_jsxs(IfSection, { children: [_jsx(ExpressionLineDiv, { children: this.renderThen() }, void 0),
                    _jsx(TestContext.Consumer, { children: (testcontext) => (_jsxs(IfIndentedSection, { children: [_jsx(ExpressionLineDiv, Object.assign({ color: this.getLogicDataTestColor(testcontext, "true") }, { children: _jsx(ResultSection, Object.assign({ bracketcolor: this.getLogicDataTestColor(testcontext, "true") }, { children: "\u00A0\u00A0return true\u00A0\u00A0" }), void 0) }), void 0),
                                _jsx(ExpressionLineDiv, { children: "else" }, void 0),
                                _jsx(ExpressionLineDiv, Object.assign({ color: this.getLogicDataTestColor(testcontext, "false") }, { children: _jsx(ResultSection, Object.assign({ bracketcolor: this.getLogicDataTestColor(testcontext, "false") }, { children: "\u00A0\u00A0return false\u00A0\u00A0" }), void 0) }), void 0)] }, void 0)) }, void 0)] }, void 0));
        }
        if (this._ifThenStyle === IfThenStyle.BRANCH_DATA_RETURN) {
            if (this.showElseAsDefault) {
                return (_jsx(TestContext.Consumer, { children: (testcontext) => (_jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: this.getTestValueColor(this.defaultOutput, testcontext) }, { children: _jsxs(IfSection, { children: [_jsx(ExpressionLineDiv, {}, void 0),
                                _jsxs(ExpressionLineDiv, { children: [this.defaultText, " = ", _jsx(ExpressionValueRenderer, { el: this.defaultOutput }, void 0)] }, void 0)] }, void 0) }), void 0)) }, void 0));
            }
            else {
                return (_jsx(TestContext.Consumer, { children: (testcontext) => (_jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: this.getTestValueColor(this.defaultOutput, testcontext) }, { children: _jsxs(IfSection, { children: [_jsx(ExpressionLineDiv, { children: "else" }, void 0),
                                _jsxs(IfIndentedSection, { children: [_jsx(ExpressionLineDiv, { children: this.renderThen() }, void 0),
                                        _jsx(ExpressionLineDiv, { children: _jsx(ExpressionValueRenderer, { el: this.defaultOutput }, void 0) }, void 0)] }, void 0)] }, void 0) }), void 0)) }, void 0));
            }
        }
        if (this._ifThenStyle === IfThenStyle.DATA_RETURN) {
            return (_jsx(TestContext.Consumer, { children: (testcontext) => (_jsx(IfSection, { children: _jsxs(IfIndentedSection, { children: [_jsx(ExpressionLineDiv, { children: this.renderThen() }, void 0),
                            _jsxs(ExpressionLineDiv, { children: ["\u00A0\u00A0", _jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: this.getTestValueColor(this.trueOutput, testcontext) }, { children: _jsxs(ResultSection, Object.assign({ bracketcolor: this.getTestValueColor(this.trueOutput, testcontext) }, { children: ["\u00A0\u00A0", _jsx(ExpressionValueRenderer, { el: this.trueOutput }, void 0)] }), void 0) }), void 0)] }, void 0),
                            _jsx(ExpressionLineDiv, { children: "else" }, void 0),
                            _jsx(ExpressionTestResultColorContext.Provider, Object.assign({ value: this.getTestValueColor(this.defaultOutput, testcontext) }, { children: _jsxs(ExpressionLineDiv, { children: ["\u00A0\u00A0", _jsx(ResultSection, Object.assign({ bracketcolor: this.getTestValueColor(this.defaultOutput, testcontext) }, { children: _jsx(ExpressionValueRenderer, { el: this.defaultOutput }, void 0) }), void 0)] }, void 0) }), void 0)] }, void 0) }, void 0)) }, void 0));
        }
        return _jsx(ExpressionLineDiv, {}, void 0);
    }
    onUpdate() {
        var _a;
        (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.refresh();
    }
    addIfBranchFromAction(event) {
        event.stopPropagation();
        this.addIfBranch();
    }
    deleteIfBranchFromAction(index) {
        if (this.ifBranches.length === 1)
            return null;
        this.deleteIfBranch(index);
    }
    renderRuleChange() {
        return (_jsx(TestContext.Consumer, { children: (testcontext) => {
                if (testcontext)
                    return null;
                if (this.expressionValueType.type !== XDataType.BOOLEAN) {
                    return (_jsxs(RuleChangeToggleButtonGroup, Object.assign({ value: this._ifThenStyle, exclusive: true, onChange: this.changeStyle, "aria-label": "rule style", size: "small" }, { children: [_jsx(ToggleButton, Object.assign({ value: IfThenStyle.DATA_RETURN, "aria-label": IfThenStyle.DATA_RETURN }, { children: this._ifThenStyle !== IfThenStyle.DATA_RETURN ? (_jsx(LeftAlignIcon, { alt: IfThenStyle.DATA_RETURN }, void 0)) : (_jsx(ActiveLeftAlignIcon, { alt: IfThenStyle.DATA_RETURN }, void 0)) }), void 0),
                            _jsx(ToggleButton, Object.assign({ value: IfThenStyle.BRANCH_DATA_RETURN, "aria-label": "Branch Data Return" }, { children: this._ifThenStyle !== IfThenStyle.BRANCH_DATA_RETURN ? (_jsx(RightAlignIcon, { alt: IfThenStyle.BRANCH_DATA_RETURN }, void 0)) : (_jsx(ActiveRightAlignIcon, { alt: IfThenStyle.BRANCH_DATA_RETURN }, void 0)) }), void 0)] }), void 0));
                }
                else {
                    return (_jsxs(RuleChangeToggleButtonGroup, Object.assign({ value: this._ifThenStyle, exclusive: true, onChange: this.changeStyle, "aria-label": "rule style", size: "small" }, { children: [_jsx(ToggleButton, Object.assign({ value: IfThenStyle.DATA_RETURN, "aria-label": IfThenStyle.DATA_RETURN }, { children: this._ifThenStyle !== IfThenStyle.DATA_RETURN ? (_jsx(LeftAlignIcon, { alt: IfThenStyle.DATA_RETURN }, void 0)) : (_jsx(ActiveLeftAlignIcon, { alt: IfThenStyle.DATA_RETURN }, void 0)) }), void 0),
                            _jsx(ToggleButton, Object.assign({ value: IfThenStyle.BRANCH_DATA_RETURN, "aria-label": "Branch Data Return" }, { children: this._ifThenStyle !== IfThenStyle.BRANCH_DATA_RETURN ? (_jsx(RightAlignIcon, { alt: "Branch Data Return" }, void 0)) : (_jsx(ActiveRightAlignIcon, { alt: "Branch Data Return" }, void 0)) }), void 0),
                            _jsx(ToggleButton, Object.assign({ value: IfThenStyle.LOGICAL_RETURN, "aria-label": "Logical Return" }, { children: this._ifThenStyle !== IfThenStyle.LOGICAL_RETURN ? (_jsx(LogicalIcon, { alt: "Logical Return" }, void 0)) : (_jsx(ActiveLogicalIcon, { alt: "Logical Return" }, void 0)) }), void 0)] }), void 0));
                }
            } }, void 0));
    }
    changeStyle(event, value) {
        this.ifThenStyle = value;
        StateManager.changed(this);
    }
    get ifThenStyle() {
        return this._ifThenStyle;
    }
    set ifThenStyle(value) {
        if (value === IfThenStyle.LOGICAL_RETURN) {
            this.expressionValueType = new BooleanDataDefinition();
            this.trueOutput.expectedType = new BooleanDataDefinition();
            this.trueOutput.valueType = ExpressionValueType.VALUE;
            this.trueOutput.value = true;
            this.defaultOutput.valueType = ExpressionValueType.VALUE;
            this.defaultOutput.value = false;
            this.defaultOutput.expectedType = new BooleanDataDefinition();
        }
        this._ifThenStyle = value;
        this.onUpdate();
    }
    evaluate(data, testContext) {
        for (let ifThenPair of this.ifBranches) {
            let value = ifThenPair.ifExpression.evaluate(data, testContext);
            if (testContext) {
                testContext.addTestResultItem(new ExpressionValueTestResult(ifThenPair._id, value.toString()));
            }
            if (value) {
                if (this.ifThenStyle === IfThenStyle.BRANCH_DATA_RETURN) {
                    return ifThenPair.thenExpression.evaluate(data, testContext);
                }
                else if (this.ifThenStyle === IfThenStyle.DATA_RETURN) {
                    value = this.trueOutput.evaluate(data, testContext);
                    if (testContext) {
                        testContext.addTestResultItem(new ExpressionValueTestResult(this.trueOutput._id, (value === null || value === void 0 ? void 0 : value.toString()) || "undefined"));
                    }
                    return value;
                }
                else {
                    if (testContext) {
                        testContext.addTestResultItem(new ExpressionValueTestResult(this._id, "true"));
                    }
                    return true;
                }
            }
        }
        if (this.ifThenStyle === IfThenStyle.BRANCH_DATA_RETURN || this.ifThenStyle === IfThenStyle.DATA_RETURN) {
            let value = this.defaultOutput.evaluate(data, testContext);
            if (testContext) {
                testContext.addTestResultItem(new ExpressionTestResult(this.defaultOutput._id, value.toString()));
            }
            return value;
        }
        else {
            if (testContext) {
                testContext.addTestResultItem(new ExpressionValueTestResult(this._id, "false"));
            }
            return false;
        }
    }
    getStorageData() {
        let data = super.getStorageData();
        let branches = [];
        for (let e of this._ifBranches) {
            branches.push(e.getStorageData());
        }
        Reflect.set(data, "branches", branches);
        Reflect.set(data, "showElseAsDefault", this.showElseAsDefault);
        Reflect.set(data, "showSimpleReturn", this.showSimpleReturn);
        Reflect.set(data, "ifThenStyle", this._ifThenStyle);
        Reflect.set(data, "thenText", this.thenText);
        Reflect.set(data, "defaultText", this.defaultText);
        return data;
    }
    setStorageData(data) {
        super.setStorageData(data);
        let branchData = Reflect.get(data, "branches");
        for (let e of branchData) {
            let exp = new IfThenPair();
            exp.setStorageData(e);
            this._ifBranches.push(exp);
        }
        this.showElseAsDefault = Reflect.get(data, "showElseAsDefault");
        this.showSimpleReturn = Reflect.get(data, "showSimpleReturn");
        this._ifThenStyle = Reflect.get(data, "ifThenStyle");
        this.thenText = Reflect.get(data, "thenText");
        this.defaultText = Reflect.get(data, "defaultText");
    }
}
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "renderThen", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Boolean]),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "renderBranchActions", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], IfThenExpression.prototype, "renderEnd", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "onUpdate", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "addIfBranchFromAction", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number]),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "deleteIfBranchFromAction", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "renderRuleChange", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", void 0)
], IfThenExpression.prototype, "changeStyle", null);
const TestValueRender = styled.div `
    font-size: 12px;
    position: absolute;

    right: 10px;
    bottom: -16px;

    ${(props) => (props.color ? `color: ${props.color};` : ``)}
`;
const ResultSection = styled.div `
    display: inline-flex;
    position: relative;
    padding: 5px 10px;
    text-align: center;

    &::before {
        content: "";
        width: 4px;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        border: 1px solid ${(props) => props.bracketcolor || "#93A9BF"};
        border-right: none;
        border-top-left-radius: 3px;
        border-bottom-left-radius: 3px;
    }

    &::after {
        content: "";
        width: 4px;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        border: 1px solid ${(props) => props.bracketcolor || "#93A9BF"};
        border-left: none;
        border-top-right-radius: 3px;
        border-bottom-right-radius: 3px;
    }
`;
//# sourceMappingURL=IfThenExpression.js.map