"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 _userSelector = _interopRequireDefault(require("../../../user-selector"));

var _tooltip = _interopRequireDefault(require("../../../tooltip"));

var _departmentInput = _interopRequireDefault(require("./department-input"));

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 _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; }

var preCls = "form-field-department";

function OverlayInner(_ref) {
  var hasError = _ref.hasError,
      hasActive = _ref.hasActive,
      tip = _ref.tip;
  if (hasActive) return /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      color: "#3F4755"
    }
  }, tip);
  if (hasError) return /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      color: "#F05E5E"
    }
  }, tip);
  return /*#__PURE__*/_react["default"].createElement("span", null);
}

var _default = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _classNames;

  var label = props.label,
      placeholder = props.placeholder,
      value = props.value,
      onChange = props.onChange,
      require = props.require,
      name = props.name,
      getContainer = props.getContainer;
  var departRef = (0, _react.useRef)();

  var _useState = (0, _react.useState)(""),
      _useState2 = _slicedToArray(_useState, 2),
      tip = _useState2[0],
      setTip = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      hasError = _useState4[0],
      setHasError = _useState4[1];

  var _useState5 = (0, _react.useState)(false),
      _useState6 = _slicedToArray(_useState5, 2),
      hasActive = _useState6[0],
      setHasActive = _useState6[1];

  var _useState7 = (0, _react.useState)(value),
      _useState8 = _slicedToArray(_useState7, 2),
      selectedDepart = _useState8[0],
      setSelectedDepart = _useState8[1];

  (0, _react.useEffect)(function () {
    setSelectedDepart(value);
  }, [value]);

  var onDepartSelect = function onDepartSelect(keys, data) {
    setHasActive(false);
    setHasError(false);
    setTip("");
    var departInfo = data.node.data;
    var selectedDepart = {
      departmentId: departInfo.id,
      departmentName: departInfo.name
    };
    setSelectedDepart(selectedDepart); // @ts-ignore

    departRef.current && departRef.current.close();
    onChange(selectedDepart);
  };

  var verifing = function verifing() {
    if (require && !selectedDepart) {
      setHasError(true);
      setTip("部门不可为空");
      return [{
        name: name,
        tip: "部门不可为空"
      }];
    }

    return [];
  };

  var onPopupVisibleChangeHandler = function onPopupVisibleChangeHandler(visible) {
    setHasError(false);
    setHasActive(false);

    if (visible) {
      var errors = verifing();

      if (errors.length) {
        setHasError(true);
      }

      setHasActive(true);
    }
  };

  var onClearHandler = function onClearHandler() {
    onChange(undefined);
    setHasActive(false);
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      verifing: verifing
    };
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(preCls)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__label"))
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__label-text"))
  }, label), require && /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__label-require"))
  }, "*")), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__border"), (_classNames = {}, _defineProperty(_classNames, "".concat(preCls, "__border--error"), hasError), _defineProperty(_classNames, "".concat(preCls, "__border--active"), hasActive), _classNames))
  }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"].Error, {
    placement: "topLeft",
    destroyTooltipOnHide: true,
    visible: (hasActive || hasError) && tip,
    overlay: /*#__PURE__*/_react["default"].createElement(OverlayInner, {
      hasActive: hasActive,
      hasError: hasError,
      tip: tip
    }),
    getTooltipContainer: getContainer
  }, /*#__PURE__*/_react["default"].createElement(_userSelector["default"].Depart, {
    ref: departRef,
    onSelect: onDepartSelect // position="bottomLeft"
    ,
    position: "centerRight",
    onPopupVisibleChange: onPopupVisibleChangeHandler
  }, /*#__PURE__*/_react["default"].createElement(_departmentInput["default"] // @ts-ignore
  , {
    onClick: function onClick() {
      return departRef.current && departRef.current.open();
    },
    department: selectedDepart,
    placeholder: placeholder,
    onClear: onClearHandler
  })))));
});

exports["default"] = _default;