"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;
exports.visibilityMapText = void 0;

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

var _titaUi = require("tita-ui");

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

var _userSelector = _interopRequireDefault(require("../../user-selector"));

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

require("./index.css");

var _style = require("../style");

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

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

var visibilityList = function visibilityList() {
  var visibility = [{
    value: 2,
    lable: (0, _getLocale.getLocale)("OKR_MyO_From_Associatedmem")
  }, {
    value: 3,
    lable: (0, _getLocale.getLocale)("OKR_MyO_Pop_Directrep")
  }, {
    value: 4,
    lable: (0, _getLocale.getLocale)("OKR_MyO_Pop_Specified")
  }];

  if (window.BSGlobal.tenantInfo.isOpenInfo) {
    visibility.unshift({
      value: 1,
      lable: (0, _getLocale.getLocale)("OKR_MyO_Pop_Public")
    });
  }

  return visibility;
};

var visibilityMapText = function visibilityMapText() {
  return {
    1: (0, _getLocale.getLocale)('OKR_MyO_Pop_Public'),
    2: (0, _getLocale.getLocale)('OKR_MyO_From_Associatedmem'),
    3: (0, _getLocale.getLocale)('OKR_MyO_Pop_Directrep'),
    4: (0, _getLocale.getLocale)('OKR_MyO_Pop_Specified')
  };
};

exports.visibilityMapText = visibilityMapText;

function _default(_ref) {
  var visibility = _ref.visibility,
      setVisibility = _ref.setVisibility,
      visiableLimit = _ref.visiableLimit,
      setVisiableLimit = _ref.setVisiableLimit,
      isCreateModal = _ref.isCreateModal;
  var selectorRef = (0, _react.useRef)();

  var onOptionChange = function onOptionChange(v) {
    if (v === 4) {
      selectorRef.current && selectorRef.current.open();
    } else {
      setVisibility(v);
    }
  };

  var onOk = function onOk(selectNodes) {
    var departments = selectNodes.departments,
        groups = selectNodes.groups,
        users = selectNodes.users;

    if (departments.length === 0 && groups.length === 0 && users.length === 0) {
      selectorRef.current && selectorRef.current.close();
      return;
    }

    setVisibility(4);
    setVisiableLimit(selectNodes);
    selectorRef.current && selectorRef.current.close();
  };

  var renderLabel = function renderLabel(_ref2) {
    var value = _ref2.value,
        lable = _ref2.lable;

    if (value === 4) {
      var _visiableLimit$groups = visiableLimit.groups,
          groups = _visiableLimit$groups === void 0 ? [] : _visiableLimit$groups,
          _visiableLimit$depart = visiableLimit.departments,
          departments = _visiableLimit$depart === void 0 ? [] : _visiableLimit$depart,
          _visiableLimit$users = visiableLimit.users,
          users = _visiableLimit$users === void 0 ? [] : _visiableLimit$users;
      var text = [];
      var jsxDom = [];
      (departments || []).forEach(function (item) {
        jsxDom.push( /*#__PURE__*/_react["default"].createElement(_openData.OpenDepartmentName, {
          id: item.id,
          name: item.name
        }));
        jsxDom.push( /*#__PURE__*/_react["default"].createElement("span", null, "\u3001"));
        text.push(item.name);
      });
      (users || []).forEach(function (item) {
        jsxDom.push( /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
          id: item.userId,
          name: item.name
        }));
        jsxDom.push( /*#__PURE__*/_react["default"].createElement("span", null, "\u3001"));
        text.push(item.name);
      });
      (groups || []).forEach(function (item) {
        jsxDom.push( /*#__PURE__*/_react["default"].createElement("span", null, item.name, "\u3001"));
        text.push(item.name);
      });

      if (!text.length) {
        return visibilityMapText[visibility];
      }

      return (0, _openData.isWechat)() ? jsxDom : text.join('、');
    }

    return lable;
  };

  var subTitleRender = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: "okr-field-visible"
    }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Title_Perm'), /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: (0, _getLocale.getLocale)('OKR_MyO_Pop_Inadditiontot'),
      getTooltipContainer: function getTooltipContainer() {
        return document.body;
      }
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "tu-icon-question-mark",
      style: {
        display: 'inline-block',
        marginLeft: 8,
        marginBottom: 3,
        color: '#bfc7d5',
        verticalAlign: 'middle'
      }
    })));
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-modal__sub-block"
  }, isCreateModal ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-modal__sub-title"
  }, subTitleRender) : /*#__PURE__*/_react["default"].createElement(_style.TitleLine, null, subTitleRender), /*#__PURE__*/_react["default"].createElement(_userSelector["default"], {
    ref: selectorRef // @ts-ignore
    ,
    multiple: true,
    onOk: onOk,
    position: "centerRight",
    selectNodes: visiableLimit,
    trigger: /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        visibility: 'hidden',
        position: 'absolute',
        zIndex: -1,
        height: 36
      }
    }, "empty select")
  }), /*#__PURE__*/_react["default"].createElement(_titaUi.Select, {
    value: visibility,
    onChange: onOptionChange,
    options: visibilityList() // @ts-ignore
    ,
    onClick: function onClick(e) {
      return e.stopPropagation();
    },
    renderLabel: renderLabel
  }));
}