var _excluded = ["type", "action", "props"];

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import { Tree } from '@alicloudfe/components';
import { ProActionGroup } from '../../../actions';
import { usePrefixCls } from '@teamix/utils';
import React, { isValidElement, useState } from 'react';
import ProField, { renderTags } from '@teamix/pro-field';
import { getTreeNodeProps } from '../..';
import ProIconAction from './components/IconAction';
import ProIconSwitch from './components/IconSwitch';
import ProHoverTooltip from './components/HoverTooltip';
var cls = usePrefixCls('teamix-pro-sidebar-tree');

var ProSideBarTreeNode = function ProSideBarTreeNode(props) {
  var tag = props.tag,
      render = props.render,
      _props$valueType = props.valueType,
      valueType = _props$valueType === void 0 ? 'text' : _props$valueType,
      fieldProps = props.fieldProps,
      extra = props.extra,
      label = props.label;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      extraVisible = _useState2[0],
      setExtraVisible = _useState2[1]; // 获取 extra 区域渲染组件


  var getExtraComponent = function getExtraComponent(type, action, props, others) {
    var _ref = others !== null && others !== void 0 ? others : {},
        _ref$hover = _ref.hover,
        hover = _ref$hover === void 0 ? true : _ref$hover;

    if (type === 'action') {
      return /*#__PURE__*/React.createElement(ProHoverTooltip, {
        visible: !hover || extraVisible,
        trigger: /*#__PURE__*/React.createElement(ProActionGroup, _objectSpread({
          type: "text",
          actions: action
        }, props))
      });
    }

    if (type === 'iconSwitch') {
      return /*#__PURE__*/React.createElement(ProIconSwitch, _objectSpread({
        tooltipVisible: !hover || extraVisible
      }, props));
    }

    if (type === 'iconAction') {
      return /*#__PURE__*/React.createElement(ProIconAction, _objectSpread({
        visible: !hover || extraVisible,
        action: action
      }, props));
    }

    if (type === 'custom') {
      return /*#__PURE__*/React.createElement(ProHoverTooltip, {
        className: cls('extra-custom'),
        visible: !hover || extraVisible,
        trigger: others.trigger
      });
    }
  }; // 渲染 extra 区域


  var renderExtra = function renderExtra() {
    var result = /*#__PURE__*/React.createElement(React.Fragment, null); // extra 是否是 action 配置

    var isActionGroupProps = Array.isArray(extra) && ! /*#__PURE__*/isValidElement(extra === null || extra === void 0 ? void 0 : extra[0]) && Array.isArray(extra === null || extra === void 0 ? void 0 : extra[0]); // 直接渲染 action

    if (isActionGroupProps) {
      result = /*#__PURE__*/React.createElement(ProActionGroup, {
        actions: extra,
        type: "text"
      });
    } // 大杂烩


    if (extra && Array.isArray(extra)) {
      result = /*#__PURE__*/React.createElement(React.Fragment, null, extra.map(function (item, index) {
        if ( /*#__PURE__*/isValidElement(item) || typeof item === 'string') {
          return /*#__PURE__*/React.createElement("span", {
            key: index,
            className: cls('item-content-extra-item')
          }, item);
        }

        var type = item.type,
            _item$action = item.action,
            action = _item$action === void 0 ? {} : _item$action,
            _item$props = item.props,
            props = _item$props === void 0 ? {} : _item$props,
            others = _objectWithoutProperties(item, _excluded);

        return /*#__PURE__*/React.createElement("span", {
          key: index,
          className: cls('item-content-extra-item')
        }, getExtraComponent(type, action, props, others));
      }));
    }

    return result;
  }; // 鼠标移入


  var onMouseEnter = function onMouseEnter(e) {
    var _e$target, _e$target$closest;

    if (e === null || e === void 0 ? void 0 : (_e$target = e.target) === null || _e$target === void 0 ? void 0 : (_e$target$closest = _e$target.closest) === null || _e$target$closest === void 0 ? void 0 : _e$target$closest.call(_e$target, '.teamix-pro-sidebar-delete-overlay')) {
      return;
    }

    setExtraVisible(true);
  }; // 鼠标移出


  var onMouseLeave = function onMouseLeave(e) {
    var _e$target2, _e$target2$closest;

    if (e === null || e === void 0 ? void 0 : (_e$target2 = e.target) === null || _e$target2 === void 0 ? void 0 : (_e$target2$closest = _e$target2.closest) === null || _e$target2$closest === void 0 ? void 0 : _e$target2$closest.call(_e$target2, '.teamix-pro-sidebar-delete-overlay')) {
      return;
    }

    setExtraVisible(false);
  }; // 获取 ProFieldRender


  var getFieldRender = function getFieldRender() {
    if (typeof render === 'function') {
      return render;
    }

    return _objectSpread({
      ellipsis: true,
      emptyText: '-'
    }, render);
  }; // 获取 tag 参数


  var getTags = function getTags(tags) {
    if ( /*#__PURE__*/isValidElement(tags)) {
      return tags;
    }

    if (Array.isArray(tags)) {
      return tags.map(function (item) {
        return _objectSpread({
          size: 'small'
        }, item);
      });
    }

    if (_typeof(tags) === 'object') {
      return _objectSpread({
        size: 'small'
      }, tags);
    }
  };

  return /*#__PURE__*/React.createElement("div", {
    className: cls('item-context'),
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave
  }, /*#__PURE__*/React.createElement("div", {
    className: cls('item-context-front')
  }, /*#__PURE__*/React.createElement(ProField, _objectSpread({
    value: label,
    type: valueType,
    render: getFieldRender()
  }, fieldProps)), tag && /*#__PURE__*/React.createElement("span", {
    className: cls('item-context-tag')
  }, renderTags(getTags(tag)))), /*#__PURE__*/React.createElement("div", {
    className: cls('item-content-extra')
  }, renderExtra()));
}; // 渲染树节点


export function renderTreeNode(data) {
  return data.map(function (item) {
    var _getTreeNodeProps = getTreeNodeProps(item),
        nodeProps = _getTreeNodeProps.nodeProps,
        labelProps = _getTreeNodeProps.labelProps;

    return /*#__PURE__*/React.createElement(Tree.Node, _objectSpread({
      key: item.value,
      label: /*#__PURE__*/React.createElement(ProSideBarTreeNode, _objectSpread({}, labelProps))
    }, nodeProps), item.children && item.children.length > 0 && renderTreeNode(item.children));
  });
} // 渲染树节点 依赖 value

export function renderTreeNodeDependenceValue(data, onBeforeRenderNodeEvent, checkedKeys, selectedKeys, allData) {
  return data.map(function (item) {
    var newProps = item;

    if (onBeforeRenderNodeEvent) {
      newProps = onBeforeRenderNodeEvent(item, checkedKeys, selectedKeys, allData !== null && allData !== void 0 ? allData : data);
    }

    var _getTreeNodeProps2 = getTreeNodeProps(newProps),
        nodeProps = _getTreeNodeProps2.nodeProps,
        labelProps = _getTreeNodeProps2.labelProps;

    return /*#__PURE__*/React.createElement(Tree.Node, _objectSpread({
      key: item.value,
      label: /*#__PURE__*/React.createElement(ProSideBarTreeNode, _objectSpread({}, labelProps))
    }, nodeProps), item.children && item.children.length > 0 && renderTreeNodeDependenceValue(item.children, onBeforeRenderNodeEvent, checkedKeys, selectedKeys, allData !== null && allData !== void 0 ? allData : data));
  });
}
export default ProSideBarTreeNode;