(function () { const apiBaseUrlStorageKey = "tarot-time-api-base-url"; const apiKeyStorageKey = "tarot-time-api-key"; function normalizeBaseUrl(value) { return String(value || "") .trim() .replace(/\/+$/, ""); } function normalizeApiKey(value) { return String(value || "").trim(); } function normalizeConnectionSettings(settings) { return { apiBaseUrl: normalizeBaseUrl(settings?.apiBaseUrl), apiKey: normalizeApiKey(settings?.apiKey) }; } function stripLegacyCredentialParams() { try { const currentUrl = new URL(window.location.href); const hadApiKeyParam = currentUrl.searchParams.has("apiKey") || currentUrl.searchParams.has("api_key"); if (!hadApiKeyParam) { return; } currentUrl.searchParams.delete("apiKey"); currentUrl.searchParams.delete("api_key"); const nextUrl = `${currentUrl.pathname}${currentUrl.search}${currentUrl.hash}`; window.history.replaceState(window.history.state, "", nextUrl); } catch (_error) { } } function persistConnectionSettings(settings) { let didPersist = true; try { const params = new URLSearchParams(window.location.search || ""); const queryValue = String(params.get("apiBaseUrl") || "").trim(); if (queryValue) { window.localStorage.setItem(apiBaseUrlStorageKey, normalizeBaseUrl(queryValue)); } if (settings.apiBaseUrl) { window.localStorage.setItem(apiBaseUrlStorageKey, settings.apiBaseUrl); } else { window.localStorage.removeItem(apiBaseUrlStorageKey); } if (settings.apiKey) { window.localStorage.setItem(apiKeyStorageKey, settings.apiKey); } else { window.localStorage.removeItem(apiKeyStorageKey); } } catch (_error) { didPersist = false; } return didPersist; } function readConfiguredConnectionSettings() { try { const params = new URLSearchParams(window.location.search || ""); const queryValue = String(params.get("apiBaseUrl") || "").trim(); if (queryValue) { window.localStorage.setItem(apiBaseUrlStorageKey, normalizeBaseUrl(queryValue)); } const storedBaseUrl = String(window.localStorage.getItem(apiBaseUrlStorageKey) || "").trim(); const storedApiKey = String(window.localStorage.getItem(apiKeyStorageKey) || "").trim(); return normalizeConnectionSettings({ apiBaseUrl: storedBaseUrl, apiKey: storedApiKey }); } catch (_error) { return normalizeConnectionSettings({ apiBaseUrl: "", apiKey: "" }); } } stripLegacyCredentialParams(); const initialConnectionSettings = readConfiguredConnectionSettings(); window.TarotAppConfig = { ...(window.TarotAppConfig || {}), apiBaseUrl: initialConnectionSettings.apiBaseUrl, apiKey: initialConnectionSettings.apiKey, getApiBaseUrl() { return normalizeBaseUrl(this.apiBaseUrl); }, getApiKey() { return normalizeApiKey(this.apiKey); }, isConnectionConfigured() { return Boolean(this.getApiBaseUrl()); }, getConnectionSettings() { return { apiBaseUrl: this.getApiBaseUrl(), apiKey: this.getApiKey() }; }, updateConnectionSettings(nextSettings = {}) { const previous = this.getConnectionSettings(); const current = normalizeConnectionSettings({ ...previous, ...nextSettings }); const didPersist = persistConnectionSettings(current); this.apiBaseUrl = current.apiBaseUrl; this.apiKey = current.apiKey; if (previous.apiBaseUrl !== current.apiBaseUrl || previous.apiKey !== current.apiKey) { document.dispatchEvent(new CustomEvent("connection:updated", { detail: { previous, current: { ...current } } })); } return { ...current, didPersist }; } }; })();