'use strict';

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

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var Tree = require('rc-tree');
var cls = require('classnames');
var Tree_styles = require('./Tree.styles.js');
var Tree_tokens = require('./Tree.tokens.js');
var traverseTree = require('./utils/traverseTree.js');
var sizeToIconSize = require('./utils/sizeToIconSize.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);
var Tree__default = /*#__PURE__*/_interopDefault(Tree);
var cls__default = /*#__PURE__*/_interopDefault(cls);

var _SpinnerStyled;

/**
 * Многоуровневый раскрывающийся список в виде дерева.
 */
var treeRoot = function treeRoot(Root) {
  return /*#__PURE__*/React.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 = React.useState(defaultSelectedKeys || []),
      _useState2 = _rollupPluginBabelHelpers.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' ? Tree_tokens.classes.treeInverted : undefined;
    var itemFilledClass = fullWidthItemSelection ? Tree_tokens.classes.treeItemFilled : undefined;
    var radioModeClass = isRadioMode ? Tree_tokens.classes.treeRadioMode : undefined;
    var tightVariantClass = variant === 'tight' ? Tree_tokens.classes.treeTightVariant : undefined;

    // Проходимся по дереву и устанавливаем соответствующие классы для узлов дерева,
    // чтобы соблюсти иерархию вложенности в UI.
    var treeData = React.useMemo(function () {
      return traverseTree.traverseTree(items, selectedKeys);
    }, [items, selectedKeys]);
    return /*#__PURE__*/React__default.default.createElement(Root, {
      view: view,
      size: size,
      variant: variant,
      ref: ref,
      items: items
    }, /*#__PURE__*/React__default.default.createElement(Tree__default.default, _rollupPluginBabelHelpers.extends({
      loadData: loadData,
      onLoad: onLoadData,
      height: height,
      itemHeight: itemHeight,
      virtual: virtual,
      multiple: multiple,
      checkable: checkable,
      className: cls__default.default(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.default.createElement(Tree_styles.SpinnerStyled, null));
        }
        return /*#__PURE__*/React__default.default.createElement(Tree_styles.IconArrowWrapper, null, /*#__PURE__*/React__default.default.createElement(Tree_styles.StyledArrow, {
          size: sizeToIconSize.sizeToIconSize(size, variant),
          color: "inherit",
          className: "arrow"
        }));
      },
      icon: hasIcon && (icon || /*#__PURE__*/React__default.default.createElement(Tree_styles.StyledFolder, {
        size: sizeToIconSize.sizeToIconSize(size, variant),
        color: "inherit"
      })),
      titleRender: function titleRender(item) {
        return /*#__PURE__*/React__default.default.createElement(Tree_styles.TitleWrapper, {
          fullWidthItemSelection: fullWidthItemSelection,
          arrowPlacement: arrowPlacement,
          singleLine: singleLine
        }, /*#__PURE__*/React__default.default.createElement(Tree_styles.Title, {
          singleLine: singleLine
        }, renderTitle ? renderTitle(item) : typeof item.title === 'function' ? item.title(item) : item.title), item.contentRight && /*#__PURE__*/React__default.default.createElement(Tree_styles.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.default.createElement(Tree_styles.Line, {
          style: style,
          left: left,
          dropPosition: dropPosition,
          variant: variant
        });
      }
    })));
  });
};
var treeConfig = {
  name: 'Tree',
  tag: 'div',
  layout: treeRoot,
  base: Tree_styles.base,
  variations: {},
  defaults: {
    view: 'default',
    size: 'm'
  }
};

exports.treeConfig = treeConfig;
exports.treeRoot = treeRoot;
