import { objectWithoutProperties as _objectWithoutProperties, slicedToArray as _slicedToArray, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useRef, useMemo } from 'react';
import { cx } from '../../utils/index.js';
import { base as base$1 } from './variations/_view/base.js';
import { base as base$2 } from './variations/_size/base.js';
import { ListWrapper, Ul, base } from './Dropdown.styles.js';
import { childrenWithProps } from './utils/index.js';
import { classes } from './Dropdown.tokens.js';
import { useKeyNavigation } from './hooks/useKeyboardNavigation.js';
import { useList } from './hooks/useList.js';
import { usePathMaps } from './hooks/usePathMaps.js';
import { FloatingPopover } from './FloatingPopover.js';
import { Context } from './Dropdown.context.js';
import { getVisibleItems } from './utils/getVisibleItems.js';
import { getItemByFocused } from './utils/getItemByFocused.js';
import { safeUseId } from '../../utils/react.js';
import { getPlacement } from './utils/getPlacement.js';
import { getItemId } from './utils/getItemId.js';
import { Inner } from './ui/Inner/Inner.js';

var _excluded = ["items", "trigger", "placement", "children", "variant", "zIndex", "listMaxHeight", "listWidth", "portal", "renderItem", "beforeList", "afterList", "onToggle", "alwaysOpened", "onHover", "onItemSelect", "openByRightClick", "offset", "closeOnSelect", "closeOnOverlayClick", "itemRole", "disabled", "size", "view", "className"];

/**
 * Выпадающий список с поддержкой вложенных пунктов.
 */
