type RuntimeConfig = {
	appName: string;
	site: string;
	server: string;
	appVersion: string;
	umamiHost: string;
	umamiWebsiteId: string;
	timezone: string;
};

declare global {
	interface Window {
		LST_CONFIG?: RuntimeConfig;
		umami?: {
			track: (eventName: string, eventData?: Record<string, unknown>) => void;
		};
	}
}

export const runtimeConfig: RuntimeConfig = {
	appName: window.LST_CONFIG?.appName ?? "LST",
	site: window.LST_CONFIG?.site ?? "unknown",
	server: window.LST_CONFIG?.server ?? "unknown",
	appVersion: window.LST_CONFIG?.appVersion ?? "dev",
	umamiHost: window.LST_CONFIG?.umamiHost ?? "",
	umamiWebsiteId: window.LST_CONFIG?.umamiWebsiteId ?? "",
	timezone: window.LST_CONFIG?.timezone ?? "America/Chicago",
};

export function loadUmami() {
	if (!runtimeConfig.umamiHost) return;
	if (!runtimeConfig.umamiWebsiteId) return;
	if (document.querySelector("script[data-website-id]")) return;

	const script = document.createElement("script");
	script.defer = true;
	script.src = `${runtimeConfig.umamiHost}/script.js`;
	script.setAttribute("data-website-id", runtimeConfig.umamiWebsiteId);

	document.head.appendChild(script);
}

export function trackLstEvent(
	eventName: string,
	eventData?: Record<string, unknown>,
) {
	window.umami?.track(eventName, {
		app: runtimeConfig.appName,
		site: runtimeConfig.site,
		server: runtimeConfig.server,
		appVersion: runtimeConfig.appVersion,
		...eventData,
	});
}

/*

event type

		trackLstEvent("exampleClick", {
			module: "example",
			action: "test_click",
			label: "Example Button",
			page: window.location.pathname,
		});

*/
