/**
 * 路由离开前记住缓存，返回后不刷新页面
 *
 * 比如创建赛事页面，如果前往查看规则，返回后，希望保留之前的表单
 *
 * 注意，用了这个 mixin，就不要用 onShow 了，而是用 mounted，
 * 否则可能会重复触发刷新
 *
 * @param {string} config.refresh 刷新方法
 * @returns 返回对象，包含 beforeRouteLeave 和 activated 方法
 * @example
 * ```ts
 * // 在 Vue 组件中：
 * export default {
 *   mixins: [getRouteLeaveCache({ refresh: 'fetchData' })],
 *   methods: {
 *     fetchData() { ... },
 *     onClickRule() {
 *       this._jumpToCacheRoute();
 *       this.$router.push('/rule');
 *     },
 *   },
 * };
 * ```
 */
function getRouteLeaveCache(_ref) {
  var refresh = _ref.refresh;
  var randomKey = '';
  var RANDOM_KEY = 'RANDOM_KEY_CREATE';
  return {
    // beforeRouteLeave(to: { name: string }, from: any, next: Function) {
    //   if (cacheRoutes.includes(to.name)) {
    //     randomKey = `${Math.random() * 10000000}`;
    //     localStorage.setItem(RANDOM_KEY, randomKey);
    //   } else {
    //     localStorage.setItem(RANDOM_KEY, '');
    //   }
    //   next();
    // },
    activated: function activated() {
      var _a;
      var localRandomKey = localStorage.getItem(RANDOM_KEY);
      localStorage.setItem(RANDOM_KEY, '');
      var useCache = localRandomKey && localRandomKey == randomKey;
      if (!useCache && refresh) {
        // @ts-ignore
        (_a = this[refresh]) === null || _a === void 0 ? void 0 : _a.call(this);
      }
    },
    mounted: function mounted() {
      localStorage.setItem(RANDOM_KEY, '');
    },
    methods: {
      _jumpToCacheRoute: function _jumpToCacheRoute() {
        randomKey = "".concat(Math.random() * 10000000);
        localStorage.setItem(RANDOM_KEY, randomKey);
      }
    }
  };
}

export { getRouteLeaveCache };
