"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.parseTag = exports.parseString = exports.isPlatform = exports.getOpenId = exports["default"] = exports.OpenUserName = exports.OpenDepartmentName = void 0;

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

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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

/**
 * 企业微信用户来源代码
 */
var WX_SOURCE = 860;
/**
 * 钉钉用户来源代码
 */

var DINGTALK_SOURCE = 861;
/**
 * 飞书用户来源代码
 */

var FEISHU_SOURCE = 862;
/**
 * 企业微信浏览器标识字符串
 */

var WX_UA_STR = "micromessenger";
/**
 * 获取全局对象BSGlobal中的数据
 * @param key 键名
 */

var getGlobal = function getGlobal(key) {
  return (// @ts-ignore
    window.BSGlobal[key]
  );
};
/**
 * 获取用户来源渠道
 */


var getSource = function getSource() {
  var _getGlobal;

  return ((_getGlobal = getGlobal("tenantInfo")) === null || _getGlobal === void 0 ? void 0 : _getGlobal.Source) || 0;
};
/**
 * 用户浏览器 userAgent
 */


var getUA = function getUA() {
  return navigator.userAgent.toLowerCase();
};

var isPlatform = function isPlatform(name) {
  var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "all";

  switch (name) {
    case "wx":
      {
        if (type == "source") return getSource() == WX_SOURCE;
        if (type == "env") return getUA().includes(WX_UA_STR);
        return getSource() == WX_SOURCE || getUA().includes(WX_UA_STR);
      }

    case "dd":
      {
        return getSource() == DINGTALK_SOURCE;
      }

    case "feishu":
      {
        return getSource() == FEISHU_SOURCE;
      }
  }
};
/**
 * 获取openId
 * @param {number} id 人员id
 * @param {string} type openData类型
 */


exports.isPlatform = isPlatform;

var getOpenId = function getOpenId(id, type) {
  var openIds;

  if (type == "userName") {
    openIds = getGlobal("openIds");
  } else if (type == "departmentName") {
    openIds = getGlobal("openDepartmentIds");
  }

  return openIds && openIds[id];
};
/**
 * 根据平台返回获取开放数据的方法
 *
 */


exports.getOpenId = getOpenId;

function openData() {
  // @ts-ignore
  if (getSource() == WX_SOURCE || getUA().includes(WX_UA_STR)) {
    return function (p) {
      if (p.id === undefined) return p.name || null; // <span style={{color:'red'}}>ID_EMPTY</span>//

      var openId = getOpenId(p.id, p.type);
      if (!openId) return p.name || p.id || null; // <span style={{color:'red'}}>OPENID_EMPTY</span>;

      var ref = (0, _react.useRef)(null);
      (0, _react.useEffect)(function () {
        var _ref;

        // @ts-ignore
        (_ref = window.WWOpenData || window.top.WWOpenData) === null || _ref === void 0 ? void 0 : _ref.bind(ref.current);
      });
      return /*#__PURE__*/_react["default"].createElement("div", {
        style: {
          display: "inline-block",
          position: "relative",
          overflow: "hidden",
          textOverflow: "ellipsis",
          verticalAlign: "middle"
        }
      },
      /*#__PURE__*/
      // @ts-ignore
      _react["default"].createElement("ww-open-data", {
        ref: ref,
        type: p.type,
        openid: openId
      }), /*#__PURE__*/_react["default"].createElement("div", {
        style: {
          position: "absolute",
          top: 0,
          left: 0,
          right: 0,
          bottom: 0,
          zIndex: 1
        }
      }));
    };
  }

  return function (p) {
    return p.name || p.id || null;
  };
}

var getOpenData = openData();
/**
 * 获取各平台开放数据
 * @param {id} openId
 * @param {type} 数据类型
 * @param {name} 用于不运行在平台时直接展示信息
 */

var _default = getOpenData;
/**
 * 获取开放数据：用户名
 * @param p
 */

exports["default"] = _default;

var OpenUserName = function OpenUserName(p) {
  return getOpenData(_objectSpread(_objectSpread({}, p), {}, {
    type: "userName"
  }));
};
/**
 * 获取开放数据：部门名称
 * @param p
 */


exports.OpenUserName = OpenUserName;

