"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 _drawer = _interopRequireDefault(require("../../../drawer"));

var _bookDemo = _interopRequireDefault(require("../../../book-demo"));

var _errorBoundary = _interopRequireDefault(require("../../../error-boundary"));

var _utils = require("../../utils");

var _errorLevel = _interopRequireDefault(require("../../../../utils/error-level"));

var _helpers = require("../../../../utils/helpers");

var _platform = require("../../../../utils/platform");

require("./index.css");

var _interface = require("./interface");

var _yuyueAward = _interopRequireDefault(require("./images/yuyue-award.png"));

var _quickEntrance = _interopRequireDefault(require("../../../quick-entrance"));

var _guideTip = _interopRequireDefault(require("../../../guide-tip"));

var _getLoginUserInfo, _getTenantInfo;

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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

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 = 'app-center';
var userId = (_getLoginUserInfo = (0, _helpers.getLoginUserInfo)()) === null || _getLoginUserInfo === void 0 ? void 0 : _getLoginUserInfo.Id;
var isPerformance = ((_getTenantInfo = (0, _helpers.getTenantInfo)()) === null || _getTenantInfo === void 0 ? void 0 : _getTenantInfo.tenantType) == 2;
var noshowChangeVersion = (0, _platform.isFeishu)(true) && isPerformance;

