"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Inner", {
    enumerable: true,
    get: function() {
        return Inner;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../../../utils");
const _Selectstyles = require("../../Select.styles");
const _FloatingPopover = require("../../FloatingPopover");
const _Item = require("./ui/Item/Item");
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 Inner = ({ item, currentLevel, path, dispatchPath, index, listWidth, portal, shift, flip })=>{
    const handleToggle = (opened)=>{
        if (opened) {
            dispatchPath({
                type: 'changed_on_level',
                value: item.value.toString(),
                level: currentLevel + 1
            });
        } else {
            dispatchPath({
                type: 'cut_by_level',
                level: currentLevel + 1
            });
        }
    };
    const listWrapperRef = (0, _react.useRef)(null);
    const isCurrentListOpen = path[currentLevel + 1] === item.value.toString();
    const treeId = (0, _utils.safeUseId)();
    const listId = `${treeId}_tree_level_${currentLevel + 2}`;
    const nextLevel = currentLevel + 1;
    if (!(0, _utils.isEmpty)(item?.items)) {
        return /*#__PURE__*/ _react.default.createElement(_FloatingPopover.FloatingPopover, {
            placement: item?.placement || 'right',
            opened: isCurrentListOpen,
            onToggle: handleToggle,
            target: /*#__PURE__*/ _react.default.createElement(_Item.Item, {
                item: item,
                path: path,
                currentLevel: currentLevel,
                index: index,
                ariaControls: listId,
                ariaExpanded: isCurrentListOpen,
                ariaLevel: nextLevel,
                ariaLabel: item.label
            }),
            isInner: true,
            portal: portal,
            shift: shift,
            flip: flip
        }, /*#__PURE__*/ _react.default.createElement(_Selectstyles.ListWrapper, {
            listWidth: listWidth,
            ref: listWrapperRef
        }, /*#__PURE__*/ _react.default.createElement(_Selectstyles.Ul, {
            role: "group",
            id: listId,
            virtual: false,
            listMaxHeight: item?.listMaxHeight
        }, item.items?.map((innerItem, innerIndex)=>/*#__PURE__*/ _react.default.createElement(Inner, {
                key: `${innerIndex}/${currentLevel}`,
                item: innerItem,
                currentLevel: nextLevel,
                path: path,
                dispatchPath: dispatchPath,
                index: innerIndex,
                listWidth: listWidth,
                portal: listWrapperRef,
                shift: shift,
                flip: flip
            })))));
    }
    return /*#__PURE__*/ _react.default.createElement(_Item.Item, {
        item: item,
        path: path,
        index: index,
        currentLevel: currentLevel,
        ariaLevel: nextLevel
    });
};
