"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "DropdownInner", {
    enumerable: true,
    get: function() {
        return DropdownInner;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _utils = require("../../../../utils");
var _DropdownItem = require("../DropdownItem/DropdownItem");
var _Dropdownstyles = require("../../Dropdown.styles");
var _FloatingPopover = require("../../FloatingPopover");
var _utils1 = require("../../utils");
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 DropdownInner = function DropdownInner1(param) {
    var item = param.item, currentLevel = param.currentLevel, path = param.path, dispatchPath = param.dispatchPath, index = param.index, trigger = param.trigger, listWidth = param.listWidth, portal = param.portal;
    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 (item === null || item === void 0 ? void 0 : item.items) {
        return /*#__PURE__*/ _react.default.createElement(_FloatingPopover.FloatingPopover, {
            placement: (0, _utils1.getPlacement)((item === null || item === void 0 ? void 0 : item.placement) || 'right'),
            opened: isCurrentListOpen,
            onToggle: handleToggle,
            trigger: item.trigger || trigger,
            target: /*#__PURE__*/ _react.default.createElement(_DropdownItem.DropdownItem, {
                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(function(innerItem, innerIndex) {
            return /*#__PURE__*/ _react.default.createElement(DropdownInner, {
                key: "".concat(innerIndex, "/").concat(currentLevel),
                item: innerItem,
                currentLevel: nextLevel,
                path: path,
                dispatchPath: dispatchPath,
                index: innerIndex,
                trigger: trigger,
                listWidth: listWidth,
                portal: listWrapperRef
            });
        }))));
    }
    return /*#__PURE__*/ _react.default.createElement(_DropdownItem.DropdownItem, {
        item: item,
        path: path,
        index: index,
        currentLevel: currentLevel,
        ariaLevel: nextLevel
    });
};
