import arches from "arches";

export const fetchRelatableResources = async (
    graphSlug: string,
    nodeAlias: string,
    page: number,
    filterTerm?: string[] | string,
    initialValues?: string[] | null | undefined,
) => {
    const params = new URLSearchParams();

    params.append("page", page.toString());
    if (filterTerm) {
        if (!Array.isArray(filterTerm)) {
            filterTerm = [filterTerm];
        }
        filterTerm.forEach((term) => {
            params.append("filter_term", term);
        });
    }
    initialValues?.forEach((initialValue) => {
        params.append("initialValue", initialValue);
    });
    const response = await fetch(
        `${arches.urls.api_relatable_resources(graphSlug, nodeAlias)}?${params}`,
    );

    const parsed = await response.json();
    if (!response.ok) throw new Error(parsed.message || response.statusText);
    return parsed;
};

export const fetchGraph = async (graphId: string) => {
    const response = await fetch(`${arches.urls.graphs_api}${graphId}`);

    const parsed = await response.json();
    if (!response.ok) throw new Error(parsed.message || response.statusText);
    return parsed;
};
