"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepNavigation = void 0;
var react_1 = __importDefault(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var Stepper_1 = __importDefault(require("@mui/material/Stepper"));
var Step_1 = __importDefault(require("@mui/material/Step"));
var StepButton_1 = __importDefault(require("@mui/material/StepButton"));
var StepLabel_1 = __importDefault(require("@mui/material/StepLabel"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var ProgressButton_1 = require("./components/ProgressButton");
var styles_1 = require("./styles");
var StepNavigation = function (_a) {
    var activeStep = _a.activeStep, steps = _a.steps, _b = _a.isPending, isPending = _b === void 0 ? false : _b, _c = _a.shouldCompleteSteps, shouldCompleteSteps = _c === void 0 ? true : _c, onConfirm = _a.onConfirm, onChangeStep = _a.onChangeStep;
    var styles = (0, styles_1.useStyles)();
    var isLastStep = activeStep === steps.length - 1;
    var setActiveStep = function (step) {
        if (step !== activeStep) {
            onChangeStep(step);
        }
    };
    var isEnabled = (0, ramda_1.propOr)(true, 'enabled');
    var onBack = function (currentStep) {
        if (currentStep > 0) {
            var prevStep = currentStep - 1;
            if (isEnabled(steps[prevStep])) {
                setActiveStep(prevStep);
            }
            else {
                onBack(prevStep);
            }
        }
    };
    var onNext = function (currentStep) {
        if (currentStep < steps.length - 1) {
            var nextStep = currentStep + 1;
            if (isEnabled(steps[nextStep])) {
                setActiveStep(nextStep);
            }
            else {
                onNext(nextStep);
            }
        }
    };
    var showConfirm = isLastStep && !!onConfirm;
    var isNextButtonEnabled = showConfirm || (0, ramda_1.any)(isEnabled, (0, ramda_1.drop)(activeStep + 1, steps));
    return (react_1.default.createElement("div", { className: styles.container },
        react_1.default.createElement("div", { className: styles.placeholder }),
        react_1.default.createElement(Stepper_1.default, { nonLinear: true, activeStep: activeStep, className: styles.stepper }, steps.map(function (_a, index) {
            var label = _a.label, _b = _a.enabled, enabled = _b === void 0 ? true : _b, hasError = _a.hasError, hasWarning = _a.hasWarning;
            var disabledStep = !enabled || isPending;
            var completedStep = shouldCompleteSteps && !disabledStep && index < activeStep;
            return (react_1.default.createElement(Step_1.default, { key: label, completed: completedStep, disabled: disabledStep },
                react_1.default.createElement(StepButton_1.default, { className: styles.stepButton, onClick: function () { return setActiveStep(index); }, "data-reltio-id": "reltio-search-bulk-update-".concat(index) },
                    react_1.default.createElement(StepLabel_1.default, { error: hasError || hasWarning, classes: { error: hasError ? styles.errorLabel : styles.warningLabel } }, label))));
        })),
        react_1.default.createElement("div", { className: styles.buttons },
            activeStep > 0 && (react_1.default.createElement(Button_1.default, { variant: "outlined", className: styles.backButton, onClick: function () { return onBack(activeStep); }, disabled: isPending, "data-reltio-id": "reltio-search-bulk-update-back" }, ui_i18n_1.default.text('Back'))),
            react_1.default.createElement(ProgressButton_1.ProgressButton, { variant: "contained", color: "primary", disabled: !isNextButtonEnabled, isInProgress: isPending, onClick: isLastStep ? onConfirm : function () { return onNext(activeStep); }, "data-reltio-id": "reltio-search-bulk-update-".concat(isLastStep ? 'confirm' : 'continue') }, showConfirm ? ui_i18n_1.default.text('Confirm') : ui_i18n_1.default.text('Continue')))));
};
exports.StepNavigation = StepNavigation;