var dropdownRoot = function dropdownRoot(Root) {
  return /*#__PURE__*/forwardRef(function (_ref, ref) {
    var _getItemByFocused;
    var items = _ref.items,
      _ref$trigger = _ref.trigger,
      trigger = _ref$trigger === void 0 ? 'click' : _ref$trigger,
      _ref$placement = _ref.placement,
      placement = _ref$placement === void 0 ? 'bottom' : _ref$placement,
      children = _ref.children,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? 'normal' : _ref$variant,
      _ref$zIndex = _ref.zIndex,
      zIndex = _ref$zIndex === void 0 ? 1000 : _ref$zIndex,
      listMaxHeight = _ref.listMaxHeight,
      listWidth = _ref.listWidth,
      portal = _ref.portal,
      renderItem = _ref.renderItem,
      beforeList = _ref.beforeList,
      afterList = _ref.afterList,
      onToggle = _ref.onToggle,
      _ref$alwaysOpened = _ref.alwaysOpened,
      alwaysOpened = _ref$alwaysOpened === void 0 ? false : _ref$alwaysOpened,
      onHover = _ref.onHover,
      onItemSelect = _ref.onItemSelect,
      _ref$openByRightClick = _ref.openByRightClick,
      openByRightClick = _ref$openByRightClick === void 0 ? false : _ref$openByRightClick,
      _ref$offset = _ref.offset,
      offset = _ref$offset === void 0 ? [0, 8] : _ref$offset,
      _ref$closeOnSelect = _ref.closeOnSelect,
      closeOnSelect = _ref$closeOnSelect === void 0 ? true : _ref$closeOnSelect,
      _ref$closeOnOverlayCl = _ref.closeOnOverlayClick,
      closeOnOverlayClick = _ref$closeOnOverlayCl === void 0 ? true : _ref$closeOnOverlayCl,
      _ref$itemRole = _ref.itemRole,
      itemRole = _ref$itemRole === void 0 ? 'option' : _ref$itemRole,
      disabled = _ref.disabled,
      size = _ref.size,
      view = _ref.view,
      className = _ref.className,
      rest = _objectWithoutProperties(_ref, _excluded);
    /**
     * Refs
     */
    var floatingPopoverRef = useRef(null);
    var listWrapperRef = useRef(null);

    /**
     * Hooks
     */
    var visibleItems = useMemo(function () {
      return getVisibleItems(items);
    }, [items]);
    var _useList = useList({
        alwaysOpened: alwaysOpened,
        disabled: disabled,
        onToggle: onToggle,
        closeOnOverlayClick: closeOnOverlayClick,
        floatingPopoverRef: floatingPopoverRef,
        listWrapperRef: listWrapperRef
      }),
      path = _useList.path,
      dispatchPath = _useList.dispatchPath,
      dispatchFocusedPath = _useList.dispatchFocusedPath,
      focusedPath = _useList.focusedPath,
      handleGlobalToggle = _useList.handleGlobalToggle;
    var _usePathMaps = usePathMaps(visibleItems),
      _usePathMaps2 = _slicedToArray(_usePathMaps, 2),
      pathMap = _usePathMaps2[0],
      focusedToValueMap = _usePathMaps2[1];
    var _useKeyNavigation = useKeyNavigation({
        focusedPath: focusedPath,
        dispatchFocusedPath: dispatchFocusedPath,
        path: path,
        dispatchPath: dispatchPath,
        pathMap: pathMap,
        focusedToValueMap: focusedToValueMap,
        handleGlobalToggle: handleGlobalToggle,
        closeOnSelect: closeOnSelect,
        onItemSelect: onItemSelect,
        disabled: disabled
      }),
      onKeyDown = _useKeyNavigation.onKeyDown;
    var isCurrentListOpen = Boolean(path[0]);
    var activeDescendantItemValue = ((_getItemByFocused = getItemByFocused(focusedPath, focusedToValueMap)) === null || _getItemByFocused === void 0 ? void 0 : _getItemByFocused.value) || '';
    var treeId = safeUseId();
    var onContextMenu = function onContextMenu(e) {
      e.preventDefault();
      if (alwaysOpened || disabled) {
        return;
      }
      handleGlobalToggle(!isCurrentListOpen, e);
    };
    return /*#__PURE__*/React__default.createElement(Context.Provider, {
      value: {
        focusedPath: focusedPath,
        size: size,
        variant: variant,
        itemRole: itemRole,
        handleGlobalToggle: handleGlobalToggle,
        closeOnSelect: closeOnSelect,
        onHover: onHover,
        onItemSelect: onItemSelect,
        treeId: treeId,
        renderItem: renderItem
      }
    }, /*#__PURE__*/React__default.createElement(FloatingPopover, {
      ref: floatingPopoverRef,
      opened: isCurrentListOpen,
      onToggle: handleGlobalToggle,
      placement: getPlacement(placement),
      offset: offset,
      portal: portal,
      trigger: trigger,
      target: childrenWithProps(children, {
        role: 'combobox',
        'aria-controls': "".concat(treeId, "_tree_level_1"),
        'aria-expanded': isCurrentListOpen,
        'aria-activedescendant': activeDescendantItemValue ? getItemId(treeId, activeDescendantItemValue.toString()) : '',
        onKeyDown: onKeyDown,
        onContextMenu: openByRightClick ? onContextMenu : undefined
      }),
      zIndex: zIndex,
      isInner: false
    }, /*#__PURE__*/React__default.createElement(Root, _extends({
      className: cx(className, classes.dropdownRoot),
      ref: ref,
      view: view,
      size: size
    }, rest), /*#__PURE__*/React__default.createElement(ListWrapper, {
      ref: listWrapperRef,
      listWidth: listWidth
    }, /*#__PURE__*/React__default.createElement(Ul, {
      id: "".concat(treeId, "_tree_level_1"),
      role: "tree",
      listMaxHeight: listMaxHeight
    }, beforeList, visibleItems.map(function (item, index) {
      return /*#__PURE__*/React__default.createElement(Inner, {
        key: "".concat(index, "/0"),
        item: item,
        currentLevel: 0,
        trigger: trigger,
        path: path,
        dispatchPath: dispatchPath,
        index: index,
        listWidth: listWidth,
        portal: listWrapperRef
      });
    }), afterList)))));
  });
};
var dropdownConfig = {
  name: 'Dropdown',
  tag: 'div',
  layout: dropdownRoot,
  base: base,
  variations: {
    view: {
      css: base$1
    },
    size: {
      css: base$2
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

export { dropdownConfig, dropdownRoot };
