'use client'

import { getCookie, setCookie, deleteCookie } from 'cookies-next'
import { decrypt, encrypt } from './Crypto'

export const zustandCookieStorage = {
    getItem: (name: string) => {
        if (typeof window === 'undefined') return null
        const value = getCookie(name)
        if (!value) return null
        try {
            return JSON.parse(decrypt(value as string))
        } catch {
            return null
        }
    },

    setItem: (name: string, value: any) => {
        const encrypted = encrypt(JSON.stringify(value))
        setCookie(name, encrypted, {
            maxAge: 60 * 60 * 24 * 7, // 7 days
            path: '/'
        })
    },

    removeItem: (name: string) => {
        deleteCookie(name)
    }
}
