import { cdnUrls } from "../core/consts.js";
import { isObject } from "../utils/assertion.js";
import { useLinks } from "../core/links.js";
import { useEffect, useState } from "react";

//#region src/header/helpers.ts
/** Loads templates for Header links and enriches them with urls from LinksContext. */
function useHeaderLinks(env) {
	const [templates, setTemplates] = useState({});
	const [links] = useLinks();
	useEffect(() => {
		loadHeaderLinks();
		async function loadHeaderLinks() {
			try {
				const response = await fetch(`${cdnUrls[env]}/common/data/links/header.json`);
				if (!response.ok) return;
				const data = await response.json();
				if (isObject(data)) setTemplates({
					accountSections: data.accountSections,
					mainLinks: data.mainLinks
				});
			} catch {
				console.error("Failed to retrieve header data.");
			}
		}
	}, [env]);
	return {
		accountSections: templates.accountSections?.map((section) => {
			return {
				...section,
				links: section.links?.map((link) => ({
					...link,
					url: links[link?.id ?? ""]
				}))
			};
		}),
		mainLinks: templates.mainLinks?.map((link) => ({
			...link,
			url: links[link?.id ?? ""]
		}))
	};
}

//#endregion
export { useHeaderLinks };
globalThis.__vuiVersion__ = "4.0.0-beta.1"
//# sourceMappingURL=helpers.js.map