'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var DropdownItem = require('../DropdownItem/DropdownItem.js');
var Dropdown_styles = require('../../Dropdown.styles.js');
var FloatingPopover = require('../../FloatingPopover.js');
var react = require('../../../../utils/react.js');
var getPlacement = require('../../utils/getPlacement.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var DropdownInner = function DropdownInner(_ref) {
  var item = _ref.item,
    currentLevel = _ref.currentLevel,
    path = _ref.path,
    dispatchPath = _ref.dispatchPath,
    index = _ref.index,
    trigger = _ref.trigger,
    listWidth = _ref.listWidth,
    portal = _ref.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 = React.useRef(null);
  var isCurrentListOpen = path[currentLevel + 1] === item.value.toString();
  var treeId = react.safeUseId();
  var listId = "".concat(treeId, "_tree_level_").concat(currentLevel + 2);
  var nextLevel = currentLevel + 1;
  if (item !== null && item !== void 0 && item.items) {
    return /*#__PURE__*/React__default.default.createElement(FloatingPopover.FloatingPopover, {
      placement: getPlacement.getPlacement((item === null || item === void 0 ? void 0 : item.placement) || 'right'),
      opened: isCurrentListOpen,
      onToggle: handleToggle,
      trigger: item.trigger || trigger,
      target: /*#__PURE__*/React__default.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.default.createElement(Dropdown_styles.ListWrapper, {
      ref: listWrapperRef,
      listWidth: listWidth
    }, /*#__PURE__*/React__default.default.createElement(Dropdown_styles.Ul, {
      id: listId,
      role: "group",
      listMaxHeight: item.listMaxHeight
    }, item.beforeList, item.items.map(function (innerItem, innerIndex) {
      return /*#__PURE__*/React__default.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.default.createElement(DropdownItem.DropdownItem, {
    item: item,
    path: path,
    index: index,
    currentLevel: currentLevel,
    ariaLevel: nextLevel
  });
};

exports.DropdownInner = DropdownInner;
//# sourceMappingURL=DropdownInner.js.map
