"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;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _Stepstokens = require("./Steps.tokens");
const _Stepsstyles = require("./Steps.styles");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_view/base");
const _base2 = require("./variations/_itemView/base");
const _getItemStatus = require("./utils/getItemStatus");
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;
}
const stepsRoot = (StepItem)=>(Root)=>/*#__PURE__*/ (0, _react.forwardRef)((props, outerRef)=>{
            const { view = 'default', itemView = 'default', size, orientation = 'horizontal', contentAlign = 'left', hasLine = true, hasContent = 'all', hasLoader, items = [], onChange, current, status, ...rest } = props;
            const [prevIndex, setPrevIndex] = (0, _react.useState)();
            const hasIndicator = items.some((item)=>item.indicator != null);
            const isSimple = items.every((item)=>!item.title && !item.content);
            const innerItems = (0, _react.useMemo)(()=>{
                const isUncontrolled = current !== undefined;
                return items.map((item, index)=>{
                    const itemStatus = (0, _getItemStatus.getItemStatus)({
                        isUncontrolled,
                        current,
                        status,
                        index,
                        item
                    });
                    return {
                        ...item,
                        status: itemStatus
                    };
                });
            }, [
                status,
                current,
                items
            ]);
            (0, _react.useEffect)(()=>{
                const calcPrevIndex = ()=>{
                    if (current !== undefined) {
                        setPrevIndex(current);
                    } else {
                        const index = innerItems.findIndex((item)=>item.status === 'active');
                        setPrevIndex(index !== -1 ? index : undefined);
                    }
                };
                calcPrevIndex();
                return ()=>{
                    calcPrevIndex();
                };
            }, [
                current,
                innerItems
            ]);
            return /*#__PURE__*/ _react.default.createElement(Root, {
                size: size,
                view: view,
                itemView: itemView,
                ref: outerRef,
                className: (0, _classnames.default)({
                    [_Stepstokens.classes.verticalOrientation]: orientation === 'vertical',
                    [_Stepstokens.classes.simple]: isSimple,
                    [_Stepstokens.classes.hasIndicator]: hasIndicator
                }),
                ...rest
            }, innerItems.map((item, index)=>{
                const isActive = item.status === 'active';
                const itemContent = hasContent === 'active' && !isActive || hasContent === 'none' ? '' : item.content;
                const itemContentAlign = orientation === 'horizontal' ? contentAlign : 'left';
                const onClick = onChange ? ()=>onChange(item, index, prevIndex) : undefined;
                return /*#__PURE__*/ _react.default.createElement(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,
                    rootView: view,
                    height: item.height,
                    width: item.width
                });
            }));
        });
const stepsConfig = (StepItem)=>({
        name: 'Steps',
        tag: 'div',
        layout: stepsRoot(StepItem),
        base: _Stepsstyles.base,
        variations: {
            size: _base.base,
            view: _base1.base,
            itemView: _base2.base
        },
        defaults: {
            view: 'default',
            size: 'm',
            itemView: 'default'
        }
    });
