"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 _Dropdownstyles = require("../../Dropdown.styles");
const _FloatingPopover = require("../../FloatingPopover");
const _utils1 = require("../../utils");
const _ui = require("./ui");
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, trigger, listWidth, portal })=>{
    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 (item?.items?.length) {
        return /*#__PURE__*/ _react.default.createElement(_FloatingPopover.FloatingPopover, {
            placement: (0, _utils1.getPlacement)(item?.placement || 'right'),
            opened: isCurrentListOpen,
            onToggle: handleToggle,
            trigger: item.trigger || trigger,
            target: /*#__PURE__*/ _react.default.createElement(_ui.Item, {
                item: item,
                index: index,
                path: path,
                currentLevel: currentLevel,
                ariaControls: listId,
                ariaExpanded: isCurrentListOpen,
                ariaLevel: nextLevel,
                ariaLabel: item.label
            }),
            isInner: true,
            portal: portal
        }, /*#__PURE__*/ _react.default.createElement(_Dropdownstyles.ListWrapper, {
            ref: listWrapperRef,
            listWidth: listWidth
        }, /*#__PURE__*/ _react.default.createElement(_Dropdownstyles.Ul, {
            id: listId,
            role: "group",
            listMaxHeight: item.listMaxHeight
        }, item.beforeList, item.items.map((innerItem, innerIndex)=>/*#__PURE__*/ _react.default.createElement(Inner, {
                key: `${innerIndex}/${currentLevel}`,
                item: innerItem,
                currentLevel: nextLevel,
                path: path,
                dispatchPath: dispatchPath,
                index: innerIndex,
                trigger: trigger,
                listWidth: listWidth,
                portal: listWrapperRef
            })))));
    }
    return /*#__PURE__*/ _react.default.createElement(_ui.Item, {
        item: item,
        path: path,
        index: index,
        currentLevel: currentLevel,
        ariaLevel: nextLevel
    });
};
