import { jsx as _jsx } from "react/jsx-runtime";
import { DataUtilities } from "../../Common/DataUtilities";
import { StateManager } from "../../CommonUI/StateManagement/StateManager";
import { PropertyEditorColor } from "../../CommonUI/PropertyEditing/PropertyEditorColor";
import { ObserveState } from "../../CommonUI/StateManagement/ObserveState";
import { FlowConstants } from "../CommonUI/FlowConstants";
import { FlowStepOutputInstructionType } from "./FlowStepOutputInstructions";
import { SequenceEndStep } from "../Steps/General/SequenceEndStep";
import { HTMLUtilities } from "../../CommonUI/HTMLUtilities";
export var FlowStepSequenceStrategy;
(function (FlowStepSequenceStrategy) {
    FlowStepSequenceStrategy["ENDFLOW"] = "endflow";
    FlowStepSequenceStrategy["ENDFLOWINERROR"] = "endflowinerror";
    FlowStepSequenceStrategy["LINK"] = "link";
})(FlowStepSequenceStrategy || (FlowStepSequenceStrategy = {}));
export class FlowStepSequence {
    constructor(color) {
        this.sequenceType = "standard";
        this._id = DataUtilities.generateUniqueId();
        this._x = -1;
        this._y = -1;
        this._desiredY = -1;
        this._desiredX = -1;
        this._height = FlowConstants.DEFAULT_STACK_HEIGHT;
        this._stackColor = FlowConstants.DEFAULT_SEQUENCE_COLOR;
        this._canDelete = true;
        this._isCollapsed = false;
        this._canCopy = true;
        this._steps = [];
        this._strategy = FlowStepSequenceStrategy.ENDFLOW;
        this.errorMessage = "flow error";
        this._hiddenStep = new SequenceEndStep();
        this._hiddenStep.sequence = this;
        this._hiddenStep._id = `${this._id}_end`;
        this._hiddenStep.getOutcomeInstructions()[0].strategy = FlowStepOutputInstructionType.END_FLOW;
        if (color) {
            this._stackColor = color;
        }
    }
    getElementId() {
        return this._id;
    }
    get geometry() {
        return HTMLUtilities.getGeometry(this._id, FlowConstants.DESIGN_SURFACE_ID);
    }
    get designerGeometry() {
        return HTMLUtilities.getGeometry(FlowConstants.DESIGN_SURFACE_ID);
    }
    get x() {
        var _a, _b;
        // we need to get the updated position of end / error using geometry as we are not setting the position data for them in positioned logic.
        if (this.isEnd || this.isError) {
            return (((_a = this.geometry) === null || _a === void 0 ? void 0 : _a.left) || 0) - (((_b = this.designerGeometry) === null || _b === void 0 ? void 0 : _b.left) || 0);
        }
        return this._x;
    }
    set x(value) {
        if (this._id === FlowConstants.PRIMARY_SEQUENCE && value !== FlowConstants.PRIMARY_SEQUENCE_POSITION.x)
            throw new Error("you cannot move the primary sequence (x)");
        this._x = value;
    }
    get y() {
        var _a, _b, _c;
        // we need to get the updated position of end / error using geometry as we are not setting the position data for them in positioned logic.
        if (this.isEnd || this.isError) {
            return ((((_a = this.geometry) === null || _a === void 0 ? void 0 : _a.top) || 0) +
                (((_b = this.editContext) === null || _b === void 0 ? void 0 : _b.designerScrollTop) || 0) -
                (((_c = this.designerGeometry) === null || _c === void 0 ? void 0 : _c.top) || 0));
        }
        return this._y;
    }
    set y(value) {
        if (this._id === FlowConstants.PRIMARY_SEQUENCE && value !== FlowConstants.PRIMARY_SEQUENCE_POSITION.y)
            throw new Error("you cannot move the primary sequence (y)");
        this._y = value;
    }
    get desiredY() {
        return this._desiredY;
    }
    set desiredY(value) {
        this._desiredY = value;
        this._y = value;
    }
    get desiredX() {
        return this._desiredX;
    }
    set desiredX(value) {
        this._desiredX = value;
        this._x = value;
    }
    get width() {
        return this.editContext.displayBehavior.getSequenceMainSize(this).width || 0;
    }
    get height() {
        return this._height;
    }
    set height(value) {
        //let oldVal = this._height;
        this._height = value;
    }
    set strategy(value) {
        var _a, _b;
        this._strategy = value;
        let hiddenStep = this.hiddenStep;
        let ins = hiddenStep.getOutcomeInstructions()[0];
        switch (value) {
            case FlowStepSequenceStrategy.ENDFLOW:
                ins.strategy = FlowStepOutputInstructionType.END_FLOW;
                break;
            case FlowStepSequenceStrategy.LINK:
                ins.strategy = FlowStepOutputInstructionType.BRANCH;
                break;
            case FlowStepSequenceStrategy.ENDFLOWINERROR:
                ins.strategy = FlowStepOutputInstructionType.THROW_EXCEPTION;
                break;
        }
        (_b = (_a = this.flow) === null || _a === void 0 ? void 0 : _a.context) === null || _b === void 0 ? void 0 : _b.onSequenceConnected(this);
    }
    get strategy() {
        return this._strategy;
    }
    reset() {
        this._x = this.desiredX;
        this._y = this.desiredY;
    }
    get steps() {
        return this._steps;
    }
    get instructions() {
        var _a;
        return ((_a = this.editContext) === null || _a === void 0 ? void 0 : _a.displayBehavior.getSequenceInstructions(this)) || [];
    }
    get canDelete() {
        return this._canDelete;
    }
    set canDelete(d) {
        this._canDelete = d;
    }
    get canCopy() {
        return this._canCopy;
    }
    set canCopy(c) {
        this._canCopy = c;
    }
    get isCollapsed() {
        return this._isCollapsed;
    }
    set isCollapsed(collapsed) {
        var _a, _b, _c;
        this._isCollapsed = collapsed;
        if (collapsed) {
            (_a = this.editContext) === null || _a === void 0 ? void 0 : _a.onSequenceCollapsed(this);
        }
        else if (!((_b = this.editContext) === null || _b === void 0 ? void 0 : _b.draggingControl)) {
            (_c = this.editContext) === null || _c === void 0 ? void 0 : _c.onSequenceExpanded(this);
        }
        StateManager.propertyChanged(this, "isCollapsed");
    }
    get color() {
        return this._stackColor;
    }
    set color(color) {
        this._stackColor = color;
    }
    removeStep(step) {
        let index = this._steps.indexOf(step);
        if (index >= 0) {
            step.sequence = undefined;
            this._steps.splice(index, 1);
            StateManager.propertyChanged(this, "steps");
        }
    }
    find(stepId) {
        for (let s of [...this._steps, this.hiddenStep]) {
            if (s._id === stepId)
                return s;
        }
        return null;
    }
    get hiddenStep() {
        return this._hiddenStep;
    }
    addStep(step, index) {
        var _a;
        // add step to last
        if (index === undefined || index === null || ((_a = this.editContext) === null || _a === void 0 ? void 0 : _a.displayBehavior.isCollapsed(this))) {
            index = this._steps.length;
        }
        if (!this.isNormal && step.getOutcomeInstructions().length) {
            throw new Error(`You cannot add Multi Output Step in the ${this.isError ? "Error" : "End"} stack.`);
        }
        else {
            this._steps.splice(index, 0, step);
            step.sequence = this;
            StateManager.propertyChanged(this, "steps");
        }
    }
    moveStep(step, index) {
        let originIdx = this._steps.indexOf(step);
        if (originIdx < 0)
            return;
        this._steps.splice(originIdx, 1);
        if (index === undefined || index === null) {
            index = this._steps.length;
        }
        this._steps.splice(index, 0, step);
        StateManager.propertyChanged(this, "steps");
    }
    toggleShowDescriptionForSteps(show) {
        for (let step of this._steps) {
            if (step.description !== "") {
                if (step.isShowDescription !== show) {
                    step.isShowDescription = show;
                    StateManager.propertyChanged(step, "isShowDescription");
                }
            }
        }
    }
    renderEditUI() {
        return (_jsx(ObserveState, { listenTo: this, properties: ["color"], control: () => (_jsx(PropertyEditorColor, { "data-testid": `${this._id}_stack-color`, editObject: this, propertyName: "color", label: "Stack Color", updateCallback: () => {
                    StateManager.propertyChanged(this, "color");
                } }, void 0)) }, void 0));
    }
    get isError() {
        return this._id === FlowConstants.ERROR_SEQUENCE;
    }
    get isEnd() {
        return this._id === FlowConstants.END_SEQUENCE;
    }
    get isPrimary() {
        return this._id === FlowConstants.PRIMARY_SEQUENCE;
    }
    get isNormal() {
        return !this.isError && !this.isEnd;
    }
    get isDragOver() {
        var _a;
        return ((_a = this.editContext) === null || _a === void 0 ? void 0 : _a.dragOverSequenceId) === this._id;
    }
    get editContext() {
        var _a;
        return (_a = this.flow) === null || _a === void 0 ? void 0 : _a.context;
    }
    get isOneStep() {
        return this._steps.length <= 1;
    }
}
//# sourceMappingURL=FlowStepSequence.js.map