"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"] = _default;

var _react = _interopRequireWildcard(require("react"));

var _utils = require("../utils");

var _loading = _interopRequireDefault(require("../loading"));

var _requestApis = require("../request-apis");

var _resultPart = _interopRequireDefault(require("./result-part"));

var _empty = _interopRequireDefault(require("../empty"));

var _group = _interopRequireDefault(require("../empty/group"));

var _hooks = require("../../../utils/hooks");

var _context = require("../context");

var _getLocale = require("../../../utils/getLocale");

require("./index.css");

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 _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 _default(props) {
  var searchKey = props.searchKey,
      onNodeSelect = props.onNodeSelect,
      selected = props.selected,
      _props$staffStatus = props.staffStatus,
      staffStatus = _props$staffStatus === void 0 ? 1 : _props$staffStatus;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      loading = _useState2[0],
      setLoading = _useState2[1];

  var _useContext = (0, _react.useContext)(_context.CommonContext),
      departOnly = _useContext.departOnly,
      searchUserOnly = _useContext.searchUserOnly,
      sourceFrom = _useContext.sourceFrom,
      auth = _useContext.auth,
      isVerify = _useContext.isVerify,
      _useContext$panes = _useContext.panes,
      panes = _useContext$panes === void 0 ? {
    depart: true,
    group: true
  } : _useContext$panes,
      _useContext$searchedP = _useContext.searchedPanes,
      searchedPanes = _useContext$searchedP === void 0 ? {
    user: true,
    depart: true,
    group: true
  } : _useContext$searchedP;

  var _useState3 = (0, _react.useState)([]),
      _useState4 = _slicedToArray(_useState3, 2),
      person = _useState4[0],
      setPerson = _useState4[1];

  var _useState5 = (0, _react.useState)([]),
      _useState6 = _slicedToArray(_useState5, 2),
      departs = _useState6[0],
      setDeparts = _useState6[1];

  var _useState7 = (0, _react.useState)([]),
      _useState8 = _slicedToArray(_useState7, 2),
      group = _useState8[0],
      setGroup = _useState8[1];

  var _useState9 = (0, _react.useState)(false),
      _useState10 = _slicedToArray(_useState9, 2),
      noAuth = _useState10[0],
      setNoAuth = _useState10[1];

  var searchUser = function searchUser() {
    setLoading(true);
    (0, _requestApis.search)({
      searchKey: searchKey,
      departOnly: departOnly,
      searchUserOnly: searchUserOnly,
      auth: auth,
      panes: panes,
      isVerify: isVerify,
      staffStatus: staffStatus
    }).then(function (res) {
      var Code = res.Code,
          Data = res.Data;
      var departments = Data.departments,
          groups = Data.groups,
          users = Data.users;

      if (Code === 401) {
        setNoAuth(true);
      } else {
        setNoAuth(false);
      }

      setPerson((0, _utils.translateUsersToTreeNode)(users || []));
      setGroup((0, _utils.translateGroupsToTreeNode)(groups || []));
      setDeparts((0, _utils.translateDepartsToTreeNode)(departments || []));
      setLoading(false);
    });
  };

  (0, _hooks.useDebouncedEffect)(function () {
    searchUser();
  }, 200, [searchKey, staffStatus]); // 计算结果，是否显示空值

  var itemCount = 0;

  if (searchedPanes.user && person) {
    itemCount += person.length;
  }

  if (searchedPanes.depart && departs) {
    itemCount += departs.length;
  }

  if (searchedPanes.group && group) {
    itemCount += group.length;
  }

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "persel-searched"
  }, loading && /*#__PURE__*/_react["default"].createElement(_loading["default"], null), !loading && /*#__PURE__*/_react["default"].createElement("div", null, searchedPanes.user && person && person.length ? /*#__PURE__*/_react["default"].createElement(_resultPart["default"], {
    title: (0, _getLocale.getLocale)('Per_Ma_form_detail_Personnel'),
    treeData: person,
    onSelect: onNodeSelect,
    selected: selected
  }) : undefined, searchedPanes.depart && departs && departs.length ? /*#__PURE__*/_react["default"].createElement(_resultPart["default"], {
    title: (0, _getLocale.getLocale)('Mod_Department'),
    treeData: departs,
    onSelect: onNodeSelect,
    selected: selected
  }) : undefined, searchedPanes.group && group && group.length ? /*#__PURE__*/_react["default"].createElement(_resultPart["default"], {
    title: (0, _getLocale.getLocale)('Mod_Groups'),
    treeData: group,
    onSelect: onNodeSelect,
    selected: selected
  }) : undefined, itemCount == 0 && !noAuth && /*#__PURE__*/_react["default"].createElement(_empty["default"], null), itemCount == 0 && noAuth && /*#__PURE__*/_react["default"].createElement(_empty["default"], {
    errorTip: (0, _getLocale.getLocale)('Mod_Nopermissiondep')
  })));
}