"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Inner", {
    enumerable: true,
    get: function() {
        return Inner;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _utils = require("../../../../utils");
var _Selectstyles = require("../../Select.styles");
var _FloatingPopover = require("../../FloatingPopover");
var _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;
}
var Inner = function Inner1(param) {
    var item = param.item, currentLevel = param.currentLevel, path = param.path, dispatchPath = param.dispatchPath, index = param.index, listWidth = param.listWidth, portal = param.portal, shift = param.shift, flip = param.flip;
    var handleToggle = function 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
            });
        }
    };
    var listWrapperRef = (0, _react.useRef)(null);
    var isCurrentListOpen = path[currentLevel + 1] === item.value.toString();
    var treeId = (0, _utils.safeUseId)();
    var listId = "".concat(treeId, "_tree_level_").concat(currentLevel + 2);
    var nextLevel = currentLevel + 1;
    if (!(0, _utils.isEmpty)(item === null || item === void 0 ? void 0 : item.items)) {
        var _item_items;
        return /*#__PURE__*/ _react.default.createElement(_FloatingPopover.FloatingPopover, {
            placement: (item === null || item === void 0 ? void 0 : 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 === null || item === void 0 ? void 0 : item.listMaxHeight
        }, (_item_items = item.items) === null || _item_items === void 0 ? void 0 : _item_items.map(function(innerItem, innerIndex) {
            return /*#__PURE__*/ _react.default.createElement(Inner, {
                key: "".concat(innerIndex, "/").concat(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
    });
};
