import handleErrorResponses from '../httpErrorResponseHandler';
import useSWR, { mutate, SWRConfiguration } from 'swr';
import { useState, useEffect } from 'react';
import { formatApiPath } from 'utils/formatPath';

const useUiBootstrap = (options: SWRConfiguration = {}) => {
    // The point of the bootstrap is to get multiple datasets in one call. Therefore,
    // this needs to be refactored to seed other hooks with the correct data.
    const BOOTSTRAP_CACHE_KEY = `api/admin/ui-bootstrap`;

    const fetcher = () => {
        const path = formatApiPath(`api/admin/ui-bootstrap`);

        return fetch(path, {
            method: 'GET',
            credentials: 'include',
        })
            .then(handleErrorResponses('ui bootstrap'))
            .then(res => res.json());
    };

    const { data, error } = useSWR(BOOTSTRAP_CACHE_KEY, fetcher, options);
    const [loading, setLoading] = useState(!error && !data);

    const refetchUiBootstrap = () => {
        mutate(BOOTSTRAP_CACHE_KEY);
    };

    useEffect(() => {
        setLoading(!error && !data);
    }, [data, error]);

    return {
        bootstrap: data,
        error,
        loading,
        refetchUiBootstrap,
    };
};

export default useUiBootstrap;
