import { ensure } from './commonUtil';
import { formatDate } from './dateUtil';

var debug = require('debug')('worker:store');
/** 持久化渠道 */


var CHANNEL = window.localStorage;
/** session storage */

export var session = window.sessionStorage;
/**
 * 设置临时缓存 window
 * @param {string} key 键
 * @param {any} value 值
 * @param {boolean} temp 是否是临时缓存 默认 true
 */

export var setCache = function setCache(key, value) {
  var temp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;

  var _key = "_cache".concat(key);

  window[_key] = value;
  /** 临时数据清单 用于清空 */

  if (temp) {
    if (window._cache_list) {
      window._cache_list.add(_key);
    } else {
      window._cache_list = new Set([_key]);
    }
  }
};
/**
 * 清除缓存 window
 * @param {string?} key 键，为空时清除所有临时缓存
 */

export var clearCache = function clearCache(key) {
  if (key) {
    var _key = "_cache".concat(key);

    Reflect.deleteProperty(window, _key);
  } else if (window._cache_list) {
    window._cache_list.forEach(function (k) {
      Reflect.deleteProperty(window, k);
    });

    window._cache_list = null;
  }
};
/**
 * 获取缓存 window
 * @param {*} key 键
 */

export var getCache = function getCache(key) {
  return window["_cache".concat(key)] || null;
};
/**
 * 获取缓存数据 localStorage
 * @param {*} key
 * @param {*} isJson 是否是json 默认是
 */

export var getStore = function getStore(key) {
  var channel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : CHANNEL;
  return ensure(function () {
    return JSON.parse(channel.getItem(key));
  }, channel.getItem(key));
};
/**
 * 设置缓存 localStorage
 * @param {string} key 键
 * @param {any} data 值
 */

export var setStore = function setStore(key, data) {
  var channel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : CHANNEL;

  if (data instanceof Object) {
    channel.setItem(key, JSON.stringify(data));
  } else {
    channel.setItem(key, data);
  }
};
/**
 * 清空 localStorage
 * @param {string} key 键
 */

export var clearStore = function clearStore(key) {
  var channel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : CHANNEL;

  if (key) {
    channel.removeItem(key);
  } else {
    channel.clear();
  }
};
/**
 * 获取缓存数据 sessionStorage
 * @param {*} key
 * @param {*} isJson 是否是json 默认是
 */

export var getSessionStore = function getSessionStore(key) {
  return getStore(key, window.sessionStorage);
};
/**
 * 设置缓存 sessionStorage
 * @param {string} key 键
 * @param {any} data 值
 */

export var setSessionStore = function setSessionStore(key, data) {
  setStore(key, data, window.sessionStorage);
};
/**
 * 清空 sessionStorage
 * @param {*} key 键
 */

export var clearSessionStore = function clearSessionStore(key) {
  clearStore(key, window.sessionStorage);
};
/**
 * 持久化方法
 * @param {string} _key 键
 * @param {any} state 值
 * @param {array<string>} whiteList 白名单
 */

export var persistence = function persistence(_key, state) {
  var whiteList = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

  /* 持久化白名单 */
  var persistenceData = {};
  whiteList.forEach(function (key) {
    persistenceData[key] = state[key];
  });
  setStore(_key, persistenceData);
};
/** 设计器定制的 一套缓存机制 */

/**
 * 获取缓存数据 localStorage
 * @param {*} key
 * @param {*} isJson 是否是json 默认是
 */
// export const getStore = key => ensure(() => JSON.parse(CHANNEL.getItem(key)), CHANNEL.getItem(key));

var localStorageKey = 'magicBoxCache';
/** 清楚缓存 */

export var clearMagicStore = function clearMagicStore() {
  clearStore(localStorageKey);
};
/**
 * 设置缓存 localStorage
 * @param {string} key 键
 * @param {any} data 值
 */

export var setMagicStore = function setMagicStore(key, data) {
  try {
    var localStore = getStore(localStorageKey) || {
      date: formatDate(),
      data: {}
    };
    localStore.data[key] = data;
    setStore(localStorageKey, localStore);
  } catch (error) {
    debug(error);
    clearMagicStore();
  }
};
/** 获取设计器缓存 */

export var getMagicStore = function getMagicStore(key) {
  try {
    var localStore = getStore(localStorageKey) || {
      date: formatDate(),
      data: {}
    };

    if (localStore.date !== formatDate()) {
      throw new Error('清除设计器缓存');
    }

    return localStore.data[key];
  } catch (error) {
    debug(error);
    clearMagicStore();
  }

  return null;
};