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

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

var _bsGlobal = require("../../utils/bs-global");

var _requestApi = require("./request-api");

var _constant = require("./constant");

var _title = _interopRequireDefault(require("./images/title.png"));

var _leftLv = _interopRequireDefault(require("./images/left-lv.png"));

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

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 prefix = 'version-change-modal';
var VersionChangeModal = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var onClose = _ref.onClose;

  var _useState = (0, _react.useState)(function () {
    return (0, _constant.getVersionList)();
  }),
      _useState2 = _slicedToArray(_useState, 1),
      versionList = _useState2[0];

  var _useState3 = (0, _react.useState)(true),
      _useState4 = _slicedToArray(_useState3, 2),
      showRight = _useState4[0],
      setShowRight = _useState4[1];

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

  var _useState7 = (0, _react.useState)(20220602),
      _useState8 = _slicedToArray(_useState7, 1),
      source = _useState8[0];

  var defaultScrollLength = 236 * 3 + 40;
  var bookDemoRef = (0, _react.useRef)(null);
  var listRef = (0, _react.useRef)(null);

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

  var closeVersionDialog = function closeVersionDialog() {
    return onClose();
  };

  var onVersionChange = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(version) {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (0, _requestApi.setVersion)(version);

            case 2:
              location.href = '/';

            case 3:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onVersionChange(_x) {
      return _ref2.apply(this, arguments);
    };
  }();

  var getCurVersion = function getCurVersion() {
    var _getVersionList$find;

    var menuVersion = (0, _bsGlobal.getBSGlobal)('tenantAuthentication').MenuVersion || _constant.Versions.OKR;

    return (_getVersionList$find = (0, _constant.getVersionList)().find(function (item) {
      return item.version === menuVersion;
    })) === null || _getVersionList$find === void 0 ? void 0 : _getVersionList$find.name;
  };

  var checkArrowVivible = function checkArrowVivible() {
    var _listRef$current = listRef.current,
        scrollLeft = _listRef$current.scrollLeft,
        clientWidth = _listRef$current.clientWidth,
        scrollWidth = _listRef$current.scrollWidth;

    if (scrollLeft === 0) {
      setShowLeft(false);
      setShowRight(true);
    } else if (scrollLeft + clientWidth === scrollWidth) {
      setShowLeft(true);
      setShowRight(false);
    } else {
      setShowLeft(true);
      setShowRight(true);
    }
  };

  var goLeft = function goLeft() {
    var _listRef$current2 = listRef.current,
        scrollLeft = _listRef$current2.scrollLeft,
        scrollWidth = _listRef$current2.scrollWidth,
        clientWidth = _listRef$current2.clientWidth;
    var length = scrollLeft;

    if (scrollLeft - defaultScrollLength > 0) {
      length = defaultScrollLength;
    }

    var target = scrollLeft - length;
    var timer = setInterval(function () {
      var step = length / 10;
      step = step > 0 ? Math.ceil(step) : Math.floor(step);
      listRef.current.scrollLeft = listRef.current.scrollLeft - step;

      if (Math.abs(listRef.current.scrollLeft - target) <= Math.abs(step)) {
        listRef.current.scrollLeft = target;
        clearInterval(timer);
        checkArrowVivible();
      }
    }, 30);
  };

  var goRight = function goRight() {
    var _listRef$current3 = listRef.current,
        clientWidth = _listRef$current3.clientWidth,
        scrollLeft = _listRef$current3.scrollLeft,
        scrollWidth = _listRef$current3.scrollWidth;
    var length = scrollWidth - clientWidth;

    if (scrollLeft + defaultScrollLength < scrollWidth - clientWidth) {
      length = scrollLeft + defaultScrollLength;
    }

    var timer = setInterval(function () {
      var step = length / 10;
      step = step > 0 ? Math.ceil(step) : Math.floor(step);
      listRef.current.scrollLeft = listRef.current.scrollLeft + step;

      if (Math.abs(length - listRef.current.scrollLeft) <= Math.abs(step)) {
        listRef.current.scrollLeft = length;
        clearInterval(timer); // 判断箭头是否展示

        checkArrowVivible();
      }
    }, 30);
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__wrapper")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__bg")
  }), /*#__PURE__*/_react["default"].createElement(_scrollbar["default"], null, /*#__PURE__*/_react["default"].createElement("div", {
    className: prefix
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "_bg-color1")
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "_bg-color2")
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "_bg-color3")
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefix, "__close-icon"),
    onClick: closeVersionDialog
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-canceled"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__header")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__title")
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _title["default"],
    height: 38
  })), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefix, "__cur-version")
  }, "\u5F53\u524D\u7248\u672C", /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefix, "__version-tag")
  }, "\u300C", getCurVersion(), "\u300D")), /*#__PURE__*/_react["default"].createElement("span", {
    onClick: onShow,
    className: "".concat(prefix, "__book-demo")
  }, "\u9884\u7EA6\u6F14\u793A", /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-fanhui"
  })))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__list-wrapper")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__list"),
    ref: listRef
  }, versionList.map(function (versionItem) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "__list-item ").concat(prefix, "__list-item-").concat(versionItem.key)
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "__list-item-img")
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: versionItem.image,
      width: 148
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "__list-item-title ").concat(prefix, "__list-item-title-").concat(versionItem.key)
    }, versionItem.name), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "__list-item-content")
    }, versionItem.itemType === 'normal' && versionItem.items.map(function (item) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefix, "__detail-item")
      }, /*#__PURE__*/_react["default"].createElement("span", null, item), /*#__PURE__*/_react["default"].createElement("span", {
        className: "tu-icon-finished"
      }));
    }), versionItem.itemType === 'tag' && versionItem.items.map(function (item) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefix, "__app-item")
      }, /*#__PURE__*/_react["default"].createElement("img", {
        src: item.image,
        width: "30"
      }), /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefix, "__app-item-title")
      }, item.title));
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefix, "__btn"),
      onClick: function onClick() {
        return onVersionChange(versionItem.version);
      }
    }, "\u514D\u8D39\u4F53\u9A8C")));
  })), showLeft && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__left-arrow"),
    onClick: goLeft
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-left"
  })), showRight && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__right-arrow"),
    onClick: goRight
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-APP-xi"
  }))), /*#__PURE__*/_react["default"].createElement(_bookDemo["default"].BookDemoPC, {
    visible: false,
    ref: bookDemoRef,
    type: _bookDemo["default"].DemoType.UniversalBookDemo,
    source: source
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "__left-lv")
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _leftLv["default"],
    width: 85
  })));
});
var _default = VersionChangeModal;
exports["default"] = _default;