"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;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _Icon = require("../../_Icon");
var _hooks = require("../../../hooks");
var _Carouseltokens = require("./Carousel.tokens");
var _base = require("./variations/_size/base");
var _base1 = require("./variations/_view/base");
var _Carouselstyles = require("./Carousel.styles");
var _useCarousel = require("./hooks/useCarousel");
var _ui = require("./ui");
var _utils = require("./utils");
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 _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(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
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 _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 _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _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 VISIBLE_DOTS_DEFAULT = 10;
var DOTS_CENTERED_DEFAULT = false;
var AUTO_PLAY_INTERVAL_DEFAULT = 5000;
var VIRTUAL_OVERSCAN = 3;
var DEFAULT_ESTIMATED_SLIDE_WIDTH = 320;
var ITEM_VIRTUAL_ATTRIBUTE = 'data-carousel-index';
var carouselNewRoot = function carouselNewRoot(Root) {
    return /*#__PURE__*/ (0, _react.forwardRef)(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), view = _ref2.view, size = _ref2.size, _ref_scrollAlign = _ref2.scrollAlign, scrollAlign = _ref_scrollAlign === void 0 ? 'start' : _ref_scrollAlign, children = _ref2.children, _ref_controlArrowsDisabled = _ref2.controlArrowsDisabled, controlArrowsDisabled = _ref_controlArrowsDisabled === void 0 ? false : _ref_controlArrowsDisabled, paginationOptions = _ref2.paginationOptions, _ref_gap = _ref2.gap, gap = _ref_gap === void 0 ? '20px' : _ref_gap, className = _ref2.className, style = _ref2.style, _ref_defaultIndex = _ref2.defaultIndex, defaultIndex = _ref_defaultIndex === void 0 ? 0 : _ref_defaultIndex, _ref_loop = _ref2.loop, loop = _ref_loop === void 0 ? false : _ref_loop, _ref_autoPlay = _ref2.autoPlay, autoPlay = _ref_autoPlay === void 0 ? false : _ref_autoPlay, _ref_autoPlayInterval = _ref2.autoPlayInterval, autoPlayInterval = _ref_autoPlayInterval === void 0 ? AUTO_PLAY_INTERVAL_DEFAULT : _ref_autoPlayInterval, _ref_swipeEnabled = _ref2.swipeEnabled, swipeEnabled = _ref_swipeEnabled === void 0 ? false : _ref_swipeEnabled, _ref_virtual = _ref2.virtual, virtual = _ref_virtual === void 0 ? false : _ref_virtual, outerIndex = _ref2.index, outerOnChangeIndex = _ref2.onChangeIndex, rest = _object_without_properties(_ref2, [
            "view",
            "size",
            "scrollAlign",
            "children",
            "controlArrowsDisabled",
            "paginationOptions",
            "gap",
            "className",
            "style",
            "defaultIndex",
            "loop",
            "autoPlay",
            "autoPlayInterval",
            "swipeEnabled",
            "virtual",
            "index",
            "onChangeIndex"
        ]), _rest1 = _sliced_to_array(_rest, 1), ref = _rest1[0];
        var childrenArray = (0, _react.useMemo)(function() {
            return _react.Children.toArray(children);
        }, [
            children
        ]);
        var slidesAmount = childrenArray.length;
        var _useState = _sliced_to_array((0, _react.useState)(function() {
            return (0, _utils.getNormalizedIndex)(defaultIndex, slidesAmount, loop);
        }), 2), innerIndex = _useState[0], setInnerIndex = _useState[1];
        var index = outerIndex !== null && outerIndex !== void 0 ? outerIndex : innerIndex;
        var onChangeIndex = (0, _react.useCallback)(function(newIndex) {
            var normalizedIndex = (0, _utils.getNormalizedIndex)(newIndex, slidesAmount, loop);
            if (outerOnChangeIndex) {
                outerOnChangeIndex(normalizedIndex);
            }
            setInnerIndex(normalizedIndex);
        }, [
            slidesAmount,
            loop,
            outerOnChangeIndex
        ]);
        var handleClickLeft = function handleClickLeft() {
            onChangeIndex(index - 1);
        };
        var handleClickRight = function handleClickRight() {
            onChangeIndex(index + 1);
        };
        (0, _react.useEffect)(function() {
            if (!autoPlay || slidesAmount <= 1 || autoPlayInterval <= 0 || !loop && index >= slidesAmount - 1) {
                return;
            }
            var interval = window.setInterval(function() {
                onChangeIndex(index + 1);
            }, autoPlayInterval);
            return function() {
                return window.clearInterval(interval);
            };
        }, [
            autoPlay,
            autoPlayInterval,
            index,
            loop,
            slidesAmount
        ]);
        var _useCarousel1 = (0, _useCarousel.useCarousel)({
            index: index,
            scrollAlign: scrollAlign,
            onChangeIndex: onChangeIndex,
            swipeEnabled: swipeEnabled,
            slidesAmount: slidesAmount,
            gap: gap,
            virtual: virtual
        }), scrollRef = _useCarousel1.scrollRef, trackRef = _useCarousel1.trackRef, virtualItems = _useCarousel1.virtualItems, beforeSpacerWidth = _useCarousel1.beforeSpacerWidth, afterSpacerWidth = _useCarousel1.afterSpacerWidth, measureVirtualItem = _useCarousel1.measureVirtualItem;
        var slides = (0, _react.useMemo)(function() {
            if (!virtual) {
                return children;
            }
            return virtualItems.map(function(virtualItem) {
                var child = childrenArray[virtualItem.index];
                return /*#__PURE__*/ _react.default.createElement(_Carouselstyles.CarouselVirtualItem, _define_property({
                    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, _object_spread({
            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 === null || paginationOptions === void 0 ? void 0 : paginationOptions.disabled) && /*#__PURE__*/ _react.default.createElement(_ui.Dots, {
            index: index,
            onChange: onChangeIndex,
            visibleCount: (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.visibleDots) || VISIBLE_DOTS_DEFAULT,
            count: slidesAmount,
            centered: (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.centered) || DOTS_CENTERED_DEFAULT
        }));
    });
};
var carouselNewConfig = {
    name: 'Carousel',
    tag: 'div',
    layout: carouselNewRoot,
    base: _Carouselstyles.base,
    variations: {
        view: {
            css: _base1.base
        },
        size: {
            css: _base.base
        }
    },
    defaults: {
        view: 'default',
        size: 's'
    }
};
