'use strict';

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

var React = require('react');
var FloatingPopover = require('../../FloatingPopover.js');
var Combobox_styles = require('../../Combobox.styles.js');
var react = require('../../../../utils/react.js');
var isEmpty = require('../../../../utils/isEmpty.js');
var Item = require('./ui/Item/Item.js');

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

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

var Inner = function Inner(_ref) {
  var item = _ref.item,
    currentLevel = _ref.currentLevel,
    path = _ref.path,
    dispatchPath = _ref.dispatchPath,
    index = _ref.index,
    listWidth = _ref.listWidth,
    portal = _ref.portal,
    shift = _ref.shift,
    flip = _ref.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 = 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 (!isEmpty.isEmpty(item === null || item === void 0 ? void 0 : item.items)) {
    var _item$items;
    return /*#__PURE__*/React__default.default.createElement(FloatingPopover.FloatingPopover, {
      placement: (item === null || item === void 0 ? void 0 : item.placement) || 'right-start',
      opened: isCurrentListOpen,
      onToggle: handleToggle,
      target: /*#__PURE__*/React__default.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.default.createElement(Combobox_styles.ListWrapper, {
      listWidth: listWidth,
      ref: listWrapperRef
    }, /*#__PURE__*/React__default.default.createElement(Combobox_styles.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.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.default.createElement(Item.Item, {
    item: item,
    path: path,
    index: index,
    currentLevel: currentLevel,
    ariaLevel: nextLevel
  });
};

exports.Inner = Inner;
