/**
 * window.localStorage
 * @method set 设置
 * @method get 获取
 * @method remove 移除
 * @method clear 移除全部
 */
export const Local = {
    set(key: string, val: any) {
        window.localStorage.setItem(key, JSON.stringify(val))
    },
    get(key: string) {
        const json: any = window.localStorage.getItem(key)
        return JSON.parse(json)
    },
    remove(key: string) {
        window.localStorage.removeItem(key)
    },
    clear() {
        // 退出登录时保留表格自定义列、Vue DevTools 等配置
        const preserveKeys = [
            'VXE_CUSTOM_STORE',
            '__VUE_DEVTOOLS_NEXT_PLUGIN_SETTINGS__dev.esm.pinia__',
        ]
        const preserved: Record<string, string | null> = {}
        preserveKeys.forEach(key => {
            preserved[key] = window.localStorage.getItem(key)
        })
        window.localStorage.clear()
        preserveKeys.forEach(key => {
            if (preserved[key] !== null) {
                window.localStorage.setItem(key, preserved[key] as string)
            }
        })
    },
}

/**
 * window.sessionStorage
 * @method set 设置会话缓存
 * @method get 获取会话缓存
 * @method remove 移除会话缓存
 * @method clear 移除全部会话缓存
 */
export const Session = {
    set(key: string, val: any) {
        window.sessionStorage.setItem(key, JSON.stringify(val))
    },
    get(key: string) {
        const json: any = window.sessionStorage.getItem(key)
        return JSON.parse(json)
    },
    remove(key: string) {
        window.sessionStorage.removeItem(key)
    },
    clear() {
        window.sessionStorage.clear()
    },
}
