import { t as debug_default } from "./debug-cDi3H0Pi.js";
import { decoratorsCollection } from "@japa/browser-client";
function normalizeCookieOptions(defaultDomain, options) {
	return Object.assign({
		domain: defaultDomain,
		path: "/"
	}, options);
}
function tryDecode(value) {
	try {
		return value.indexOf("%") !== -1 ? decodeURIComponent(value) : value;
	} catch {
		return value;
	}
}
function extendBrowserClient(cookieClient, baseURL) {
	debug_default("extending @japa/browser-client with adonisjs specific methods");
	if (!baseURL) {
		const HOST = process.env.HOST;
		const PORT = process.env.PORT;
		if (HOST && PORT) baseURL = `http://${HOST}:${PORT}`;
	}
	const cookiesDomain = baseURL ? new URL(baseURL).host : void 0;
	decoratorsCollection.register({ context(context) {
		context.getCookie = async function(key) {
			const cookie = (await this.cookies()).find(({ name }) => name === key);
			if (!cookie) return null;
			return cookieClient.unsign(key, tryDecode(cookie.value));
		};
		context.getEncryptedCookie = async function(key) {
			const cookie = (await this.cookies()).find(({ name }) => name === key);
			if (!cookie) return null;
			return cookieClient.decrypt(key, tryDecode(cookie.value));
		};
		context.getPlainCookie = async function(key) {
			const cookie = (await this.cookies()).find(({ name }) => name === key);
			if (!cookie) return null;
			return cookieClient.decode(key, tryDecode(cookie.value));
		};
		context.setCookie = async function(key, value, options) {
			const signedValue = cookieClient.sign(key, value);
			if (signedValue) await this.addCookies([{
				name: key,
				value: signedValue,
				...normalizeCookieOptions(cookiesDomain, options)
			}]);
		};
		context.setEncryptedCookie = async function(key, value, options) {
			const encryptedValue = cookieClient.encrypt(key, value);
			if (encryptedValue) await this.addCookies([{
				name: key,
				value: encryptedValue,
				...normalizeCookieOptions(cookiesDomain, options)
			}]);
		};
		context.setPlainCookie = async function(key, value, options) {
			const plainCookie = cookieClient.encode(key, value);
			if (plainCookie) await this.addCookies([{
				name: key,
				value: plainCookie,
				...normalizeCookieOptions(cookiesDomain, options)
			}]);
		};
	} });
}
export { extendBrowserClient };