var OpenDepartmentName = function OpenDepartmentName(p) {
  return getOpenData(_objectSpread(_objectSpread({}, p), {}, {
    type: "departmentName"
  }));
};
/**
 * 后端类型转化map
 */


exports.OpenDepartmentName = OpenDepartmentName;
var openDataMap = {
  user: OpenUserName,
  department: OpenDepartmentName
};
/**
 * 展示操作记录等内容中的openData数据
 * @param str 含有openData数据的字符串
 */

var parseString = function parseString(str) {
  var _window$BSGlobal$tena;

  // @ts-ignore
  var source = (_window$BSGlobal$tena = window.BSGlobal.tenantInfo) === null || _window$BSGlobal$tena === void 0 ? void 0 : _window$BSGlobal$tena.Source;
  var ua = navigator.userAgent.toLowerCase();
  if (source != 860 && ua.indexOf("micromessenger") == -1) return /*#__PURE__*/_react["default"].createElement("span", {
    dangerouslySetInnerHTML: {
      __html: str
    }
  });
  var reg = /\[(\w+?)\:(\w+?)\]/g;
  var strReg = /\[\w+?\:\w+?\]/;
  var openDatas = [];
  var matchStr = reg.exec(str);

  while (matchStr != null) {
    var OpenDataCompoent = openDataMap[matchStr[2]];

    if (!OpenDataCompoent) {
      openDatas.push(matchStr[0]);
    } else {
      openDatas.push( /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(OpenDataCompoent, {
        id: matchStr[1]
      })));
    }

    matchStr = reg.exec(str);
  }

  var commonStrs = str.split(strReg);
  var renderData = [];

  for (var i = 0; i < commonStrs.length; i++) {
    renderData.push( /*#__PURE__*/_react["default"].createElement("span", {
      dangerouslySetInnerHTML: {
        __html: commonStrs[i]
      }
    }));
    renderData.push(openDatas[i]);
  }

  return renderData;
};
/**
 * 展示回复内容中@ 他人等包含openData的数据
 * @param str 含有openData数据的字符串
 */


exports.parseString = parseString;

var parseTag = function parseTag(str) {
  var _window$BSGlobal$tena2;

  // @ts-ignore
  var source = (_window$BSGlobal$tena2 = window.BSGlobal.tenantInfo) === null || _window$BSGlobal$tena2 === void 0 ? void 0 : _window$BSGlobal$tena2.Source;
  var ua = navigator.userAgent.toLowerCase();
  if (source != 860 && ua.indexOf("micromessenger") == -1) return /*#__PURE__*/_react["default"].createElement("span", {
    dangerouslySetInnerHTML: {
      __html: str
    }
  });
  var reg = /<\w+[\w\W]*?(userId|departmentId)=["'](\S+?)["'][\w\W]*?>@([\w\W]*?)<\/\w+>/g;
  var strReg = /<\w+[\w\s"'\/=\:\.]*?(?:userId|departmentId)=["']\S+?["'][\w\s"'\/=\:\.]*?>[\w\W]*?<\/\w+?>/;
  var openDatas = [];
  var matchStr = reg.exec(str);

  while (matchStr != null) {
    var OpenDataCompoent = matchStr[1] == "userId" ? OpenUserName : OpenDepartmentName;

    if (!OpenDataCompoent) {
      openDatas.push( /*#__PURE__*/_react["default"].createElement("span", {
        dangerouslySetInnerHTML: {
          __html: matchStr[0]
        }
      }));
    } else {
      openDatas.push( /*#__PURE__*/_react["default"].createElement("span", {
        style: {
          color: "#1890ff"
        }
      }, "@", /*#__PURE__*/_react["default"].createElement(OpenDataCompoent, {
        id: matchStr[2],
        name: matchStr[3]
      })));
    }

    matchStr = reg.exec(str);
  }

  var commonStrs = str.split(strReg);
  var renderData = [];

  for (var i = 0; i < commonStrs.length; i++) {
    renderData.push( /*#__PURE__*/_react["default"].createElement("span", {
      dangerouslySetInnerHTML: {
        __html: commonStrs[i]
      }
    }));
    renderData.push(openDatas[i]);
  }

  return renderData;
};

exports.parseTag = parseTag;