import { objectWithoutProperties as _objectWithoutProperties, slicedToArray as _slicedToArray, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useReducer, useRef, useCallback } from 'react';
import { cx } from '../../utils/index.js';
import { pathReducer } from './reducers/pathReducer.js';
import { focusedPathReducer } from './reducers/focusedPathReducer.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 { useHashMaps } from './hooks/useHashMaps.js';
import { FloatingPopover } from './FloatingPopover.js';
import { Context } from './Dropdown.context.js';
import { getItemByFocused } from './utils/getItemByFocused.js';
import { safeUseId } from '../../utils/react.js';
import { useOutsideClick } from '../../hooks/useOutsideClick.js';
import { getPlacement } from './utils/getPlacement.js';
import { getItemId } from './utils/getItemId.js';
import { DropdownInner } from './ui/DropdownInner/DropdownInner.js';

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

/**
 * Выпадающий список.
 */
var dropdownRoot = function dropdownRoot(Root) {
  return /*#__PURE__*/forwardRef(function (_ref, ref) {
    var _getItemByFocused;
    var items = _ref.items,
      children = _ref.children,
      placement = _ref.placement,
      offset = _ref.offset,
      _ref$closeOnOverlayCl = _ref.closeOnOverlayClick,
      closeOnOverlayClick = _ref$closeOnOverlayCl === void 0 ? true : _ref$closeOnOverlayCl,
      onToggle = _ref.onToggle,
      size = _ref.size,
      view = _ref.view,
      _ref$itemRole = _ref.itemRole,
      itemRole = _ref$itemRole === void 0 ? 'treeitem' : _ref$itemRole,
      className = _ref.className,
      listMaxHeight = _ref.listMaxHeight,
      listWidth = _ref.listWidth,
      listHeight = _ref.listHeight,
      _ref$closeOnSelect = _ref.closeOnSelect,
      closeOnSelect = _ref$closeOnSelect === void 0 ? true : _ref$closeOnSelect,
      onHover = _ref.onHover,
      onItemSelect = _ref.onItemSelect,
      onItemClick = _ref.onItemClick,
      _ref$trigger = _ref.trigger,
      trigger = _ref$trigger === void 0 ? 'click' : _ref$trigger,
      _ref$openByRightClick = _ref.openByRightClick,
      openByRightClick = _ref$openByRightClick === void 0 ? false : _ref$openByRightClick,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? 'normal' : _ref$variant,
      _ref$hasArrow = _ref.hasArrow,
      hasArrow = _ref$hasArrow === void 0 ? true : _ref$hasArrow,
      _ref$alwaysOpened = _ref.alwaysOpened,
      alwaysOpened = _ref$alwaysOpened === void 0 ? false : _ref$alwaysOpened,
      portal = _ref.portal,
      renderItem = _ref.renderItem,
      zIndex = _ref.zIndex,
      beforeList = _ref.beforeList,
      afterList = _ref.afterList,
      rest = _objectWithoutProperties(_ref, _excluded);
    var initialPath = alwaysOpened ? ['root'] : [];
    var _useReducer = useReducer(pathReducer, initialPath),
      _useReducer2 = _slicedToArray(_useReducer, 2),
      path = _useReducer2[0],
      dispatchPath = _useReducer2[1];
    var _useReducer3 = useReducer(focusedPathReducer, []),
      _useReducer4 = _slicedToArray(_useReducer3, 2),
      focusedPath = _useReducer4[0],
      dispatchFocusedPath = _useReducer4[1];
    var isCurrentListOpen = Boolean(path[0]);
    var _useHashMaps = useHashMaps(items),
      _useHashMaps2 = _slicedToArray(_useHashMaps, 2),
      pathMap = _useHashMaps2[0],
      focusedToValueMap = _useHashMaps2[1];
    var activeDescendantItemValue = ((_getItemByFocused = getItemByFocused(focusedPath, focusedToValueMap)) === null || _getItemByFocused === void 0 ? void 0 : _getItemByFocused.value) || '';
    var floatingPopoverRef = useRef(null);
    var listWrapperRef = useRef(null);
    var treeId = safeUseId();

    /* Логика работы при клике за пределами выпадающего списка */
    useOutsideClick(function (event) {
      if (!isCurrentListOpen || !closeOnOverlayClick || alwaysOpened) {
        return;
      }
      dispatchPath({
        type: 'reset'
      });
      dispatchFocusedPath({
        type: 'reset'
      });
      if (onToggle) {
        onToggle(false, event);
      }
    }, [floatingPopoverRef, listWrapperRef]);
    var handleGlobalToggle = function handleGlobalToggle(opened, event) {
      if (alwaysOpened || opened) {
        dispatchPath({
          type: 'opened_first_level'
        });
      } else {
        dispatchFocusedPath({
          type: 'reset'
        });
        dispatchPath({
          type: 'reset'
        });
      }
      if (onToggle) {
        onToggle(opened, event);
      }
    };
    var onContextMenu = useCallback(function (e) {
      e.preventDefault();
      if (alwaysOpened) {
        return;
      }
      handleGlobalToggle(!isCurrentListOpen, e);
    }, [handleGlobalToggle, isCurrentListOpen, alwaysOpened]);
    var _useKeyNavigation = useKeyNavigation({
        focusedPath: focusedPath,
        dispatchFocusedPath: dispatchFocusedPath,
        path: path,
        dispatchPath: dispatchPath,
        pathMap: pathMap,
        focusedToValueMap: focusedToValueMap,
        handleGlobalToggle: handleGlobalToggle,
        closeOnSelect: closeOnSelect,
        onItemSelect: onItemSelect,
        onItemClick: onItemClick
      }),
      onKeyDown = _useKeyNavigation.onKeyDown;
    return /*#__PURE__*/React__default.createElement(Context.Provider, {
      value: {
        focusedPath: focusedPath,
        size: size,
        variant: variant,
        itemRole: itemRole,
        handleGlobalToggle: handleGlobalToggle,
        closeOnSelect: closeOnSelect,
        onHover: onHover,
        onItemClick: onItemClick,
        onItemSelect: onItemSelect,
        hasArrow: hasArrow,
        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 || listHeight
    }, beforeList, items.map(function (item, index) {
      return /*#__PURE__*/React__default.createElement(DropdownInner, {
        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 };
//# sourceMappingURL=Dropdown.js.map
