import { RouterProvider } from 'react-router';
import createLoader from '@ablogcms/react-router/list-view/create-loader';
import createRouter from '@ablogcms/react-router/list-view/create-router';
import { AcmsContextProvider } from '@stores/acms';
import Announce from '@ablogcms/components/announce';
import ModuleAdmin from '../components/module-admin/module-admin';
import { preloadModules } from '../hooks/use-modules-swr';

const loader = createLoader({
  id: 'module-admin',
  admin: 'module_index',
  onLoad: ({ context, url }) => {
    preloadModules(context, url.searchParams);
  },
  ignoredParams: ['rid'],
});

const router = createRouter({
  contextProvider: AcmsContextProvider,
  element: <ModuleAdmin />,
  errorElement: (
    <Announce
      title={ACMS.i18n('module_index.error.unknown.title')}
      message={ACMS.i18n('module_index.error.unknown.message')}
    />
  ),
  loader,
});

function ModuleAdminRoot() {
  return <RouterProvider router={router} />;
}

export default ModuleAdminRoot;
