"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CookieHelper = void 0;
class CookieHelper {
    static parseCookies(req) {
        const cookieHeader = req.headers.cookie;
        if (!cookieHeader) {
            return {};
        }
        const cookies = {};
        cookieHeader.split(';').forEach(cookie => {
            const [name, ...rest] = cookie.split('=');
            const value = rest.join('=').trim();
            if (name) {
                cookies[name.trim()] = decodeURIComponent(value);
            }
        });
        return cookies;
    }
    static get(req, name) {
        const cookies = this.parseCookies(req);
        return cookies[name];
    }
    static set(res, name, value, options) {
        const defaultOptions = {
            httpOnly: true,
            secure: process.env.NODE_ENV === 'production',
            sameSite: 'lax',
            path: '/',
        };
        const finalOptions = { ...defaultOptions, ...options };
        res.cookie(name, value, finalOptions);
    }
    static update(res, name, value, options) {
        this.set(res, name, value, options);
    }
    static delete(res, name, options) {
        const deleteOptions = {
            ...options,
            expires: new Date(0),
            maxAge: 0,
        };
        res.clearCookie(name, deleteOptions);
    }
    static exists(req, name) {
        return this.get(req, name) !== undefined;
    }
    static getAll(req) {
        return this.parseCookies(req);
    }
    static setMultiple(res, cookies) {
        Object.entries(cookies).forEach(([name, { value, options }]) => {
            this.set(res, name, value, options);
        });
    }
    static deleteMultiple(res, names, options) {
        names.forEach(name => {
            this.delete(res, name, options);
        });
    }
}
exports.CookieHelper = CookieHelper;
//# sourceMappingURL=cookie.helper.js.map