"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.setPrefix = setPrefix;
exports.default = exports.sessionCache = exports.localCache = void 0;

require("core-js/modules/es6.regexp.constructor");

var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));

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

var cacheName = 'gd-components';
var VERSION = '1.0.0';
var CACHE_TIME = 7 * 24 * 60 * 1000;

function changeTime(time) {
  if (typeof time === 'number') return time;

  if (typeof time === 'string' && /([.\d]+)([DdSsHhMm])$/.test(time)) {
    switch (RegExp.$2.toLocaleLowerCase()) {
      case 'd':
        return RegExp.$1 * 24 * 60 * 60 * 1000;

      case 'h':
        return RegExp.$1 * 60 * 60 * 1000;

      case 'm':
        return RegExp.$1 * 60 * 1000;

      case 's':
        return RegExp.$1 * 1000;

      default:
        break;
    }
  }

  console.error(`无效的缓存时间:${time}`); // eslint-disable-line

  throw new Error('无效的时间');
}

function createCache(api) {
  return {
    get(key, config) {
      if (config === void 0) {
        config = {};
      }

      var _config = config,
          _config$version = _config.version,
          version = _config$version === void 0 ? VERSION : _config$version;

      try {
        var root = JSON.parse(api.getItem(cacheName));

        if (root && root.data[key] && root.data[key].version == version) {
          // eslint-disable-line
          if (root.data[key].expires < new Date()) {
            this.remove(key);
            return undefined;
          }

          return root.data[key].data;
        }

        return undefined;
      } catch (error) {
        return undefined;
      }
    },

    set(key, data, config) {
      if (config === void 0) {
        config = {};
      }

      var _config2 = config,
          _config2$version = _config2.version,
          version = _config2$version === void 0 ? VERSION : _config2$version,
          _config2$time = _config2.time,
          time = _config2$time === void 0 ? CACHE_TIME : _config2$time;

      try {
        var d = JSON.parse(api.getItem(cacheName)) || {
          version,
          data: {}
        };
        d.data[key] = {
          version,
          data,
          expires: +new Date() + changeTime(time)
        };
        api.setItem(cacheName, (0, _stringify.default)(d));
        return true;
      } catch (error) {
        if (error.name === 'QuotaExceededError') {
          api.removeItem(cacheName);
          return this.set(key, data, config);
        }

        return false;
      }
    },

    clear() {
      try {
        api.removeItem(cacheName);
        return true;
      } catch (error) {
        return false;
      }
    },

    remove(key) {
      try {
        this.set(key, undefined);
        return true;
      } catch (error) {
        return false;
      }
    }

  };
}

var localStorage = null;
var sessionStorage = null;

if (_constant.isServer) {
  var noop = {
    getItem() {},

    setItem() {},

    removeItem() {}

  };
  localStorage = noop;
  sessionStorage = noop;
} else {
  localStorage = window.localStorage;
  sessionStorage = window.sessionStorage;
}

function setPrefix(name) {
  cacheName = name;
}

var localCache = createCache(localStorage);
exports.localCache = localCache;
var sessionCache = createCache(sessionStorage);
exports.sessionCache = sessionCache;
var _default = {
  setPrefix,
  localCache,
  sessionCache
};
exports.default = _default;