import { useEffect, useState } from 'react';
import { cdnUrls, useLinks } from '../core';
import { isObject } from '../utils';
/** Loads templates for Header links and enriches them with urls from LinksContext. */
export 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)) {
                    // const mainLinks = data.mainLinks.filter((link: { id: string }) => link.id !== 'dataFabric')
                    setTemplates({ accountSections: data.accountSections, mainLinks: data.mainLinks });
                }
            }
            catch {
                console.error('Failed to retrieve header data.');
            }
        }
    }, [env]);
    const accountSections = templates.accountSections?.map(section => {
        return { ...section, links: section.links?.map(link => ({ ...link, url: links[link?.id ?? ''] })) };
    });
    const mainLinks = templates.mainLinks?.map(link => ({
        ...link,
        url: links[link?.id ?? ''],
    }));
    return { accountSections, mainLinks };
}
//# sourceMappingURL=helpers.js.map