const require_getStorageAttributes = require('../getStorageAttributes.cjs');
const require_built = require('../config/dist/esm/built.cjs');

//#region ../@intlayer/core/dist/esm/utils/localeStorage.mjs
const buildCookieString = (name, value, attributes) => {
	const parts = [`${name}=${encodeURIComponent(value)}`];
	if (attributes.path) parts.push(`Path=${attributes.path}`);
	if (attributes.domain) parts.push(`Domain=${attributes.domain}`);
	if (attributes.expires instanceof Date) parts.push(`Expires=${attributes.expires.toUTCString()}`);
	if (attributes.secure) parts.push("Secure");
	if (attributes.sameSite) parts.push(`SameSite=${attributes.sameSite}`);
	return parts.join("; ");
};
/**
* Retrieves the locale from various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* The function checks storage locations in order of priority as defined in the configuration.
*
* @returns The locale if found in any storage, or undefined if not found
*/
const getLocaleFromStorage = (options) => {
	const { routing, internationalization } = require_built.built_default;
	const { locales } = internationalization;
	const { storage } = routing;
	if (storage === false || options?.isCookieEnabled === false) return void 0;
	const storageAttributes = require_getStorageAttributes.getStorageAttributes(storage);
	const isValidLocale = (value) => {
		if (!value) return false;
		return locales.includes(value);
	};
	const readCookie = (name) => {
		try {
			const fromOption = options?.getCookie?.(name);
			if (fromOption !== null) return fromOption;
		} catch {}
		try {
			const cookieString = document.cookie ?? "";
			if (!cookieString) return;
			const pairs = cookieString.split(";");
			for (let i = 0; i < pairs.length; i++) {
				const part = pairs[i].trim();
				if (!part) continue;
				const equalIndex = part.indexOf("=");
				if ((equalIndex >= 0 ? part.substring(0, equalIndex) : part) === name) {
					const rawValue = equalIndex >= 0 ? part.substring(equalIndex + 1) : "";
					try {
						return decodeURIComponent(rawValue);
					} catch {
						return rawValue;
					}
				}
			}
		} catch {}
	};
	for (let i = 0; i < storageAttributes.cookies.length; i++) {
		const { name } = storageAttributes.cookies[i];
		const value = readCookie(name);
		if (isValidLocale(value)) return value;
	}
	for (let i = 0; i < storageAttributes.localStorage.length; i++) {
		const { name } = storageAttributes.localStorage[i];
		try {
			const value = options?.getLocaleStorage?.(name);
			if (isValidLocale(value)) return value;
		} catch {}
	}
	for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
		const { name } = storageAttributes.sessionStorage[i];
		try {
			const value = options?.getSessionStorage?.(name);
			if (isValidLocale(value)) return value;
		} catch {}
	}
	for (let i = 0; i < storageAttributes.headers.length; i++) {
		const { name } = storageAttributes.headers[i];
		try {
			const value = options?.getHeader?.(name);
			if (isValidLocale(value)) return value;
		} catch {}
	}
};
/**
* Stores the locale in various storage mechanisms (cookies, localStorage, sessionStorage, headers).
* The function writes to all configured storage locations according to their attributes.
* Respects overwrite flags for localStorage and sessionStorage.
*
* @param locale - The locale to store
*/
const setLocaleInStorage = (locale, options) => {
	if (require_built.built_default.routing.storage === false || options?.isCookieEnabled === false) return;
	const storageAttributes = require_getStorageAttributes.getStorageAttributes(require_built.built_default.routing.storage);
	for (let i = 0; i < storageAttributes.cookies.length; i++) {
		const { name, attributes } = storageAttributes.cookies[i];
		try {
			if (options?.setCookieStore) options?.setCookieStore?.(name, locale, {
				...attributes,
				expires: attributes.expires instanceof Date ? attributes.expires.getTime() : attributes.expires
			});
		} catch {
			try {
				if (options?.setCookieString) {
					const cookieString = buildCookieString(name, locale, attributes);
					options?.setCookieString?.(name, cookieString);
				}
			} catch {}
		}
	}
	if (options?.setLocaleStorage) for (let i = 0; i < storageAttributes.localStorage.length; i++) {
		const { name } = storageAttributes.localStorage[i];
		try {
			if (!(options?.overwrite ?? true) && options?.getLocaleStorage) {
				if (options?.getLocaleStorage?.(name)) continue;
			}
			options?.setLocaleStorage?.(name, locale);
		} catch {}
	}
	if (options?.setSessionStorage) for (let i = 0; i < storageAttributes.sessionStorage.length; i++) {
		const { name } = storageAttributes.sessionStorage[i];
		try {
			if (!(options?.overwrite ?? true) && options?.getSessionStorage) {
				if (options?.getSessionStorage?.(name)) continue;
			}
			options?.setSessionStorage?.(name, locale);
		} catch {}
	}
	if (options?.setHeader) for (let i = 0; i < storageAttributes.headers.length; i++) {
		const { name } = storageAttributes.headers[i];
		try {
			options?.setHeader?.(name, locale);
		} catch {}
	}
};

//#endregion
exports.getLocaleFromStorage = getLocaleFromStorage;
exports.setLocaleInStorage = setLocaleInStorage;
//# sourceMappingURL=localeStorage.cjs.map