import { slicedToArray as _slicedToArray, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useState, useMemo } from 'react';
import Tree from 'rc-tree';
import cls from 'classnames';
import { base, SpinnerStyled, IconArrowWrapper, StyledArrow, StyledFolder, TitleWrapper, Title, ContentRight, Line } from './Tree.styles.js';
import { classes } from './Tree.tokens.js';
import { traverseTree } from './utils/traverseTree.js';
import { sizeToIconSize } from './utils/sizeToIconSize.js';

var _SpinnerStyled;

/**
 * Многоуровневый раскрывающийся список в виде дерева.
 */
var treeRoot = function treeRoot(Root) {
  return /*#__PURE__*/forwardRef(function (_ref, ref) {
    var items = _ref.items,
      _ref$arrowPlacement = _ref.arrowPlacement,
      arrowPlacement = _ref$arrowPlacement === void 0 ? 'left' : _ref$arrowPlacement,
      _ref$virtual = _ref.virtual,
      virtual = _ref$virtual === void 0 ? true : _ref$virtual,
      height = _ref.height,
      itemHeight = _ref.itemHeight,
      size = _ref.size,
      view = _ref.view,
      variant = _ref.variant,
      _ref$fullWidthItemSel = _ref.fullWidthItemSelection,
      fullWidthItemSelection = _ref$fullWidthItemSel === void 0 ? false : _ref$fullWidthItemSel,
      _ref$multiple = _ref.multiple,
      multiple = _ref$multiple === void 0 ? false : _ref$multiple,
      _ref$defaultExpandAll = _ref.defaultExpandAll,
      defaultExpandAll = _ref$defaultExpandAll === void 0 ? false : _ref$defaultExpandAll,
      _ref$autoExpandParent = _ref.autoExpandParent,
      autoExpandParent = _ref$autoExpandParent === void 0 ? false : _ref$autoExpandParent,
      _ref$checkable = _ref.checkable,
      checkable = _ref$checkable === void 0 ? false : _ref$checkable,
      outerSelectedKeys = _ref.selectedKeys,
      checkedKeys = _ref.checkedKeys,
      expandedKeys = _ref.expandedKeys,
      defaultCheckedKeys = _ref.defaultCheckedKeys,
      defaultExpandedKeys = _ref.defaultExpandedKeys,
      defaultSelectedKeys = _ref.defaultSelectedKeys,
      className = _ref.className,
      onTreeSelect = _ref.onTreeSelect,
      onTreeCheck = _ref.onTreeCheck,
      onTreeExpand = _ref.onTreeExpand,
      _ref$hasIcon = _ref.hasIcon,
      hasIcon = _ref$hasIcon === void 0 ? false : _ref$hasIcon,
      icon = _ref.icon,
      renderTitle = _ref.renderTitle,
      _ref$singleLine = _ref.singleLine,
      singleLine = _ref$singleLine === void 0 ? true : _ref$singleLine,
      _ref$mode = _ref.mode,
      mode = _ref$mode === void 0 ? 'default' : _ref$mode,
      _ref$draggable = _ref.draggable,
      draggable = _ref$draggable === void 0 ? false : _ref$draggable,
      onDragStart = _ref.onDragStart,
      onDragEnter = _ref.onDragEnter,
      onDragOver = _ref.onDragOver,
      onDragLeave = _ref.onDragLeave,
      onDragEnd = _ref.onDragEnd,
      onDrop = _ref.onDrop,
      allowDrop = _ref.allowDrop,
      loadData = _ref.loadData,
      onLoadData = _ref.onLoadData;
    var _useState = useState(defaultSelectedKeys || []),
      _useState2 = _slicedToArray(_useState, 2),
      selected = _useState2[0],
      setSelected = _useState2[1];
    var selectedKeys = outerSelectedKeys !== null && outerSelectedKeys !== void 0 ? outerSelectedKeys : selected;
    var isRadioMode = mode === 'radio' && !multiple;
    var handleSelect = function handleSelect(updatedSelectedKeys, info) {
      if (!multiple && isRadioMode && updatedSelectedKeys.length === 0) {
        return;
      }
      setSelected(updatedSelectedKeys);
      if (onTreeSelect) {
        onTreeSelect(updatedSelectedKeys, info);
      }
    };
    var invertedClass = arrowPlacement === 'right' ? classes.treeInverted : undefined;
    var itemFilledClass = fullWidthItemSelection ? classes.treeItemFilled : undefined;
    var radioModeClass = isRadioMode ? classes.treeRadioMode : undefined;
    var tightVariantClass = variant === 'tight' ? classes.treeTightVariant : undefined;

    // Проходимся по дереву и устанавливаем соответствующие классы для узлов дерева,
    // чтобы соблюсти иерархию вложенности в UI.
    var treeData = useMemo(function () {
      return traverseTree(items, selectedKeys);
    }, [items, selectedKeys]);
    return /*#__PURE__*/React__default.createElement(Root, {
      view: view,
      size: size,
      variant: variant,
      ref: ref,
      items: items
    }, /*#__PURE__*/React__default.createElement(Tree, _extends({
      loadData: loadData,
      onLoad: onLoadData,
      height: height,
      itemHeight: itemHeight,
      virtual: virtual,
      multiple: multiple,
      checkable: checkable,
      className: cls(className, invertedClass, itemFilledClass, radioModeClass, tightVariantClass),
      defaultExpandAll: defaultExpandAll,
      autoExpandParent: autoExpandParent,
      style: {
        border: '1px solid #000'
      },
      treeData: treeData,
      selectedKeys: selectedKeys
    }, checkedKeys !== undefined ? {
      checkedKeys: checkedKeys
    } : {}, expandedKeys !== undefined ? {
      expandedKeys: expandedKeys
    } : {}, {
      defaultCheckedKeys: defaultCheckedKeys,
      defaultExpandedKeys: defaultExpandedKeys,
      onSelect: handleSelect
      // Тайп кастинг для упрощения API
      ,
      onCheck: onTreeCheck,
      onExpand: onTreeExpand,
      switcherIcon: function switcherIcon(node) {
        if (node.isLeaf) {
          return null;
        }
        if (node.loading) {
          return _SpinnerStyled || (_SpinnerStyled = /*#__PURE__*/React__default.createElement(SpinnerStyled, null));
        }
        return /*#__PURE__*/React__default.createElement(IconArrowWrapper, null, /*#__PURE__*/React__default.createElement(StyledArrow, {
          size: sizeToIconSize(size, variant),
          color: "inherit",
          className: "arrow"
        }));
      },
      icon: hasIcon && (icon || /*#__PURE__*/React__default.createElement(StyledFolder, {
        size: sizeToIconSize(size, variant),
        color: "inherit"
      })),
      titleRender: function titleRender(item) {
        return /*#__PURE__*/React__default.createElement(TitleWrapper, {
          fullWidthItemSelection: fullWidthItemSelection,
          arrowPlacement: arrowPlacement,
          singleLine: singleLine
        }, /*#__PURE__*/React__default.createElement(Title, {
          singleLine: singleLine
        }, renderTitle ? renderTitle(item) : typeof item.title === 'function' ? item.title(item) : item.title), item.contentRight && /*#__PURE__*/React__default.createElement(ContentRight, null, item.contentRight));
      },
      draggable: draggable,
      onDragStart: onDragStart,
      onDragEnter: onDragEnter,
      onDragOver: onDragOver,
      onDragLeave: onDragLeave,
      onDragEnd: onDragEnd,
      onDrop: onDrop,
      allowDrop: allowDrop,
      dropIndicatorRender: function dropIndicatorRender(props) {
        var offset = 0;
        var dropPosition = props.dropPosition,
          dropLevelOffset = props.dropLevelOffset,
          indent = props.indent;
        var left = -dropLevelOffset * indent + offset;
        var style = {};
        if (dropPosition === 0) {
          style.left = indent + offset;
        }
        return /*#__PURE__*/React__default.createElement(Line, {
          style: style,
          left: left,
          dropPosition: dropPosition,
          variant: variant
        });
      }
    })));
  });
};
var treeConfig = {
  name: 'Tree',
  tag: 'div',
  layout: treeRoot,
  base: base,
  variations: {},
  defaults: {
    view: 'default',
    size: 'm'
  }
};

export { treeConfig, treeRoot };
