import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useState } from 'react';
import { isObject, noop, useCallbackRef } from '../utils';
import { cdnUrls } from './consts';
const LinksContext = createContext(undefined);
/**
 * Adds loggedIn=1 parameter to the URL if 'isLoggedIn' flag is true, which means the user is logged in.
 * If 'isLoggedIn' is falsy, loggedin param will be removed.
 * This is supposed to aid login flow when navigating between Veracity applications.
 */
export function getLoggedInLink(link, isLoggedIn) {
    try {
        const url = new URL(link);
        if (isLoggedIn)
            url.searchParams.append('loggedin', '1');
        else
            url.searchParams.delete('loggedin');
        return url.toString();
    }
    catch {
        return link;
    }
}
export function LinksProvider(props) {
    const linksState = useState({});
    return _jsx(LinksContext.Provider, { value: linksState, ...props });
}
/** Returns links and setLinks from LinksContext. */
export function useLinks() {
    const context = useContext(LinksContext);
    if (context === undefined) {
        console.error('useLinks must be used within a LinksContext.');
        return [{}, noop];
    }
    return context;
}
/**
 * Retrieves the links from CDN for the given environment and appends them with loggedin=1 parameter
 * if user is logged in. Links will be reloaded when 'env' changes or updated when 'isLoggedIn' changes.
 */
export function useLoadLinks(env, isLoggedIn) {
    const [, setLinks] = useLinks();
    // Load links from the CDN for the given environment.
    const loadLinks = async (env) => {
        try {
            const response = await fetch(`${cdnUrls[env]}/common/data/links/base.json`);
            if (!response.ok)
                return;
            const data = await response.json();
            if (isObject(data))
                setLinks(updateLoggedInParam(data));
        }
        catch {
            console.error('Failed to retrieve links');
        }
    };
    /**
     * Adds/removes loggedin=1 URL param for each link based on isLoggedIn arg.
     * useCallbackRef is used here to prevent a stale closure in async loadLinks.
     */
    const updateLoggedInParam = useCallbackRef((links) => {
        return Object.keys(links).reduce((loggedInLinks, key) => {
            loggedInLinks[key] = getLoggedInLink(links[key], isLoggedIn);
            return loggedInLinks;
        }, {});
    });
    useEffect(() => {
        if (!env)
            return;
        loadLinks(env);
    }, [env]);
    useEffect(() => {
        setLinks(updateLoggedInParam);
    }, [isLoggedIn]);
}
//# sourceMappingURL=links.js.map