"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
});
var _exportNames = {
  ProIconSwitch: true
};
Object.defineProperty(exports, "ProIconSwitch", {
  enumerable: true,
  get: function get() {
    return _IconSwitch.default;
  }
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
require("./index.scss");
var _utils = require("@teamix/utils");
var _sidebarContainer = _interopRequireDefault(require("./components/sidebar-container"));
var _tree = _interopRequireDefault(require("./components/tree"));
var _ = require("..");
var _actionRef = require("./utils/action-ref");
var _lodash = _interopRequireDefault(require("lodash.debounce"));
var _typing = require("./typing");
Object.keys(_typing).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _typing[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _typing[key];
    }
  });
});
var _utils2 = require("./utils");
Object.keys(_utils2).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _utils2[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _utils2[key];
    }
  });
});
var _IconSwitch = _interopRequireDefault(require("./components/tree-node/components/IconSwitch"));
var _excluded = ["selectedKeys", "checkedKeys", "defaultSelectedKeys", "defaultCheckedKeys", "dataSource", "className", "style", "searchOnChange", "showSearch", "onSelect", "onCheck", "treeProps", "cardProps", "actionRef", "onBeforeRenderNodeEvent", "beforeRenderNode", "url", "method", "params", "data", "extendParams", "beforeRequest", "successMsg", "errorMsg", "formatParams", "formatResult", "onSuccess", "onError", "link", "scrollArea", "onExpandAllChange", "onExpandLevelChange"];
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 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');
var ProSidebar = function ProSidebar(props) {
  var selectedKeys = props.selectedKeys,
    checkedKeys = props.checkedKeys,
    defaultSelectedKeys = props.defaultSelectedKeys,
    defaultCheckedKeys = props.defaultCheckedKeys,
    dataSourceProp = props.dataSource,
    _props$className = props.className,
    className = _props$className === void 0 ? '' : _props$className,
    style = props.style,
    searchOnChange = props.searchOnChange,
    _props$showSearch = props.showSearch,
    showSearch = _props$showSearch === void 0 ? true : _props$showSearch,
    onSelect = props.onSelect,
    onCheck = props.onCheck,
    treeProps = props.treeProps,
    cardProps = props.cardProps,
    actionRefProp = props.actionRef,
    onBeforeRenderNodeEvent = props.onBeforeRenderNodeEvent,
    beforeRenderNode = props.beforeRenderNode,
    _props$url = props.url,
    url = _props$url === void 0 ? '' : _props$url,
    _props$method = props.method,
    method = _props$method === void 0 ? 'post' : _props$method,
    params = props.params,
    data = props.data,
    extendParams = props.extendParams,
    beforeRequest = props.beforeRequest,
    successMsg = props.successMsg,
    errorMsg = props.errorMsg,
    formatParams = props.formatParams,
    formatResult = props.formatResult,
    onSuccess = props.onSuccess,
    onError = props.onError,
    link = props.link,
    _props$scrollArea = props.scrollArea,
    scrollArea = _props$scrollArea === void 0 ? 'all' : _props$scrollArea,
    onExpandAllChangeProp = props.onExpandAllChange,
    onExpandLevelChangeProp = props.onExpandLevelChange,
    others = _objectWithoutProperties(props, _excluded);
  // 搜索的关键词
  var _useState = (0, _react.useState)(),
    _useState2 = _slicedToArray(_useState, 2),
    searchKey = _useState2[0],
    setSearchKey = _useState2[1];
  // loading 状态
  var _useState3 = (0, _react.useState)(true),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  // 数据源
  var _useState5 = (0, _react.useState)(dataSourceProp !== null && dataSourceProp !== void 0 ? dataSourceProp : []),
    _useState6 = _slicedToArray(_useState5, 2),
    dataSource = _useState6[0],
    setDataSource = _useState6[1];
  // 展开全部状态
  var _useState7 = (0, _react.useState)(false),
    _useState8 = _slicedToArray(_useState7, 2),
    expandAllState = _useState8[0],
    setExpandAllState = _useState8[1];
  // 展开层级状态
  var _useState9 = (0, _react.useState)(0),
    _useState10 = _slicedToArray(_useState9, 2),
    expandLevelState = _useState10[0],
    setExpandLevelState = _useState10[1];
  // 树层级
  var _useState11 = (0, _react.useState)(0),
    _useState12 = _slicedToArray(_useState11, 2),
    expandLevel = _useState12[0],
    setExpandLevel = _useState12[1];
  var actionRef = (0, _react.useRef)();
  // const requestDataKey = method.toLowerCase() === 'get' ? 'params' : 'data';
  var requestConfig = {
    url: url,
    method: method,
    params: params,
    data: data,
    extendParams: extendParams,
    beforeRequest: beforeRequest,
    successMsg: successMsg,
    errorMsg: errorMsg,
    formatParams: formatParams,
    formatResult: formatResult,
    onSuccess: onSuccess,
    onError: onError,
    link: link
  };
  (0, _react.useEffect)(function () {
    if (url) {
      request();
    } else {
      setLoading(false);
    }
  }, []);
  (0, _react.useEffect)(function () {
    setDataSource(dataSourceProp !== null && dataSourceProp !== void 0 ? dataSourceProp : []);
  }, [dataSourceProp]);
  // 请求数据
  var request = function request() {
    setLoading(true);
    (0, _utils.doCommonRequest)(requestConfig).then(function (resp) {
      setDataSource(resp);
    }).finally(function () {
      setLoading(false);
    });
  };
  (0, _actionRef.useActionType)(actionRefProp !== null && actionRefProp !== void 0 ? actionRefProp : actionRef, {
    refresh: function refresh() {
      request();
    }
  });
  // 展开全部变化回调
  var onExpandAllChange = function onExpandAllChange(status) {
    setExpandAllState(status);
    onExpandAllChangeProp === null || onExpandAllChangeProp === void 0 ? void 0 : onExpandAllChangeProp(status);
  };
  // 展开层级回调
  var onExpandLevelChange = function onExpandLevelChange(level) {
    setExpandLevelState(level);
    onExpandLevelChangeProp === null || onExpandLevelChangeProp === void 0 ? void 0 : onExpandLevelChangeProp(level);
  };
  // DataSource 变化回调。用于更新层级
  var onDataSourceChange = function onDataSourceChange(dataSource) {
    var level = (0, _.getTreeLevel)(dataSource);
    setExpandLevel(level);
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(cls(_defineProperty({
      '': true,
      'scroll-tree': scrollArea === 'tree'
    }, "deep-".concat(expandLevel), true)), " ").concat(className),
    style: style
  }, /*#__PURE__*/_react.default.createElement(_sidebarContainer.default, _objectSpread(_objectSpread({
    searchOnChange: (0, _lodash.default)(function (value) {
      if (showSearch) {
        searchOnChange === null || searchOnChange === void 0 ? void 0 : searchOnChange(value);
        setSearchKey(value);
      }
    }, 300),
    onExpandAllChange: onExpandAllChange,
    onExpandLevelChange: onExpandLevelChange,
    showSearch: showSearch,
    scrollArea: scrollArea,
    expandLevel: expandLevel,
    isTree: (0, _.getTreeLevel)(dataSource) > 1 ? true : false
  }, others), cardProps), loading && /*#__PURE__*/_react.default.createElement(_.ProSkeletonRaw.SideBar, null), !loading && /*#__PURE__*/_react.default.createElement(_tree.default, _objectSpread({
    dataSource: dataSource,
    searchKey: searchKey,
    onSelect: onSelect,
    onCheck: onCheck,
    selectedKeys: selectedKeys,
    checkedKeys: checkedKeys,
    actionRef: actionRefProp !== null && actionRefProp !== void 0 ? actionRefProp : actionRef,
    onBeforeRenderNodeEvent: onBeforeRenderNodeEvent,
    beforeRenderNode: beforeRenderNode,
    expandAll: expandAllState,
    expandLevel: expandLevelState,
    onDataSourceChange: onDataSourceChange,
    defaultSelectedKeys: defaultSelectedKeys,
    defaultCheckedKeys: defaultCheckedKeys
  }, treeProps))));
};
var _default = ProSidebar;
exports.default = _default;