"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useHeaderLinks = useHeaderLinks;
const react_1 = require("react");
const core_1 = require("../core");
const utils_1 = require("../utils");
/** Loads templates for Header links and enriches them with urls from LinksContext. */
function useHeaderLinks(env) {
    var _a, _b;
    const [templates, setTemplates] = (0, react_1.useState)({});
    const [links] = (0, core_1.useLinks)();
    (0, react_1.useEffect)(() => {
        loadHeaderLinks();
        function loadHeaderLinks() {
            return __awaiter(this, void 0, void 0, function* () {
                try {
                    const response = yield fetch(`${core_1.cdnUrls[env]}/common/data/links/header.json`);
                    if (!response.ok)
                        return;
                    const data = yield response.json();
                    if ((0, utils_1.isObject)(data)) {
                        // const mainLinks = data.mainLinks.filter((link: { id: string }) => link.id !== 'dataFabric')
                        setTemplates({ accountSections: data.accountSections, mainLinks: data.mainLinks });
                    }
                }
                catch (_a) {
                    console.error('Failed to retrieve header data.');
                }
            });
        }
    }, [env]);
    const accountSections = (_a = templates.accountSections) === null || _a === void 0 ? void 0 : _a.map(section => {
        var _a;
        return Object.assign(Object.assign({}, section), { links: (_a = section.links) === null || _a === void 0 ? void 0 : _a.map(link => { var _a; return (Object.assign(Object.assign({}, link), { url: links[(_a = link === null || link === void 0 ? void 0 : link.id) !== null && _a !== void 0 ? _a : ''] })); }) });
    });
    const mainLinks = (_b = templates.mainLinks) === null || _b === void 0 ? void 0 : _b.map(link => {
        var _a;
        return (Object.assign(Object.assign({}, link), { url: links[(_a = link === null || link === void 0 ? void 0 : link.id) !== null && _a !== void 0 ? _a : ''] }));
    });
    return { accountSections, mainLinks };
}
//# sourceMappingURL=helpers.js.map