import { type Ref, ref } from "vue";
import { FileInfo } from "./fileMgr";
import { Permission } from "./peer";
import { Modal } from "ant-design-vue";

export const formatSize = (size: number): string => {
  if (size < 1024) return `${size} B`;
  if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`;
  return `${(size / (1024 * 1024)).toFixed(2)} MB`;
};

export let localCurrentFile: Ref<FileInfo> = ref(null);
export let remoteCurrentFile: Ref<FileInfo> = ref(null);
export const stringToBase64 = (str: string) => {
  //汉字转base64
  return btoa(encodeURIComponent(str));
};
export const base64ToString = (base64: string) => {
  //base64转汉字
  return decodeURIComponent(atob(base64));
};
export const confirmWin = (
  title: string,
  content: string,
  okText: string,
  cancelText: string,
): Promise<boolean> => {
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title,
      content,
      okText,
      cancelText,
      async onOk() {
        resolve(true);
      },
      onCancel() {
        reject(false);
      },
    });
  });
};
const commonCharsCN =
  "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多点行学法所民得经十三之进着等部度家电力里如水化高自二理起小物现实加量都两体制机当西从广业本去把性好应开它合还因由其些然前外天政四日那社义事平形相全表间样与关各重新线内数正心反你明看原又么利比或但质气第向道命此变条只没结解问意建月公无系军很情者最立代想已通并提直题党程展五果料象员革位入常文总次品式活设及管特件长求老头基资边流路级少图山统接知较将组见计别她手角期根论运农指几九区强放决皮被干做必战先回则任取据处队南给色光门即保治北造百规热领七海口东导器压志世金增争济阶油思术极交受联什认六共权收证改清己美再采转更单风切打白教速花带安场身车例真务具万每目至达走积示议声报斗完类八离华名确才科张信马节话米整空元况今集温传土许步群广石记需段研界拉林律叫且究观越织装影算低持音众书布复容儿须际商非验连断深难近矿千周委素技备半办青省列习响约支般史感劳便团往酸历市克何除消构府称太准精值号率族维划选标写存候毛亲快效斯院查江型眼王按格养易置派层片始却专状育厂京识适属圆包火住调满县局照参红细引听该冯价严龙飞";
const commonCharsEN = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
export const randomChars = (num: number = 3, chars: string = commonCharsEN) => {
  const result = [];
  for (let i = 0; i < num; i++) {
    const randomIndex = Math.floor(Math.random() * chars.length);
    result.push(chars[randomIndex]);
  }
  return result.join("");
};
export const sign2peerid = (sign: string) => {
  sign = sign.replace(/[^a-zA-Z0-9]/g, "");
  sign = sign + "+kuraa.cc+explorer";
  return stringToBase64(sign).slice(0, 32);
};
//获取url query
export const getUrlParam = (key: string) => {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == key) {
      return pair[1];
    }
  }
  return null;
};
// 权限设置
let permissionSet: { [key in Permission]: boolean } = null;
export function getPermissionSet(): { [key in Permission]: boolean } {
  if (!permissionSet) {
    permissionSet = {
      edit: false,
      view: true,
      download: true,
      desktop: true,
      call: true,
      camera: true,
      ...(getCache("permissionSet") || {}),
    };
  }
  return permissionSet;
}
export function setPermissionSet(set: { [key in Permission]: boolean }) {
  cacheIt("permissionSet", set);
  permissionSet = set;
}
//缓存
export function cacheIt(key: string, value: any) {
  localStorage.setItem(key, JSON.stringify(value));
}
export function getCache(key: string) {
  return JSON.parse(localStorage.getItem(key) || null);
}
