"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.isWechat = exports.getSource = exports.getOpenId = 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 WX_UA_STR = "micromessenger";

var getGlobal = function getGlobal(key) {
  return window.BSGlobal[key];
};

var getSource = function getSource() {
  var _getGlobal;

  return ((_getGlobal = getGlobal("tenantInfo")) === null || _getGlobal === void 0 ? void 0 : _getGlobal.Source) || 0;
};

exports.getSource = getSource;

var isWechat = function isWechat() {
  return getSource() == 860;
};

exports.isWechat = isWechat;

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

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(p) {
  if (getSource() == WX_SOURCE || getUA().includes(WX_UA_STR)) {
    return function (p) {
      if (p.id === undefined) return p.name || null;
      var openId = getOpenId(p.id, p.type);
      if (!openId) return p.name || p.id || null;
      var ref = (0, _react.useRef)(null);
      (0, _react.useEffect)(function () {
        window.WWOpenData && window.WWOpenData.bind(ref.current);
      });
      return /*#__PURE__*/_react["default"].createElement("div", {
        style: {
          display: "inline-block",
          position: "relative",
          overflow: "hidden",
          textOverflow: "ellipsis",
          verticalAlign: "middle"
        }
      }, /*#__PURE__*/_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();

var OpenUserName = function OpenUserName(p) {
  return getOpenData(_objectSpread(_objectSpread({}, p), {}, {
    type: "userName"
  }));
};

exports.OpenUserName = OpenUserName;

var OpenDepartmentName = function OpenDepartmentName(p) {
  return getOpenData(_objectSpread(_objectSpread({}, p), {}, {
    type: "departmentName"
  }));
};

exports.OpenDepartmentName = OpenDepartmentName;