"use strict";
/*
 * @adonisjs/session
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CookieDriver = void 0;
/**
 * Cookie driver utilizes the encrypted HTTP cookies to write session value.
 */
class CookieDriver {
    constructor(config, ctx) {
        this.config = config;
        this.ctx = ctx;
    }
    /**
     * Read session value from the cookie
     */
    read(sessionId) {
        const cookieValue = this.ctx.request.encryptedCookie(sessionId);
        if (typeof cookieValue !== 'object') {
            return null;
        }
        return cookieValue;
    }
    /**
     * Write session values to the cookie
     */
    write(sessionId, values) {
        if (typeof values !== 'object') {
            throw new Error('Session cookie driver expects an object of values');
        }
        this.ctx.response.encryptedCookie(sessionId, values, this.config.cookie);
    }
    /**
     * Removes the session cookie
     */
    destroy(sessionId) {
        if (this.ctx.request.cookiesList()[sessionId]) {
            this.ctx.response.clearCookie(sessionId);
        }
    }
    /**
     * Updates the cookie with existing cookie values
     */
    touch(sessionId) {
        const value = this.read(sessionId);
        if (!value) {
            return;
        }
        this.write(sessionId, value);
    }
}
exports.CookieDriver = CookieDriver;
