import type { AppRegistryMeta } from './useAppRegistry/useAppRegistry';
import { appRegistryMetas as appStoreMetas } from '../apps/AppStore';
import { appRegistryMetas as pluginsMetas } from '../apps/Plugins';
import { appRegistryMetas as fileBrowserMetas } from '../apps/FileBrowser';
import { appRegistryMetas as terminalMetas } from '../apps/Terminal';
import { appRegistryMetas as codeEditorMetas } from '../apps/CodeEditor';
import { appRegistryMetas as chatMetas } from '../apps/Chat';
import { appRegistryMetas as dashboardMetas } from '../apps/Dashboards';
import { appRegistryMetas as chatHistoryMetas } from '../apps/ChatHistory';
import { appRegistryMetas as widgetMetas } from '../apps/Widgets';
import { appRegistryMetas as desktopMetas } from '../apps/Desktop';
import { appRegistryMetas as soulseekMetas } from '../apps/Soulseek';
import { appRegistryMetas as photosMetas } from '../apps/Photos';
import { appRegistryMetas as jellyfinMetas } from '../apps/Jellyfin';
import { appRegistryMetas as transmissionMetas } from '../apps/Transmission';
import { appRegistryMetas as giteaMetas } from '../apps/Gitea';
import { appRegistryMetas as invoicesMetas } from '../apps/Invoices';
import { appRegistryMetas as walletMetas } from '../apps/Wallet';
import { appRegistryMetas as monitorMetas } from '../apps/SystemMonitor';
import { appRegistryMetas as qrTransferMetas } from '../apps/QrTransfer';
import { appRegistryMetas as davMetas } from '../apps/Dav';
import type { QueryClient } from '@tanstack/react-query';
import { globalQueryKey } from 'hooks/useGlobal';
import { metasToRegistry } from './useAppRegistry';

export const apps = [
  ...fileBrowserMetas,
  ...terminalMetas,
  ...codeEditorMetas,
  ...chatMetas,
  ...dashboardMetas,
  ...chatHistoryMetas,
  ...widgetMetas,
  ...desktopMetas,
  ...soulseekMetas,
  ...photosMetas,
  ...jellyfinMetas,
  ...transmissionMetas,
  ...giteaMetas,
  ...invoicesMetas,
  ...walletMetas,
  ...monitorMetas,
  ...qrTransferMetas,
  ...davMetas,
  ...appStoreMetas,
  ...pluginsMetas,
];

/**
 * Put the app list in the cache before the first render, rather than seeding it from a component mounted
 * for its side effect.
 *
 * The old `<AppRegistry />` seeded through `useGlobal`'s `initialData`, which only applies to whoever
 * reads the slot first. It therefore worked entirely by sitting above `<App />` in `frontend.tsx`: any
 * `WorkspaceView` that rendered before it created the slot as `{}` and there was no second chance —
 * `initialData` is not a write. Every panel on that screen would have rendered as an empty box, for a
 * reason visible nowhere near the screen that broke.
 *
 * It is a function taking the client rather than a module-scope side effect because the app list imports
 * every panel app, and every panel app imports the Workspace framework. Keeping the call in `frontend.tsx`
 * — the one module that is nobody's dependency — is what keeps that from becoming an import cycle.
 */
/**
 * `pluginPanels` comes from the generated `Plugins.gen.tsx`, passed in rather than imported.
 *
 * The direction matters: this package is a dependency of the shell, so importing a file from
 * `apps/officer-web` would invert that and create a cycle. The shell knows what is installed; this only
 * knows how to register a panel.
 */
export const seedAppRegistry = (queryClient: QueryClient, pluginPanels: AppRegistryMeta[] = []) => {
  queryClient.setQueryData(globalQueryKey('APP_REGISTRY'), metasToRegistry([...apps, ...pluginPanels]));
};
