import useSWR from 'swr';
import { fetchClient } from '@ablogcms/fetch-client';
import { AcmsContext } from '@ablogcms/url/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 };
}
