"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.renderTreeNode = renderTreeNode;
exports.renderTreeNodeDependenceValue = renderTreeNodeDependenceValue;
var _components = require("@alicloudfe/components");
var _actions = require("../../../actions");
var _utils = require("@teamix/utils");
var _react = _interopRequireWildcard(require("react"));
var _proField = _interopRequireWildcard(require("@teamix/pro-field"));
var _ = require("../..");
var _IconAction = _interopRequireDefault(require("./components/IconAction"));
var _IconSwitch = _interopRequireDefault(require("./components/IconSwitch"));
var _HoverTooltip = _interopRequireDefault(require("./components/HoverTooltip"));
var _excluded = ["type", "action", "props"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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; }
var cls = (0, _utils.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 = (0, _react.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.default.createElement(_HoverTooltip.default, {
        visible: !hover || extraVisible,
        trigger: /*#__PURE__*/_react.default.createElement(_actions.ProActionGroup, _objectSpread({
          type: "text",
          actions: action
        }, props))
      });
    }
    if (type === 'iconSwitch') {
      return /*#__PURE__*/_react.default.createElement(_IconSwitch.default, _objectSpread({
        tooltipVisible: !hover || extraVisible
      }, props));
    }
    if (type === 'iconAction') {
      return /*#__PURE__*/_react.default.createElement(_IconAction.default, _objectSpread({
        visible: !hover || extraVisible,
        action: action
      }, props));
    }
    if (type === 'custom') {
      return /*#__PURE__*/_react.default.createElement(_HoverTooltip.default, {
        className: cls('extra-custom'),
        visible: !hover || extraVisible,
        trigger: others.trigger
      });
    }
  };
  // 渲染 extra 区域
  var renderExtra = function renderExtra() {
    var result = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
    // extra 是否是 action 配置
    var isActionGroupProps = Array.isArray(extra) && ! /*#__PURE__*/(0, _react.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.default.createElement(_actions.ProActionGroup, {
        actions: extra,
        type: "text"
      });
    }
    // 大杂烩
    if (extra && Array.isArray(extra)) {
      result = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, extra.map(function (item, index) {
        if ( /*#__PURE__*/(0, _react.isValidElement)(item) || typeof item === 'string') {
          return /*#__PURE__*/_react.default.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.default.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__*/(0, _react.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.default.createElement("div", {
    className: cls('item-context'),
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: cls('item-context-front')
  }, /*#__PURE__*/_react.default.createElement(_proField.default, _objectSpread({
    value: label,
    type: valueType,
    render: getFieldRender()
  }, fieldProps)), tag && /*#__PURE__*/_react.default.createElement("span", {
    className: cls('item-context-tag')
  }, (0, _proField.renderTags)(getTags(tag)))), /*#__PURE__*/_react.default.createElement("div", {
    className: cls('item-content-extra')
  }, renderExtra()));
};
// 渲染树节点
function renderTreeNode(data) {
  return data.map(function (item) {
    var _getTreeNodeProps = (0, _.getTreeNodeProps)(item),
      nodeProps = _getTreeNodeProps.nodeProps,
      labelProps = _getTreeNodeProps.labelProps;
    return /*#__PURE__*/_react.default.createElement(_components.Tree.Node, _objectSpread({
      key: item.value,
      label: /*#__PURE__*/_react.default.createElement(ProSideBarTreeNode, _objectSpread({}, labelProps))
    }, nodeProps), (item === null || item === void 0 ? void 0 : item.children) && item.children.length > 0 && renderTreeNode(item.children));
  });
}
// 渲染树节点 依赖 value
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 = (0, _.getTreeNodeProps)(newProps),
      nodeProps = _getTreeNodeProps2.nodeProps,
      labelProps = _getTreeNodeProps2.labelProps;
    return /*#__PURE__*/_react.default.createElement(_components.Tree.Node, _objectSpread({
      key: item.value,
      label: /*#__PURE__*/_react.default.createElement(ProSideBarTreeNode, _objectSpread({}, labelProps))
    }, nodeProps), item.children && item.children.length > 0 && renderTreeNodeDependenceValue(item.children, onBeforeRenderNodeEvent, checkedKeys, selectedKeys, allData !== null && allData !== void 0 ? allData : data));
  });
}
var _default = ProSideBarTreeNode;
exports.default = _default;