import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import ReactDraggable from "react-draggable";
import { CloseGrayIcon } from "../../../CommonUI/Icon/CloseGrayIcon";
import { StateManager } from "../../../CommonUI/StateManagement/StateManager";
import { ClickAwayListener, TableContainer, TableHead, TableCell, OutlinedInput, Select, Table, TableRow, TableBody, Typography, Button } from "@material-ui/core";
import { FlowStepOutputInstructionType } from '../../Structure/FlowStepOutputInstructions';
import { FlowStepSequenceStrategy } from '../../Structure/FlowStepSequence';
import { StrategyDialogContent, StrategyDialogDiv } from '../CommonStyles';
import { RegularStepStrategy } from '../Regular/StepStrategy';
import { BaseStepStrategy } from '../BaseStepStrategy';
import { ObserveState } from '../../../CommonUI/StateManagement/ObserveState';
export class DiagramStepStrategy extends BaseStepStrategy {
    render() {
        const { editContext } = this.props;
        if (!editContext.selectedStepStrategy) {
            return _jsx(_Fragment, {}, void 0);
        }
        let { step, position } = editContext.selectedStepStrategy;
        let sequence = step.sequence;
        if (!sequence)
            return _jsx(_Fragment, {}, void 0);
        if (sequence.steps.length !== 1)
            return _jsx(RegularStepStrategy, { editContext: editContext }, void 0);
        let paths = step.getOutcomeInstructions() || [];
        // add hidden step to edit dialog
        if (!Reflect.get(step, 'hidden')) {
            paths = [...sequence.hiddenStep.getOutcomeInstructions(), ...paths];
        }
        else {
            paths = [...paths, ...sequence.steps[0].getOutcomeInstructions()];
        }
        if (paths.length === 0) {
            return _jsx(_Fragment, {}, void 0);
        }
        return _jsx(ClickAwayListener, Object.assign({ onClickAway: this.onUnselectStepStrategyConnect }, { children: _jsx(ReactDraggable, Object.assign({ handle: ".StrategyDialog-title", defaultPosition: position }, { children: _jsx(StrategyDialogDiv, Object.assign({ "data-testid": "select-strategy-dialog" }, { children: _jsxs(StrategyDialogContent, Object.assign({ onClick: (e) => e.stopPropagation() }, { children: [_jsx(Button, Object.assign({ onClick: this.onUnselectStepStrategyConnect, className: 'StrategyDialog-close' }, { children: _jsx(CloseGrayIcon, {}, void 0) }), void 0),
                            _jsxs(Typography, Object.assign({ className: 'StrategyDialog-title' }, { children: [step === null || step === void 0 ? void 0 : step.name, " Paths"] }), void 0),
                            _jsx(TableContainer, Object.assign({ className: "StrategyDialog-table" }, { children: _jsxs(Table, { children: [_jsx(TableHead, { children: _jsxs(TableRow, { children: [_jsx(TableCell, { children: "Path" }, void 0),
                                                    _jsx(TableCell, { children: "Strategies" }, void 0)] }, void 0) }, void 0),
                                        _jsx(TableBody, { children: paths.map((instruction, idx) => {
                                                var _a;
                                                if (step.sequence.isOneStep) {
                                                    if (step.sequence.steps[0].getOutcomeInstructions()[0] === instruction) {
                                                        return null;
                                                    }
                                                }
                                                return (_jsxs(TableRow, { children: [_jsx(TableCell, { children: step.sequence.hiddenStep._id === instruction.stepId ? (((_a = paths[1]) === null || _a === void 0 ? void 0 : _a.pathName) || 'Done') : instruction.pathName }, void 0),
                                                        _jsxs(TableCell, Object.assign({ align: idx > 0 ? 'right' : 'left', style: { width: 196 } }, { children: [_jsxs(Select, Object.assign({ fullWidth: true, className: `StrategyDialog-select ${instruction.strategy === FlowStepOutputInstructionType.THROW_EXCEPTION && 'StrategyDialog-select-error'}`, variant: "outlined", native: true, value: instruction.strategy, onChange: (event) => {
                                                                        instruction.strategy = event.target.value;
                                                                        if (step.sequence && step.sequence.isOneStep && step.sequence.hiddenStep._id === instruction.stepId) {
                                                                            switch (event.target.value) {
                                                                                case FlowStepOutputInstructionType.BRANCH:
                                                                                    step.sequence.strategy = FlowStepSequenceStrategy.LINK;
                                                                                    break;
                                                                                case FlowStepOutputInstructionType.END_FLOW:
                                                                                    step.sequence.strategy = FlowStepSequenceStrategy.ENDFLOW;
                                                                                    break;
                                                                                case FlowStepOutputInstructionType.THROW_EXCEPTION:
                                                                                    step.sequence.strategy = FlowStepSequenceStrategy.ENDFLOWINERROR;
                                                                                    break;
                                                                            }
                                                                        }
                                                                        editContext.onSequenceUpdate(step.sequence);
                                                                    }, "data-testid": `select-strategy-dialog_${instruction.pathName}` }, { children: [_jsx("option", Object.assign({ value: FlowStepOutputInstructionType.THROW_EXCEPTION }, { children: "End in error" }), void 0),
                                                                        _jsx("option", Object.assign({ value: FlowStepOutputInstructionType.END_FLOW }, { children: "End flow" }), void 0),
                                                                        _jsx("option", Object.assign({ value: FlowStepOutputInstructionType.BRANCH }, { children: "Link to new step" }), void 0)] }), void 0),
                                                                instruction.strategy === FlowStepOutputInstructionType.THROW_EXCEPTION &&
                                                                    _jsx(ObserveState, { listenTo: instruction, properties: ['errorMessage'], control: () => _jsx(OutlinedInput, { className: "Strategy-error", value: instruction.errorMessage, "data-testid": `select-strategy-dialog_${instruction.pathName}-set-error`, onChange: (e) => {
                                                                                instruction.errorMessage = e.target.value;
                                                                                StateManager.propertyChanged(instruction, 'errorMessage');
                                                                            }, placeholder: "Error Message" }, void 0) }, void 0)] }), void 0)] }, idx));
                                            }) }, void 0)] }, void 0) }), void 0)] }), void 0) }), void 0) }), void 0) }), void 0);
    }
}
//# sourceMappingURL=StepStrategey.js.map