"use strict";
/*
 * @adonisjs/http-server
 *
 * (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.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CookieClient = void 0;
const PlainCookie = __importStar(require("../Drivers/Plain"));
const SignedCookie = __importStar(require("../Drivers/Signed"));
const EncryptedCookie = __importStar(require("../Drivers/Encrypted"));
/**
 * Cookie client exposes the API to parse/set AdonisJS
 * cookies as a client.
 */
class CookieClient {
    constructor(encryption) {
        this.encryption = encryption;
    }
    /**
     * Encrypt a key value pair to be sent in the cookie header
     */
    encrypt(key, value) {
        return EncryptedCookie.pack(key, value, this.encryption);
    }
    /**
     * Sign a key value pair to be sent in the cookie header
     */
    sign(key, value) {
        return SignedCookie.pack(key, value, this.encryption);
    }
    /**
     * Encode a key value pair to be sent in the cookie header
     */
    encode(_, value) {
        return PlainCookie.pack(value);
    }
    /**
     * Unsign a signed cookie value
     */
    unsign(key, value) {
        return SignedCookie.canUnpack(value) ? SignedCookie.unpack(key, value, this.encryption) : null;
    }
    /**
     * Decrypt an encrypted cookie value
     */
    decrypt(key, value) {
        return EncryptedCookie.canUnpack(value)
            ? EncryptedCookie.unpack(key, value, this.encryption)
            : null;
    }
    /**
     * Decode an encoded cookie value
     */
    decode(_, value) {
        return PlainCookie.canUnpack(value) ? PlainCookie.unpack(value) : null;
    }
    /**
     * Parse response cookie
     */
    parse(key, value) {
        /**
         * Unsign signed cookie
         */
        if (SignedCookie.canUnpack(value)) {
            return SignedCookie.unpack(key, value, this.encryption);
        }
        /**
         * Decrypted encrypted cookie
         */
        if (EncryptedCookie.canUnpack(value)) {
            return EncryptedCookie.unpack(key, value, this.encryption);
        }
        /**
         * Decode encoded cookie
         */
        if (PlainCookie.canUnpack(value)) {
            return PlainCookie.unpack(value);
        }
    }
}
exports.CookieClient = CookieClient;
