"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepsRenderer = exports.Steps = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var factory_1 = require("../factory");
var icons_1 = require("../components/icons");
var WithRemoteConfig_1 = require("../components/WithRemoteConfig");
var tpl_builtin_1 = require("../utils/tpl-builtin");
var tpl_1 = require("../utils/tpl");
var StepStatus;
(function (StepStatus) {
    StepStatus["wait"] = "wait";
    StepStatus["process"] = "process";
    StepStatus["finish"] = "finish";
    StepStatus["error"] = "error";
})(StepStatus || (StepStatus = {}));
function Steps(props) {
    var className = props.className, cx = props.classnames, steps = props.steps, _a = props.value, value = _a === void 0 ? 0 : _a, status = props.status, data = props.data, source = props.source, config = props.config;
    var stepsRow = tpl_builtin_1.resolveVariable(source, data) ||
        config ||
        steps ||
        [];
    var resolveValue = typeof value === 'string' && isNaN(+value)
        ? tpl_builtin_1.resolveVariable(value, data) || +value
        : +value;
    var valueIndex = stepsRow.findIndex(function (item) { return item.value && item.value === resolveValue; });
    var currentValue = valueIndex !== -1 ? valueIndex : resolveValue;
    var FINISH_ICON = 'check';
    var ERROR_ICON = 'close';
    function getStepStatus(step, i) {
        var stepStatus = StepStatus.wait;
        var icon = step.icon;
        if (i < currentValue) {
            stepStatus = StepStatus.finish;
            !icon && (icon = FINISH_ICON);
        }
        else if (i === currentValue) {
            stepStatus = StepStatus.process;
        }
        if (typeof status === 'string') {
            if (i === currentValue) {
                var resolveStatus = tpl_builtin_1.resolveVariable(status, data);
                stepStatus = resolveStatus || status || StepStatus.process;
                stepStatus === StepStatus.error && !icon && (icon = ERROR_ICON);
            }
        }
        else if (typeof status === 'object') {
            var key = step.value;
            key && status[key] && (stepStatus = status[key]);
        }
        return {
            stepStatus: stepStatus,
            icon: icon
        };
    }
    return (react_1.default.createElement("ul", { className: cx('Steps', className) }, stepsRow.map(function (step, i) {
        var _a = getStepStatus(step, i), stepStatus = _a.stepStatus, icon = _a.icon;
        return (react_1.default.createElement("li", { key: i, className: cx('StepsItem', "is-" + stepStatus, step.className) },
            react_1.default.createElement("div", { className: cx('StepsItem-container') },
                react_1.default.createElement("div", { className: cx('StepsItem-containerIcon') },
                    react_1.default.createElement("span", { className: cx('StepsItem-icon') }, icon ? react_1.default.createElement(icons_1.Icon, { icon: icon, className: "icon" }) : i + 1)),
                react_1.default.createElement("div", { className: cx('StepsItem-containerWrapper') },
                    react_1.default.createElement("div", { className: cx('StepsItem-body') },
                        react_1.default.createElement("div", { className: cx('StepsItem-title', i < currentValue && 'is-success') },
                            react_1.default.createElement("span", null, tpl_1.filter(step.title, data)),
                            react_1.default.createElement("span", { className: cx('StepsItem-subTitle') }, tpl_1.filter(step.subTitle || step.value, data))),
                        react_1.default.createElement("div", { className: cx('StepsItem-description') }, tpl_1.filter(step.description, data)))))));
    })));
}
exports.Steps = Steps;
var StepsWithRemoteConfig = WithRemoteConfig_1.withRemoteConfig({
    adaptor: function (data) { return data.steps || data; }
})(/** @class */ (function (_super) {
    tslib_1.__extends(class_1, _super);
    function class_1() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    class_1.prototype.render = function () {
        var _a = this.props, config = _a.config, deferLoad = _a.deferLoad, loading = _a.loading, updateConfig = _a.updateConfig, rest = tslib_1.__rest(_a, ["config", "deferLoad", "loading", "updateConfig"]);
        return react_1.default.createElement(Steps, tslib_1.__assign({ config: config }, rest));
    };
    return class_1;
}(react_1.default.Component)));
var StepsRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(StepsRenderer, _super);
    function StepsRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    StepsRenderer.prototype.render = function () {
        return react_1.default.createElement(StepsWithRemoteConfig, tslib_1.__assign({}, this.props));
    };
    StepsRenderer = tslib_1.__decorate([
        factory_1.Renderer({
            test: /(^|\/)steps$/,
            name: 'steps'
        })
    ], StepsRenderer);
    return StepsRenderer;
}(react_1.default.Component));
exports.StepsRenderer = StepsRenderer;
//# sourceMappingURL=./renderers/Steps.js.map
