import Cookies from "js-cookie";

export const authUtilsCookieEffectorStorageAdapter = (
    opts?: {
        expiresDays?: number; 
        path?:string;
        sameSite?: "lax" | "strict" | "none";
        secure?: boolean
    }
) => <T>(key:string) => ({
    get(): T | undefined {
        const raw = Cookies.get(key);
        if (raw == null) return undefined;
        try {
            return JSON.parse(raw) as T;
        } catch {
            return raw as unknown as T
        }
    },
    set(value:T) {
        Cookies.set(key, JSON.stringify(value), {
            expires: opts?.expiresDays ?? 30,
            path: opts?.path ?? "/",
            sameSiate: opts?.sameSite ?? "lax"
        })
    }
})