"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 dropdownConfig () {
        return dropdownConfig;
    },
    get dropdownRoot () {
        return dropdownRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../utils");
const _ui = require("./ui");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _Dropdownstyles = require("./Dropdown.styles");
const _utils1 = require("./utils");
const _Dropdowntokens = require("./Dropdown.tokens");
const _useKeyboardNavigation = require("./hooks/useKeyboardNavigation");
const _useList = require("./hooks/useList");
const _usePathMaps = require("./hooks/usePathMaps");
const _FloatingPopover = require("./FloatingPopover");
const _Dropdowncontext = require("./Dropdown.context");
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 dropdownRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ items, trigger = 'click', placement = 'bottom', children, variant = 'normal', zIndex = 1000, listMaxHeight, listWidth, portal, renderItem, beforeList, afterList, onToggle, alwaysOpened = false, onHover, onItemSelect, openByRightClick = false, offset = [
        0,
        8
    ], closeOnSelect = true, closeOnOverlayClick = true, itemRole = 'option', disabled, size, view, className, ...rest }, ref)=>{
        /**
             * Refs
             */ const floatingPopoverRef = (0, _react.useRef)(null);
        const listWrapperRef = (0, _react.useRef)(null);
        /**
             * Hooks
             */ const visibleItems = (0, _react.useMemo)(()=>(0, _utils1.getVisibleItems)(items), [
            items
        ]);
        const { path, dispatchPath, dispatchFocusedPath, focusedPath, handleGlobalToggle } = (0, _useList.useList)({
            alwaysOpened,
            disabled,
            onToggle,
            closeOnOverlayClick,
            floatingPopoverRef,
            listWrapperRef
        });
        const [pathMap, focusedToValueMap] = (0, _usePathMaps.usePathMaps)(visibleItems);
        const { onKeyDown } = (0, _useKeyboardNavigation.useKeyNavigation)({
            focusedPath,
            dispatchFocusedPath,
            path,
            dispatchPath,
            pathMap,
            focusedToValueMap,
            handleGlobalToggle,
            closeOnSelect,
            onItemSelect,
            disabled
        });
        const isCurrentListOpen = Boolean(path[0]);
        const activeDescendantItemValue = (0, _utils1.getItemByFocused)(focusedPath, focusedToValueMap)?.value || '';
        const treeId = (0, _utils.safeUseId)();
        const onContextMenu = (e)=>{
            e.preventDefault();
            if (alwaysOpened || disabled) {
                return;
            }
            handleGlobalToggle(!isCurrentListOpen, e);
        };
        return /*#__PURE__*/ _react.default.createElement(_Dropdowncontext.Context.Provider, {
            value: {
                focusedPath,
                size,
                variant,
                itemRole,
                handleGlobalToggle,
                closeOnSelect,
                onHover,
                onItemSelect,
                treeId,
                renderItem
            }
        }, /*#__PURE__*/ _react.default.createElement(_FloatingPopover.FloatingPopover, {
            ref: floatingPopoverRef,
            opened: isCurrentListOpen,
            onToggle: handleGlobalToggle,
            placement: (0, _utils1.getPlacement)(placement),
            offset: offset,
            portal: portal,
            trigger: trigger,
            target: (0, _utils1.childrenWithProps)(children, {
                role: 'combobox',
                'aria-controls': `${treeId}_tree_level_1`,
                'aria-expanded': isCurrentListOpen,
                'aria-activedescendant': activeDescendantItemValue ? (0, _utils1.getItemId)(treeId, activeDescendantItemValue.toString()) : '',
                onKeyDown,
                onContextMenu: openByRightClick ? onContextMenu : undefined
            }),
            zIndex: zIndex,
            isInner: false
        }, /*#__PURE__*/ _react.default.createElement(Root, {
            className: (0, _utils.cx)(className, _Dropdowntokens.classes.dropdownRoot),
            ref: ref,
            view: view,
            size: size,
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_Dropdownstyles.ListWrapper, {
            ref: listWrapperRef,
            listWidth: listWidth
        }, /*#__PURE__*/ _react.default.createElement(_Dropdownstyles.Ul, {
            id: `${treeId}_tree_level_1`,
            role: "tree",
            listMaxHeight: listMaxHeight
        }, beforeList, visibleItems.map((item, index)=>/*#__PURE__*/ _react.default.createElement(_ui.Inner, {
                key: `${index}/0`,
                item: item,
                currentLevel: 0,
                trigger: trigger,
                path: path,
                dispatchPath: dispatchPath,
                index: index,
                listWidth: listWidth,
                portal: listWrapperRef
            })), afterList)))));
    });
const dropdownConfig = {
    name: 'Dropdown',
    tag: 'div',
    layout: dropdownRoot,
    base: _Dropdownstyles.base,
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
