import useSWR, { preload } from 'swr';
import { fetchClient } from '@ablogcms/fetch-client';
import { AcmsContext } from '@ablogcms/url/types';
import { ModulesResponse } from '../types';

async function fetcher(url: string): Promise<ModulesResponse> {
  const { data } = await fetchClient.get(url);

  const { modules = [], bulkActions = [] } = data;

  return {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    modules: modules.map((module: any) => ({
      ...module,
      created_datetime: module.created_datetime ? new Date(module.created_datetime) : null,
      updated_datetime: module.updated_datetime ? new Date(module.updated_datetime) : null,
      actions: module.actions.map((action: { id: string }) => action.id),
    })),
    bulkActions,
  };
}

function getCashKey(acmsContext: AcmsContext, searchParams: URLSearchParams) {
  return ACMS.Library.acmsLink(
    {
      ...acmsContext,
      tpl: 'ajax/modules.json',
      searchParams,
    },
    { ajaxCacheBusting: false }
  );
}

export function preloadModules(acmsContext: AcmsContext, searchParams: URLSearchParams) {
  return preload(getCashKey(acmsContext, searchParams), fetcher);
}

export default function useModulesSWR(acmsContext: AcmsContext, searchParams: URLSearchParams) {
  const { data, error, isLoading, mutate } = useSWR(getCashKey(acmsContext, searchParams), fetcher, {
    keepPreviousData: true,
  });

  const invalidate = async () => {
    await mutate();
  };

  return {
    data,
    isLoading,
    error,
    invalidate,
    mutate,
  };
}