var AppCenter = function AppCenter(props) {
  var _props$appCenterVisib = props.appCenterVisible,
      appCenterVisible = _props$appCenterVisib === void 0 ? false : _props$appCenterVisib,
      closeAppCenter = props.closeAppCenter,
      setMenu = props.setMenu;

  var _useState = (0, _react.useState)(function () {
    var menus = (0, _utils.getMenus)();
    return menus.filter(function (menu) {
      return menu.code !== 'dynamic';
    });
  }),
      _useState2 = _slicedToArray(_useState, 1),
      apps = _useState2[0];

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

  var _useState5 = (0, _react.useState)(function () {
    var _window$BSGlobal, _window$BSGlobal$tena;

    return ((_window$BSGlobal = window.BSGlobal) === null || _window$BSGlobal === void 0 ? void 0 : (_window$BSGlobal$tena = _window$BSGlobal.tenantAuthentication) === null || _window$BSGlobal$tena === void 0 ? void 0 : _window$BSGlobal$tena.Version) === 1;
  }),
      _useState6 = _slicedToArray(_useState5, 1),
      isTrial = _useState6[0];

  var _useState7 = (0, _react.useState)(function () {
    var localCurrentAppId = sessionStorage.getItem("LocalCurrentAppId_".concat(userId));

    if (localCurrentAppId) {
      return Number(localCurrentAppId);
    }

    var hash = window.location.hash.split('?')[0] || '';

    if (hash) {
      var activeApp = apps.find(function (app) {
        return hash.includes(app.code);
      });
      return (activeApp === null || activeApp === void 0 ? void 0 : activeApp.id) || apps[0].id;
    }

    return apps[0].id;
  }),
      _useState8 = _slicedToArray(_useState7, 2),
      currentAppId = _useState8[0],
      setCurrentAppId = _useState8[1];

  var QuickEntranceDialogRef = (0, _react.useRef)(null);
  var bookDemoRef = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    setInit(true);
  }, []);
  (0, _react.useEffect)(function () {
    var _apps$find;

    var menus = ((_apps$find = apps.find(function (app) {
      return app.id === currentAppId;
    })) === null || _apps$find === void 0 ? void 0 : _apps$find.children) || [];
    var navigations = (0, _utils.getMenus)();
    sessionStorage.setItem("LocalCurrentAppId_".concat(userId), currentAppId.toString());
    var dynamic = navigations.filter(function (item) {
      return item.code === 'dynamic';
    });
    setMenu([].concat(_toConsumableArray(dynamic), _toConsumableArray(menus)));

    if (init) {
      var _menus$, _menus$2;

      if (((_menus$ = menus[0]) === null || _menus$ === void 0 ? void 0 : _menus$.code) === "learning") {
        var _dynamic$;

        window.location.href = (_dynamic$ = dynamic[0]) === null || _dynamic$ === void 0 ? void 0 : _dynamic$.href;
        return;
      }

      window.location.href = (_menus$2 = menus[0]) === null || _menus$2 === void 0 ? void 0 : _menus$2.href;
    }

    closeAppCenter();
  }, [currentAppId]);

  var onAppSelectHandler = function onAppSelectHandler(app) {
    if (app.type === 'newPage') {
      window.location.href = "".concat(window.BSGlobal.apiPath, "/").concat(app.href);
      return;
    }

    setCurrentAppId(app.id);
    closeAppCenter();
  };

  var getBookDemoVersion = function getBookDemoVersion() {
    var _window$BSGlobal$tena2;

    var menuVersion = (_window$BSGlobal$tena2 = window.BSGlobal.tenantAuthentication) === null || _window$BSGlobal$tena2 === void 0 ? void 0 : _window$BSGlobal$tena2.MenuVersion;

    if (menuVersion === _interface.ETrialVersion.OKR) {
      return _bookDemo["default"].DemoType.TryOKR;
    }

    if (menuVersion === _interface.ETrialVersion.Performance) {
      return _bookDemo["default"].DemoType.TryPerformance;
    }

    return _bookDemo["default"].DemoType.UniversalBookDemo;
  };

  var handlePoint = function handlePoint() {
    if (bookDemoRef.current) {
      bookDemoRef.current.show();
    }

    if (window.titaTracker) {
      window.titaTracker('action').record({
        actionName: '申请演示',
        actionGroup: '试用版-申请演示',
        productName: '系统内-申请演示'
      });
    }
  };

  var handleOkQuickMenu = function handleOkQuickMenu(newQuickMenu) {
    var _dynamic$2, _newQuickMenu$;

    var navigations = (0, _utils.getMenus)();
    var dynamic = navigations.filter(function (item) {
      return item.code === 'dynamic';
    });
    var quickMenu = navigations.find(function (item) {
      return item.code === 'quick';
    });
    var dynamicHref = (_dynamic$2 = dynamic[0]) === null || _dynamic$2 === void 0 ? void 0 : _dynamic$2.href;

    if (currentAppId !== quickMenu.id) {
      location.href = dynamicHref;
    }

    setMenu([].concat(_toConsumableArray(dynamic), _toConsumableArray(newQuickMenu)));
    setCurrentAppId(quickMenu.id);

    if (((_newQuickMenu$ = newQuickMenu[0]) === null || _newQuickMenu$ === void 0 ? void 0 : _newQuickMenu$.code) === "learning") {
      var _dynamic$3;

      window.location.href = (_dynamic$3 = dynamic[0]) === null || _dynamic$3 === void 0 ? void 0 : _dynamic$3.href;
      return;
    }

    var hash = window.location.hash.split('?')[0] || '';

    if (hash) {
      var isUseNewMenu = window.BSGlobal.isUseNewMenu;
      var activeMenu; //用来判断当前路由是否包含在修改后的常用菜单中

      if (!isUseNewMenu) {
        // 老版本的menu
        activeMenu = newQuickMenu.find(function (item) {
          var _item$href$split, _item$href$split$, _item$href$split$$spl, _item$href$split$$spl2, _item$href$split$$spl3, _item$href$split2, _item$href$split2$, _item$href$split2$$sp, _item$href$split2$$sp2, _item$href$split2$$sp3;

          return hash.includes(item.href.startsWith("#/") ? (_item$href$split = item.href.split('#/')) === null || _item$href$split === void 0 ? void 0 : (_item$href$split$ = _item$href$split[1]) === null || _item$href$split$ === void 0 ? void 0 : (_item$href$split$$spl = _item$href$split$.split('?')) === null || _item$href$split$$spl === void 0 ? void 0 : (_item$href$split$$spl2 = _item$href$split$$spl[0]) === null || _item$href$split$$spl2 === void 0 ? void 0 : (_item$href$split$$spl3 = _item$href$split$$spl2.split('/')) === null || _item$href$split$$spl3 === void 0 ? void 0 : _item$href$split$$spl3[0] : (_item$href$split2 = item.href.split('#')) === null || _item$href$split2 === void 0 ? void 0 : (_item$href$split2$ = _item$href$split2[1]) === null || _item$href$split2$ === void 0 ? void 0 : (_item$href$split2$$sp = _item$href$split2$.split('?')) === null || _item$href$split2$$sp === void 0 ? void 0 : (_item$href$split2$$sp2 = _item$href$split2$$sp[0]) === null || _item$href$split2$$sp2 === void 0 ? void 0 : (_item$href$split2$$sp3 = _item$href$split2$$sp2.split('/')) === null || _item$href$split2$$sp3 === void 0 ? void 0 : _item$href$split2$$sp3[0]);
        });
      } else {
        //新版本menu
        activeMenu = newQuickMenu.find(function (item) {
          var _item$href$split3, _item$href$split3$, _item$href$split3$$sp, _item$href$split4, _item$href$split4$, _item$href$split4$$sp;

          return hash.includes(item.href.startsWith("#/") ? (_item$href$split3 = item.href.split('#/')) === null || _item$href$split3 === void 0 ? void 0 : (_item$href$split3$ = _item$href$split3[1]) === null || _item$href$split3$ === void 0 ? void 0 : (_item$href$split3$$sp = _item$href$split3$.split('?')) === null || _item$href$split3$$sp === void 0 ? void 0 : _item$href$split3$$sp[0] : (_item$href$split4 = item.href.split('#')) === null || _item$href$split4 === void 0 ? void 0 : (_item$href$split4$ = _item$href$split4[1]) === null || _item$href$split4$ === void 0 ? void 0 : (_item$href$split4$$sp = _item$href$split4$.split('?')) === null || _item$href$split4$$sp === void 0 ? void 0 : _item$href$split4$$sp[0]);
        });
      }

      if (!activeMenu && newQuickMenu.length > 0) {
        var _newQuickMenu$2;

        window.location.href = (_newQuickMenu$2 = newQuickMenu[0]) === null || _newQuickMenu$2 === void 0 ? void 0 : _newQuickMenu$2.href;
      }
    }
  };

  var RenderSettingBtn = function RenderSettingBtn(props) {
    var app = props.app,
        QuickEntranceDialogRef = props.QuickEntranceDialogRef;

    var _useState9 = (0, _react.useState)(false),
        _useState10 = _slicedToArray(_useState9, 2),
        settingGuideVisible = _useState10[0],
        setSettingGuideVisible = _useState10[1];

    (0, _react.useEffect)(function () {
      var _getLoginUserInfo2;

      if (noshowChangeVersion) return;
      var cacheIsKonw = localStorage.getItem("AppCenterSettingGuide_".concat((_getLoginUserInfo2 = (0, _helpers.getLoginUserInfo)()) === null || _getLoginUserInfo2 === void 0 ? void 0 : _getLoginUserInfo2.Id));
      !cacheIsKonw && setSettingGuideVisible(true);
    }, []);

    var handleSettingGuideClose = function handleSettingGuideClose() {
      var _getLoginUserInfo3, _getLoginUserInfo4;

      localStorage.setItem("AppCenterSettingGuide_".concat((_getLoginUserInfo3 = (0, _helpers.getLoginUserInfo)()) === null || _getLoginUserInfo3 === void 0 ? void 0 : _getLoginUserInfo3.Id), (_getLoginUserInfo4 = (0, _helpers.getLoginUserInfo)()) === null || _getLoginUserInfo4 === void 0 ? void 0 : _getLoginUserInfo4.Id);
      setSettingGuideVisible(false);
    };

    var onHandleClickGuideTip = function onHandleClickGuideTip(e) {
      //用来阻止事件冒泡
      e.stopPropagation();
    };

    var handleSetting = function handleSetting(e) {
      var _QuickEntranceDialogR;

      e.stopPropagation();
      (_QuickEntranceDialogR = QuickEntranceDialogRef.current) === null || _QuickEntranceDialogR === void 0 ? void 0 : _QuickEntranceDialogR.showDialog();
      handleSettingGuideClose();

      if (window.titaTracker) {
        window.titaTracker('action').record({
          actionName: '快捷入口设置',
          actionGroup: '新菜单设置',
          productName: '系统菜单'
        });
      }
    };

    if (app.appId === 1) {
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(precls, "__app-item-name__right-btn"),
        onClick: onHandleClickGuideTip
      }, /*#__PURE__*/_react["default"].createElement(_guideTip["default"], {
        visible: settingGuideVisible,
        content: /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(precls, "__app-item-name__right-btn__guide-tip")
        }, "\uD83D\uDC46\u70B9\u51FB\u8FD9\u91CC\uFF0C\u53EF\u300C\u8BBE\u7F6E\u300D\u81EA\u5DF1\u5E38\u7528\u7684\u5FEB\u6377\u83DC\u5355"),
        onClickClose: handleSettingGuideClose,
        popupPlacement: 'bottom',
        type: 'button'
      }, /*#__PURE__*/_react["default"].createElement("span", {
        onClick: handleSetting
      }, "\u8BBE\u7F6E")));
    }

    return /*#__PURE__*/_react["default"].createElement("span", null);
  };

  return /*#__PURE__*/_react["default"].createElement(_drawer["default"], {
    className: "".concat(precls, "__drawer"),
    placement: "top",
    onClose: closeAppCenter,
    visible: appCenterVisible,
    width: "100%",
    autoClosable: true
  }, /*#__PURE__*/_react["default"].createElement(_quickEntrance["default"], {
    ref: QuickEntranceDialogRef,
    handleOkQuickMenu: handleOkQuickMenu
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: precls
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(precls, "__app-list")
  }, apps.length > 0 && apps.map(function (app) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(precls, "__app-item ").concat(precls, "__app-item--").concat(app.code),
      onClick: function onClick() {
        return onAppSelectHandler(app);
      }
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])("".concat(precls, "__app-item-icon"), "".concat(precls, "__app-item-icon--").concat(app.iconName))
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(precls, "__app-item-desc")
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(precls, "__app-item-name")
    }, /*#__PURE__*/_react["default"].createElement("span", null, app.title), /*#__PURE__*/_react["default"].createElement(RenderSettingBtn, {
      app: app,
      QuickEntranceDialogRef: QuickEntranceDialogRef
    })), isTrial && /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(precls, "__app-item-sub-title")
    }, app.subTitle)));
  }), new Array(20).fill(1).map(function () {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(precls, "__app-item-placeholder")
    });
  }))), isTrial && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, !noshowChangeVersion && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(precls, "__app-right-img"),
    onClick: handlePoint
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(precls, "__app-right-img__img"),
    src: _yuyueAward["default"],
    alt: "\u9884\u7EA6\u6709\u5956"
  })), /*#__PURE__*/_react["default"].createElement(_errorBoundary["default"], {
    errorLevel: _errorLevel["default"].P0,
    errorTag: "\u7533\u8BF7\u9884\u7EA6\u6F14\u793A\u5F02\u5E38"
  }, /*#__PURE__*/_react["default"].createElement(_bookDemo["default"].BookDemoPC, {
    visible: false,
    ref: bookDemoRef,
    type: getBookDemoVersion(),
    source: 20220601
  }))));
};

var _default = AppCenter;
exports["default"] = _default;