"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get stepsConfig () {
        return stepsConfig;
    },
    get stepsRoot () {
        return stepsRoot;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _Stepstokens = require("./Steps.tokens");
var _ui = require("./ui");
var _Stepsstyles = require("./Steps.styles");
var _base = require("./variations/_size/base");
var _base1 = require("./variations/_view/base");
var _base2 = require("./variations/_itemView/base");
var _getItemStatus = require("./utils/getItemStatus");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var stepsRoot = function stepsRoot(Root) {
    return /*#__PURE__*/ (0, _react.forwardRef)(function(props, outerRef) {
        var _props_view = props.view, view = _props_view === void 0 ? 'default' : _props_view, _props_itemView = props.itemView, itemView = _props_itemView === void 0 ? 'default' : _props_itemView, size = props.size, _props_orientation = props.orientation, orientation = _props_orientation === void 0 ? 'horizontal' : _props_orientation, _props_contentAlign = props.contentAlign, contentAlign = _props_contentAlign === void 0 ? 'left' : _props_contentAlign, _props_hasLine = props.hasLine, hasLine = _props_hasLine === void 0 ? true : _props_hasLine, _props_hasContent = props.hasContent, hasContent = _props_hasContent === void 0 ? 'all' : _props_hasContent, hasLoader = props.hasLoader, _props_items = props.items, items = _props_items === void 0 ? [] : _props_items, onChange = props.onChange, current = props.current, status = props.status, rest = _object_without_properties(props, [
            "view",
            "itemView",
            "size",
            "orientation",
            "contentAlign",
            "hasLine",
            "hasContent",
            "hasLoader",
            "items",
            "onChange",
            "current",
            "status"
        ]);
        var _useState = _sliced_to_array((0, _react.useState)(), 2), prevIndex = _useState[0], setPrevIndex = _useState[1];
        var hasIndicator = items.some(function(item) {
            return item.indicator != null;
        });
        var isSimple = items.every(function(item) {
            return !item.title && !item.content;
        });
        var innerItems = (0, _react.useMemo)(function() {
            var isUncontrolled = current !== undefined;
            return items.map(function(item, index) {
                var itemStatus = (0, _getItemStatus.getItemStatus)({
                    isUncontrolled: isUncontrolled,
                    current: current,
                    status: status,
                    index: index,
                    item: item
                });
                return _object_spread_props(_object_spread({}, item), {
                    status: itemStatus
                });
            });
        }, [
            status,
            current,
            items
        ]);
        (0, _react.useEffect)(function() {
            var calcPrevIndex = function calcPrevIndex() {
                if (current !== undefined) {
                    setPrevIndex(current);
                } else {
                    var index = innerItems.findIndex(function(item) {
                        return item.status === 'active';
                    });
                    setPrevIndex(index !== -1 ? index : undefined);
                }
            };
            calcPrevIndex();
            return function() {
                calcPrevIndex();
            };
        }, [
            current,
            innerItems
        ]);
        var _obj;
        return /*#__PURE__*/ _react.default.createElement(Root, _object_spread({
            size: size,
            view: view,
            itemView: itemView,
            ref: outerRef,
            className: (0, _classnames.default)((_obj = {}, _define_property(_obj, _Stepstokens.classes.verticalOrientation, orientation === 'vertical'), _define_property(_obj, _Stepstokens.classes.simple, isSimple), _define_property(_obj, _Stepstokens.classes.hasIndicator, hasIndicator), _obj))
        }, rest), innerItems.map(function(item, index) {
            var isActive = item.status === 'active';
            var itemContent = hasContent === 'active' && !isActive || hasContent === 'none' ? '' : item.content;
            var itemContentAlign = orientation === 'horizontal' ? contentAlign : 'left';
            var onClick = onChange ? function() {
                return onChange(item, index, prevIndex);
            } : undefined;
            return /*#__PURE__*/ _react.default.createElement(_ui.StepItem, {
                key: index,
                index: index,
                title: item.title,
                content: itemContent,
                indicator: item.indicator,
                status: item.status,
                size: size,
                orientation: orientation,
                contentAlign: itemContentAlign,
                hasLine: hasLine,
                hasLoader: hasLoader && isActive,
                onClick: onClick,
                items: innerItems
            });
        }));
    });
};
var stepsConfig = {
    name: 'Steps',
    tag: 'div',
    layout: stepsRoot,
    base: _Stepsstyles.base,
    variations: {
        size: _base.base,
        view: _base1.base,
        itemView: _base2.base
    },
    defaults: {
        view: 'default',
        size: 'm',
        itemView: 'default'
    }
};
