"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 _classnames = _interopRequireDefault(require("classnames"));

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

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

var _enum = require("../../../enum");

var _switch = _interopRequireDefault(require("../../../../switch"));

var _actionBtns = _interopRequireDefault(require("../../../action-btns"));

var _selectedPanel = _interopRequireDefault(require("../../../selected-panel"));

var _singleSelector = _interopRequireDefault(require("../single-selector"));

require("../../../index.css");

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 _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 _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 preCls = 'perspm-multiple-selector';

function _default(props) {
  var _props$onCancel = props.onCancel,
      onCancel = _props$onCancel === void 0 ? function () {} : _props$onCancel,
      _props$onOk = props.onOk,
      onOk = _props$onOk === void 0 ? function () {} : _props$onOk,
      _props$onStaffStatusC = props.onStaffStatusChange,
      onStaffStatusChange = _props$onStaffStatusC === void 0 ? function () {} : _props$onStaffStatusC,
      staffStatus = props.staffStatus,
      _props$autoClear = props.autoClear,
      autoClear = _props$autoClear === void 0 ? false : _props$autoClear;

  var _useContext = (0, _react.useContext)(_context.CommonContext),
      enableDimissionSwitch = _useContext.enableDimissionSwitch;

  var _useState = (0, _react.useState)(staffStatus === _enum.StaffStatus.All),
      _useState2 = _slicedToArray(_useState, 2),
      switchChecked = _useState2[0],
      setSwitchChecked = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      _useState4 = _slicedToArray(_useState3, 2),
      selected = _useState4[0],
      setSelected = _useState4[1]; // 外部可以修改选中的节点


  (0, _react.useEffect)(function () {
    setSelected(props.selected);
  }, [props.selected]);

  var getStaffStatus = function getStaffStatus(showDimissionPerson) {
    return enableDimissionSwitch && showDimissionPerson ? _enum.StaffStatus.All : _enum.StaffStatus.OnTheJob;
  };

  var onTreeNodeSelectHander = function onTreeNodeSelectHander(_, info) {
    var nodeIsSelected = !!(selected || []).filter(function (it) {
      return it.id == info.node.id;
    }).length;
    if (nodeIsSelected) return;
    delete info.node.children;
    info.node.isLeaf = true; // 选中的部门或群组不显示Open

    setSelected([].concat(_toConsumableArray(selected || []), [info.node]));
  };

  var onSwitchChange = function onSwitchChange(checked) {
    setSwitchChecked(checked);
    onStaffStatusChange(getStaffStatus(checked));
  };

  var onOkHandler = function onOkHandler(e) {
    onOk(selected, e); // autoClear 开启，提交后自动清除

    if (autoClear) setSelected([]);
  };

  var wrapperClickHandler = function wrapperClickHandler(e) {
    return e.stopPropagation();
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(preCls),
    onClick: wrapperClickHandler
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__box"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__box-inner"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__box-inner-left"))
  }, /*#__PURE__*/_react["default"].createElement(_singleSelector["default"], {
    onSelectPure: onTreeNodeSelectHander,
    showSearchInput: true,
    staffStatus: getStaffStatus(switchChecked)
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__box-inner-right"))
  }, /*#__PURE__*/_react["default"].createElement(_selectedPanel["default"], {
    selected: selected,
    setSelected: setSelected
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__box-footer"))
  }, enableDimissionSwitch && /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__box-footer__dimission"))
  }, /*#__PURE__*/_react["default"].createElement(_switch["default"], {
    checked: switchChecked,
    id: "".concat(preCls, "__dimission-switch"),
    onChange: onSwitchChange
  }), /*#__PURE__*/_react["default"].createElement("label", {
    htmlFor: "".concat(preCls, "__dimission-switch")
  }, (0, _getLocale.getLocale)('Per_Ass_New_but_disdis'))), /*#__PURE__*/_react["default"].createElement(_actionBtns["default"], {
    onCancel: onCancel,
    onOk: onOkHandler
  }))));
}