import { deleteClientCookie, getClientCookie, setClientCookie, } from "./cookies"; export const MARRIAGE_ENTRY_PATH_COOKIE = "HABIB_MARRIAGE_ENTRY_PATH"; const AUTHENTICATED_ENTRY_PATHS = new Set([ "/intro", "/terms", "/questions-list", "/finding-match", "/request-accepted", "/new-match", "/request-sent", ]); export function isAuthenticatedToken(token: string | null | undefined) { return Boolean(token && token !== "NO_TOKEN" && token.trim() !== ""); } export function normalizeCachedMarriageEntryPath( value: string | null | undefined, ) { if (!value) { return null; } let decodedValue: string; try { decodedValue = decodeURIComponent(value); } catch { return null; } return AUTHENTICATED_ENTRY_PATHS.has(decodedValue) ? decodedValue : null; } export function getAuthenticatedCachedEntryPath( token: string | null | undefined, cachedPath: string | null | undefined, ) { if (!isAuthenticatedToken(token)) { return null; } return normalizeCachedMarriageEntryPath(cachedPath); } export function getCachedMarriageEntryPath() { return normalizeCachedMarriageEntryPath( getClientCookie(MARRIAGE_ENTRY_PATH_COOKIE), ); } export function setCachedMarriageEntryPath(path: string | null) { const safePath = normalizeCachedMarriageEntryPath(path); if (!safePath) { deleteClientCookie(MARRIAGE_ENTRY_PATH_COOKIE); if (typeof window !== "undefined" && window.HabibApp?.postMessage) { try { window.HabibApp.postMessage( JSON.stringify({ action: "set_entry_path", data: { path: null } }), ); } catch (e) {} } return; } setClientCookie(MARRIAGE_ENTRY_PATH_COOKIE, safePath); if (typeof window !== "undefined" && window.HabibApp?.postMessage) { try { window.HabibApp.postMessage( JSON.stringify({ action: "set_entry_path", data: { path: safePath } }), ); } catch (e) {} } }