"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 DEFAULT_ESTIMATED_SLIDE_WIDTH () {
        return DEFAULT_ESTIMATED_SLIDE_WIDTH;
    },
    get ITEM_VIRTUAL_ATTRIBUTE () {
        return ITEM_VIRTUAL_ATTRIBUTE;
    },
    get VIRTUAL_OVERSCAN () {
        return VIRTUAL_OVERSCAN;
    },
    get carouselNewConfig () {
        return carouselNewConfig;
    },
    get carouselNewRoot () {
        return carouselNewRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _Icon = require("../../_Icon");
const _hooks = require("../../../hooks");
const _Carouseltokens = require("./Carousel.tokens");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_view/base");
const _Carouselstyles = require("./Carousel.styles");
const _useCarousel = require("./hooks/useCarousel");
const _ui = require("./ui");
const _utils = require("./utils");
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 VISIBLE_DOTS_DEFAULT = 10;
const DOTS_CENTERED_DEFAULT = false;
const AUTO_PLAY_INTERVAL_DEFAULT = 5000;
const VIRTUAL_OVERSCAN = 3;
const DEFAULT_ESTIMATED_SLIDE_WIDTH = 320;
const ITEM_VIRTUAL_ATTRIBUTE = 'data-carousel-index';
const carouselNewRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ view, size, scrollAlign = 'start', children, controlArrowsDisabled = false, paginationOptions, gap = '20px', className, style, defaultIndex = 0, loop = false, autoPlay = false, autoPlayInterval = AUTO_PLAY_INTERVAL_DEFAULT, swipeEnabled = false, virtual = false, index: outerIndex, onChangeIndex: outerOnChangeIndex, ...rest }, ref)=>{
        const childrenArray = (0, _react.useMemo)(()=>_react.Children.toArray(children), [
            children
        ]);
        const slidesAmount = childrenArray.length;
        const [innerIndex, setInnerIndex] = (0, _react.useState)(()=>(0, _utils.getNormalizedIndex)(defaultIndex, slidesAmount, loop));
        const index = outerIndex ?? innerIndex;
        const onChangeIndex = (0, _react.useCallback)((newIndex)=>{
            const normalizedIndex = (0, _utils.getNormalizedIndex)(newIndex, slidesAmount, loop);
            if (outerOnChangeIndex) {
                outerOnChangeIndex(normalizedIndex);
            }
            setInnerIndex(normalizedIndex);
        }, [
            slidesAmount,
            loop,
            outerOnChangeIndex
        ]);
        const handleClickLeft = ()=>{
            onChangeIndex(index - 1);
        };
        const handleClickRight = ()=>{
            onChangeIndex(index + 1);
        };
        (0, _react.useEffect)(()=>{
            if (!autoPlay || slidesAmount <= 1 || autoPlayInterval <= 0 || !loop && index >= slidesAmount - 1) {
                return;
            }
            const interval = window.setInterval(()=>{
                onChangeIndex(index + 1);
            }, autoPlayInterval);
            return ()=>window.clearInterval(interval);
        }, [
            autoPlay,
            autoPlayInterval,
            index,
            loop,
            slidesAmount
        ]);
        const { scrollRef, trackRef, virtualItems, beforeSpacerWidth, afterSpacerWidth, measureVirtualItem } = (0, _useCarousel.useCarousel)({
            index,
            scrollAlign,
            onChangeIndex,
            swipeEnabled,
            slidesAmount,
            gap,
            virtual
        });
        const slides = (0, _react.useMemo)(()=>{
            if (!virtual) {
                return children;
            }
            return virtualItems.map((virtualItem)=>{
                const child = childrenArray[virtualItem.index];
                return /*#__PURE__*/ _react.default.createElement(_Carouselstyles.CarouselVirtualItem, {
                    key: virtualItem.key,
                    ref: measureVirtualItem(virtualItem.index),
                    [ITEM_VIRTUAL_ATTRIBUTE]: virtualItem.index
                }, child);
            });
        }, [
            children,
            childrenArray,
            measureVirtualItem,
            virtual,
            virtualItems
        ]);
        (0, _hooks.useDisableScroll)(scrollRef);
        return /*#__PURE__*/ _react.default.createElement(Root, {
            className: className,
            style: style,
            size: size,
            view: view,
            ref: ref,
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_Carouselstyles.ControlsWrapper, null, (loop || index !== 0) && !controlArrowsDisabled && /*#__PURE__*/ _react.default.createElement(_Carouselstyles.IconButton, {
            className: _Carouseltokens.classes.leftControlButton,
            onClick: handleClickLeft
        }, /*#__PURE__*/ _react.default.createElement(_Icon.IconDisclosureLeft, {
            size: "m",
            color: "inherit"
        })), /*#__PURE__*/ _react.default.createElement(_Carouselstyles.CarouselWrapper, {
            ref: scrollRef,
            isSwipeEnabled: swipeEnabled
        }, /*#__PURE__*/ _react.default.createElement(_Carouselstyles.CarouselTrack, {
            ref: trackRef,
            gap: gap
        }, virtual && beforeSpacerWidth > 0 && /*#__PURE__*/ _react.default.createElement("div", {
            style: {
                width: beforeSpacerWidth,
                flex: '0 0 auto'
            },
            "aria-hidden": true
        }), slides, virtual && afterSpacerWidth > 0 && /*#__PURE__*/ _react.default.createElement("div", {
            style: {
                width: afterSpacerWidth,
                flex: '0 0 auto'
            },
            "aria-hidden": true
        }))), (loop || index !== slidesAmount - 1) && !controlArrowsDisabled && /*#__PURE__*/ _react.default.createElement(_Carouselstyles.IconButton, {
            className: _Carouseltokens.classes.rightControlButton,
            onClick: handleClickRight
        }, /*#__PURE__*/ _react.default.createElement(_Icon.IconDisclosureRight, {
            size: "m",
            color: "inherit"
        }))), !paginationOptions?.disabled && /*#__PURE__*/ _react.default.createElement(_ui.Dots, {
            index: index,
            onChange: onChangeIndex,
            visibleCount: paginationOptions?.visibleDots || VISIBLE_DOTS_DEFAULT,
            count: slidesAmount,
            centered: paginationOptions?.centered || DOTS_CENTERED_DEFAULT
        }));
    });
const carouselNewConfig = {
    name: 'Carousel',
    tag: 'div',
    layout: carouselNewRoot,
    base: _Carouselstyles.base,
    variations: {
        view: {
            css: _base1.base
        },
        size: {
            css: _base.base
        }
    },
    defaults: {
        view: 'default',
        size: 's'
    }
};
