"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _components = require("@alicloudfe/components");
var _card = require("../../../card");
var _proField = _interopRequireDefault(require("@teamix/pro-field"));
var _utils = require("@teamix/utils");
var _react = _interopRequireWildcard(require("react"));
require("./index.scss");
var _icon = _interopRequireDefault(require("@teamix/icon"));
var _excluded = ["children", "searchPlaceholder", "showSearch", "searchProps", "customSearch", "searchOnChange", "message", "messageProps", "customMessage", "scrollArea", "showExpandAll", "showExpandLevel", "onExpandAllChange", "onExpandLevelChange", "expandLevel", "expandLevelState", "expandAllState", "isTree"];
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
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; }
var cls = (0, _utils.usePrefixCls)('teamix-pro-sidebar-container');
var ProSidebarContainer = function ProSidebarContainer(props) {
  var children = props.children,
    searchPlaceholder = props.searchPlaceholder,
    _props$showSearch = props.showSearch,
    showSearch = _props$showSearch === void 0 ? true : _props$showSearch,
    _props$searchProps = props.searchProps,
    searchProps = _props$searchProps === void 0 ? {} : _props$searchProps,
    customSearch = props.customSearch,
    searchOnChange = props.searchOnChange,
    message = props.message,
    _props$messageProps = props.messageProps,
    messageProps = _props$messageProps === void 0 ? {} : _props$messageProps,
    customMessage = props.customMessage,
    scrollArea = props.scrollArea,
    showExpandAll = props.showExpandAll,
    showExpandLevel = props.showExpandLevel,
    onExpandAllChange = props.onExpandAllChange,
    onExpandLevelChange = props.onExpandLevelChange,
    _props$expandLevel = props.expandLevel,
    expandLevel = _props$expandLevel === void 0 ? 1 : _props$expandLevel,
    _props$expandLevelSta = props.expandLevelState,
    expandLevelStateProp = _props$expandLevelSta === void 0 ? 1 : _props$expandLevelSta,
    _props$expandAllState = props.expandAllState,
    expandAllStateProp = _props$expandAllState === void 0 ? false : _props$expandAllState,
    isTree = props.isTree,
    others = _objectWithoutProperties(props, _excluded);
  var ref = (0, _react.useRef)();
  var _useState = (0, _react.useState)('auto'),
    _useState2 = _slicedToArray(_useState, 2),
    treeHeight = _useState2[0],
    setTreeHeight = _useState2[1];
  // 展开全部状态
  var _useState3 = (0, _react.useState)(false),
    _useState4 = _slicedToArray(_useState3, 2),
    expandAllState = _useState4[0],
    setExpandAllState = _useState4[1];
  // 展开层级状态
  var _useState5 = (0, _react.useState)(1),
    _useState6 = _slicedToArray(_useState5, 2),
    expandLevelState = _useState6[0],
    setExpandLevelState = _useState6[1];
  (0, _react.useEffect)(function () {
    // 设置滚动区域
    if (scrollArea === 'tree') {
      var _ref$current$offsetTo, _ref$current;
      var remainHeight = (_ref$current$offsetTo = ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.offsetTop) !== null && _ref$current$offsetTo !== void 0 ? _ref$current$offsetTo : 0 + 40;
      if (remainHeight > 0) {
        setTreeHeight("calc(100% - ".concat(remainHeight, "px + 16px)"));
      }
    }
  }, []);
  (0, _react.useEffect)(function () {
    setExpandLevelState(expandLevelStateProp);
    setTimeout(function () {
      onExpandLevelChange === null || onExpandLevelChange === void 0 ? void 0 : onExpandLevelChange(expandLevelStateProp);
    });
  }, [expandLevelStateProp]);
  (0, _react.useEffect)(function () {
    setExpandAllState(expandAllStateProp);
    setTimeout(function () {
      onExpandAllChange === null || onExpandAllChange === void 0 ? void 0 : onExpandAllChange(expandAllStateProp);
    });
  }, [expandAllStateProp]);
  // 渲染搜索区域
  var renderSearch = function renderSearch() {
    if (customSearch || showSearch) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: cls('search')
      }, !!customSearch && customSearch, showSearch && /*#__PURE__*/_react.default.createElement(_proField.default, _objectSpread({
        mode: "edit",
        type: "text",
        placeholder: searchPlaceholder,
        innerBeforeIcon: "search-line",
        onChange: searchOnChange
      }, searchProps)));
    }
    return null;
  };
  // 渲染通知区域
  var renderNotice = function renderNotice() {
    if (message || customMessage) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: cls('notice')
      }, message && /*#__PURE__*/_react.default.createElement(_components.Message, _objectSpread(_objectSpread({
        type: "notice",
        closeable: true
      }, messageProps), {}, {
        onClose: function onClose() {
          var _ref$current$offsetTo2, _ref$current2;
          var remainHeight = (_ref$current$offsetTo2 = ref === null || ref === void 0 ? void 0 : (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.offsetTop) !== null && _ref$current$offsetTo2 !== void 0 ? _ref$current$offsetTo2 : 0 + 40;
          setTreeHeight("calc(100% - ".concat(remainHeight, "px + 56px)"));
        }
      }), message), !!customMessage && customMessage);
    }
    return null;
  };
  // 渲染折叠区
  var renderFold = function renderFold() {
    if (showExpandAll || showExpandLevel) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: cls('fold')
      }, showExpandAll && /*#__PURE__*/_react.default.createElement(_components.Button, {
        type: "primary",
        text: true,
        onClick: function onClick() {
          var visible = !expandAllState;
          setExpandAllState(visible);
          onExpandAllChange === null || onExpandAllChange === void 0 ? void 0 : onExpandAllChange(visible);
        }
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: expandAllState ? 'menu-unfold-line' : 'menu-fold-line'
      }), expandAllState ? '全部收起' : '全部展开'), showExpandLevel && /*#__PURE__*/_react.default.createElement(_components.MenuButton, {
        text: true,
        type: "secondary",
        label: "\u5C55\u793A".concat(expandLevelState, "\u7EA7"),
        onItemClick: function onItemClick(key) {
          setExpandLevelState(key);
          onExpandLevelChange === null || onExpandLevelChange === void 0 ? void 0 : onExpandLevelChange(key);
        }
      }, _toConsumableArray(new Array(expandLevel)).fill(1).map(function (item, index) {
        return /*#__PURE__*/_react.default.createElement(_components.MenuButton.Item, {
          key: index + 1
        }, "\u5C55\u793A", index + 1, "\u7EA7");
      })));
    }
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: cls({
      '': true,
      'not-is-tree': !isTree
    })
  }, /*#__PURE__*/_react.default.createElement(_card.ProCard, _objectSpread({
    bordered: false,
    compacted: true
  }, others), renderSearch(), renderNotice(), renderFold(), /*#__PURE__*/_react.default.createElement("div", {
    className: cls('context'),
    ref: ref,
    style: {
      height: treeHeight
    }
  }, children)));
};
var _default = ProSidebarContainer;
exports.default = _default;