"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 _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

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

var _openData = require("../../../../utils/open-data");

var _multipleSelector = _interopRequireDefault(require("../field-tree-selector/multiple-selector"));

var _popup = _interopRequireDefault(require("../../../popup"));

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

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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 _default = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var selector = props.selector,
      _props$input = props.input,
      input = _props$input === void 0 ? {} : _props$input,
      trigger = props.trigger,
      children = props.children,
      _props$onOk = props.onOk,
      onOk = _props$onOk === void 0 ? function () {} : _props$onOk,
      _props$position = props.position,
      position = _props$position === void 0 ? "bottomRight" : _props$position,
      _props$selectNodes = props.selectNodes,
      selectNodes = _props$selectNodes === void 0 ? {} : _props$selectNodes,
      sourceFrom = props.sourceFrom,
      _props$auth = props.auth,
      auth = _props$auth === void 0 ? false : _props$auth,
      _props$placeholder = props.placeholder,
      placeholder = _props$placeholder === void 0 ? (0, _getLocale.getLocale)("Mod_Pleaseselect") : _props$placeholder,
      _props$destroyPopupOn = props.destroyPopupOnHide,
      destroyPopupOnHide = _props$destroyPopupOn === void 0 ? true : _props$destroyPopupOn,
      _props$forceRender = props.forceRender,
      forceRender = _props$forceRender === void 0 ? false : _props$forceRender,
      _props$isGetRoot = props.isGetRoot,
      isGetRoot = _props$isGetRoot === void 0 ? false : _props$isGetRoot,
      _props$popupAlign = props.popupAlign,
      popupAlign = _props$popupAlign === void 0 ? {
    offset: [0, 0],
    overflow: {
      adjustX: true,
      adjustY: true
    },
    targetOffset: [0, 0]
  } : _props$popupAlign;

  var _useState = (0, _react.useState)([]),
      _useState2 = _slicedToArray(_useState, 2),
      selected = _useState2[0],
      setSelected = _useState2[1];

  var _useState3 = (0, _react.useState)(placeholder),
      _useState4 = _slicedToArray(_useState3, 2),
      selectedText = _useState4[0],
      setSelectedText = _useState4[1];

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

  var _useState7 = (0, _react.useState)(false),
      _useState8 = _slicedToArray(_useState7, 2),
      visible = _useState8[0],
      setVisible = _useState8[1];

  var setSelectedByProps = function setSelectedByProps() {
    var selected = [];
    var texts = [];
    var jsxDomValues = [];
    var _selectNodes$groups = selectNodes.groups,
        groups = _selectNodes$groups === void 0 ? [] : _selectNodes$groups,
        _selectNodes$departme = selectNodes.departments,
        departments = _selectNodes$departme === void 0 ? [] : _selectNodes$departme,
        _selectNodes$users = selectNodes.users,
        users = _selectNodes$users === void 0 ? [] : _selectNodes$users;
    (groups || []).forEach(function (data) {
      selected.push({
        nodeType: "group",
        id: data.id,
        data: data
      });
      texts.push(data.name);
      jsxDomValues.push( /*#__PURE__*/_react["default"].createElement("span", null, data.name));
    });
    (departments || []).forEach(function (data) {
      selected.push({
        nodeType: "depart",
        id: data.id,
        data: data
      });
      texts.push(data.name);
      jsxDomValues.push( /*#__PURE__*/_react["default"].createElement(_openData.OpenDepartmentName, {
        id: data.id,
        name: data.name
      }));
    });
    (users || []).forEach(function (data) {
      selected.push({
        nodeType: "person",
        id: data.userId,
        data: data
      });
      texts.push(data.name);
      jsxDomValues.push( /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
        id: data.userId,
        name: data.name
      }));
    });
    setSelected(selected);
    var text = texts.join("、");
    if (!text) text = placeholder;
    setSelectedText(text);
    setJsxDomValues(jsxDomValues);
  };

  (0, _react.useEffect)(function () {
    setSelectedByProps();
  }, [selectNodes]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      open: function open() {
        return setVisible(true);
      },
      close: function close() {
        return setVisible(false);
      }
    };
  });

  var onOkHandler = function onOkHandler(selectNodes) {
    setVisible(false);
    setSelected(selectNodes);
    var texts = [];
    var users = []; // 返回react组件形式的value，应用于企业微信

    var jsxDomValues = [];
    selectNodes.forEach(function (item) {
      switch (item.nodeType) {
        case "person":
          users.push(item.data);
          texts.push(item.data.name);
          jsxDomValues.push( /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
            id: item.data.userId,
            name: item.data.name
          }));
          break;

        default:
          break;
      }
    });
    var wechartOpts = {
      isWechat: (0, _openData.isWechat)(),
      jsxDomValues: jsxDomValues
    };
    onOk({
      users: users
    }, wechartOpts);
    var text = texts.join("、");
    if (!text) text = placeholder;
    setSelectedText(text);
    setJsxDomValues(jsxDomValues);
  };

  var onCancelHandler = function onCancelHandler() {
    setVisible(false);
  };

  return /*#__PURE__*/_react["default"].createElement(_context.CommonContext.Provider, {
    value: {
      sourceFrom: sourceFrom,
      auth: auth,
      isGetRoot: isGetRoot
    }
  }, /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_popup["default"], {
    popupVisible: visible,
    popupPlacement: position,
    onPopupVisibleChange: function onPopupVisibleChange(visible) {
      setVisible(visible);
    },
    forceRender: forceRender,
    destroyPopupOnHide: destroyPopupOnHide,
    popup: /*#__PURE__*/_react["default"].createElement(_multipleSelector["default"], _extends({}, selector, {
      onCancel: onCancelHandler,
      onOk: onOkHandler,
      selected: selected,
      disabledDepartSelect: true,
      panes: {
        depart: true,
        group: false
      }
    })),
    popupAlign: popupAlign
  }, children || trigger ? /*#__PURE__*/_react["default"].createElement("span", {
    onClick: function onClick() {
      return setVisible(true);
    }
  }, children || trigger) : /*#__PURE__*/_react["default"].createElement("span", {
    className: "persel-selector-field",
    style: input.style,
    onClick: function onClick() {
      return setVisible(!visible);
    }
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "persel-selector-field__text"
  }, (0, _openData.isWechat)() ? jsxDomValues : selectedText), /*#__PURE__*/_react["default"].createElement("span", {
    className: "persel-selector-field__suffix"
  })))));
});

exports["default"] = _default;