/** 配置 */
interface Options {
    /** key前缀 */
    prefix?: string;
}
/** 默认配置 */
const defaultOptions: Options = {
    prefix: ""
};

class CookieUtils {

    private readonly prefix: string;

    constructor(options: Options = defaultOptions) {
        const { prefix } = options;
        this.prefix = prefix ?? "";
    }

    /**
     * @description: 设置cookie
     * @param {string} key 键
     * @param {any} value 值
     * @param {number} expires 过期时间s小时，不传默认1年有效
     */
    public setItem(key: string, value: string | number, expires?: number): void {
        const timestamp = Date.now();
        if (typeof expires === "number") {
            expires = timestamp + expires * 60 * 60 * 1000;
        } else {
            expires = timestamp + 365 * 24 * 60 * 60 * 1000;
        }
        document.cookie = `${this.prefix}${key}=${value};expires=${new Date(expires).toUTCString()}`;
    }

    /**
     * @description: 获取cookie
     * @param {string} key 键
     */
    public getItem(key: string): string | undefined {
        const cookies = document.cookie.split("; ");
        let val: string | undefined = undefined;
        cookies.find((item) => {
            const [k, v] = item.split("=");
            if (k  ===  `${this.prefix}${key}`) {
                val = v;
                return true;
            }
            return false;
        });

        return val;
    }

    /**
     * @description: 删除指定key的cookie
     * @param {string} key 键
     * @Date: 2023-05-17 18:32:56
     * @Author: mulingyuer
     */
    public removeItem(key: string): void {
        this.setItem(key, "", -1);
    }

    /**
     * @description: 清空所有cookie
     * @Date: 2023-05-17 23:13:04
     * @Author: mulingyuer
     */
    public clear(): void {
        const cookies = document.cookie.split("; ");
        cookies.forEach((item) => {
            const [k] = item.split("=");
            this.removeItem(k);
        });
    }
}

const cookieStorage = new CookieUtils();

export default cookieStorage;
export { CookieUtils };