import useSWR from 'swr';
import { fetchClient } from '../../../lib/fetch-client';
import { AcmsContext } from '../../../lib/acmsPath/types';

async function fetcher(url: string): Promise<string[]> {
  const { data } = await fetchClient.get(url);
  return Array.isArray(data) ? data : [];
}

export default function useEntryTags(acmsContext: AcmsContext) {
  const url = ACMS.Library.acmsLink(
    {
      ...acmsContext,
      tpl: 'ajax/entry-tags.json',
    },
    { ajaxCacheBusting: false }
  );

  const { data = [], isLoading } = useSWR(url, fetcher);

  return { tags: data, isLoading };
}
