/*
 * @Author: jixiang
 * @Date: 2022-06-16 16:43:47
 * @Description:
 * @FilePath: /pc/src/utils/store.ts
 * One World One Drefeel(56580223@qq.com)
 */
import { Base64 } from 'js-base64';
export const encodeStroeKey = (n) => {
  return (
    (function (e) {
      for (var t = e.length.toString(), n = 10 - t.length; 0 < n; n--) t = '0' + t;
      return t;
    })(n) +
    (function (e) {
      for (var t = Base64.encode(e).split(''), n = 0; n < Math.floor(e.length / 100 + 1); n++)
        t.splice(100 * n + 1, 0, 3);
      return t.join('');
    })(n)
  );
};
export const decodeStroeKey = (e) => {
  try {
    for (
      var t = Number(e.substr(0, 10)), n = e.substr(10).split(''), i = 0, s = 0;
      s < Math.floor(t / 100) + 1;
      s++
    ) {
      n.splice(100 * s + 1 - i, 1);
      i++;
    }
    const o = Base64.decode(n.join(''));
    return o;
  } catch (error) {
    return e;
  }
};
/**
 * 判断是否为空
 */
export function validatenull(val) {
  if (typeof val === 'boolean') {
    return false;
  }
  if (typeof val === 'number') {
    return false;
  }
  if (val instanceof Array) {
    if (val.length === 0) return true;
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === '{}') return true;
  } else {
    if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '')
      return true;
    return false;
  }
  return false;
}
/**
 * 日期格式化
 */
export function dateFormat(date) {
  let format = 'yyyy-MM-dd hh:mm:ss';
  if (date !== 'Invalid Date') {
    const o = {
      'M+': date.getMonth() + 1, // month
      'd+': date.getDate(), // day
      'h+': date.getHours(), // hour
      'm+': date.getMinutes(), // minute
      's+': date.getSeconds(), // second
      'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
      S: date.getMilliseconds(), // millisecond
    };
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
        );
      }
    }
    return format;
  }
  return '';
}
/**
 * 存储localStorage
 */
export const setStore = (params) => {
  let { name, content, type } = params;
  //   if (
  //     website.specialKey !== undefined &&
  //     website.specialKey[name] !== undefined
  //   ) {
  //     name = website.specialKey[name] + '-' + name
  //   } else {
  //     name = keyName + name
  //   }
  name = encodeStroeKey(`wisdragon-${name}`);

  const obj = {
    dataType: typeof content,
    content: content,
    datetime: dateFormat(new Date()),
  };
  if (type) window.sessionStorage.setItem(name, encodeStroeKey(JSON.stringify(obj)));
  else window.localStorage.setItem(name, encodeStroeKey(JSON.stringify(obj)));
};
/**
 *
 * 获取localStorage
 */
export const getStore = (params) => {
  let { name, debug } = params;
  name = encodeStroeKey(`wisdragon-${name}`);
  console.log(name, '--name---');
  let obj, content;
  obj = window.sessionStorage.getItem(name);
  if (validatenull(obj)) obj = window.localStorage.getItem(name);
  if (validatenull(obj)) return;
  obj = decodeStroeKey(obj);
  try {
    obj = JSON.parse(obj);
  } catch {
    return obj;
  }
  if (debug) {
    return obj;
  }
  if (obj?.dataType == 'string') {
    content = obj.content;
  } else if (obj.dataType == 'number') {
    content = Number(obj.content);
  } else if (obj.dataType == 'boolean') {
    content = eval(obj.content);
  } else if (obj.dataType == 'object') {
    content = obj.content;
  }
  return content;
};
