"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _DataParse = _interopRequireDefault(require("./DataParse"));

var _IndexDB = _interopRequireDefault(require("./IndexDB"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

// DB 本地数据库
var DB = function () {
  function checkManifest() {
    window.addEventListener('updateready', function (e) {
      console.log('离线缓存状态：' + window.applicationCache.status);

      if (window.applicationCache.status === window.applicationCache.UPDATEREADY) {
        window.applicationCache.swapCache();

        if (window.confirm('发现此manifest文件有更新，是否更新？')) {
          window.location.reload();
        }
      } else {
        console.log('manifest文件没有变化');
      }
    }, false);
  }

  var store = window.localStorage;
  var session = window.sessionStorage;
  return {
    // application cache
    checkManifest: checkManifest,
    // localStorage
    setStore: function setStore(key, val) {
      store.setItem(key.toString(), _DataParse["default"].stringify(val));
      return true;
    },
    getStore: function getStore(key) {
      if (!key || typeof key !== 'string') return null;
      return _DataParse["default"].parse(store.getItem(key));
    },
    getAllStore: function getAllStore() {
      var storages = store.valueOf();
      var allResult = null;

      for (var i = 0; i < storages.length; i++) {
        if (!allResult) allResult = {};
        var key = storages.key(i);
        allResult[key] = _DataParse["default"].parse(storages[key]);
      }

      return allResult;
    },
    removeStore: function removeStore(key) {
      store.removeItem(key);
      return true;
    },
    clearStore: function clearStore() {
      store.clear();
      return true;
    },
    // sessionStorage
    setSession: function setSession(key, value) {
      session.setItem(key.toString(), _DataParse["default"].stringify(value));
      return true;
    },
    getSession: function getSession(key) {
      if (!key || typeof key !== 'string') return null;
      return _DataParse["default"].parse(session.getItem(key));
    },
    getAllSession: function getAllSession() {
      var storages = session.valueOf();
      var allResult = null;

      for (var i = 0; i < storages.length; i++) {
        if (!allResult) allResult = {};
        var key = storages.key(i);
        allResult[key] = _DataParse["default"].parse(storages[key]);
      }

      return allResult;
    },
    removeSession: function removeSession(key) {
      session.removeItem(key);
      return true;
    },
    clearSession: function clearSession() {
      session.clear();
      return true;
    },
    // cookie
    setCookie: function setCookie(key, value, millisecond) {
      if (!key) return false;
      var cookieStr = key + '=' + encodeURIComponent(_DataParse["default"].stringify(value));

      if (!isNaN(millisecond) && Number(millisecond)) {
        var expires = new Date();
        expires.setTime(expires.getTime() + Number(millisecond));
        cookieStr += ';expires=' + expires.toGMTString();
      }

      document.cookie = cookieStr;
      return true;
    },
    getCookie: function getCookie(key) {
      if (!key || typeof key !== 'string') return null;
      var cookiesMap = this.getAllCookie();

      for (var n in cookiesMap) {
        if (n === String(key)) {
          return cookiesMap[n];
        }
      }

      return null;
    },
    getAllCookie: function getAllCookie() {
      var cookiesArr = document.cookie.split('; ');
      var allResult = null; // 遍历cookie数组, 处理每个cookie对
      // eslint-disable-next-line

      for (var i = 0, item; item = cookiesArr[i++];) {
        var itemArr = item.split('=');
        if (!allResult) allResult = {};
        allResult[itemArr[0]] = _DataParse["default"].parse(decodeURIComponent(itemArr[1]));
      }

      return allResult;
    },
    removeCookie: function removeCookie(key) {
      var expires = new Date();
      expires.setTime(expires.getTime() - 1);
      var val = this.getCookie(key);

      if (val != null) {
        document.cookie = key + '=' + val + ';expires=' + expires.toGMTString();
      }

      return true;
    },
    clearCookie: function clearCookie() {
      var cookiesMap = this.getAllCookie();

      for (var n in cookiesMap) {
        this.removeCookie(n);
      }

      return true;
    },
    // IndexDB
    indexedDB: _IndexDB["default"].indexedDB,
    setIndexDB: _IndexDB["default"].set,
    getIndexDB: _IndexDB["default"].get,
    getAllIndexDB: _IndexDB["default"].getAll,
    removeIndexDB: _IndexDB["default"].remove,
    clearIndexDB: _IndexDB["default"].clear
  };
}();

var _default = DB;
exports["default"] = _default;
module.exports = exports.default;