import type { PageAst } from "@wrnexus/syntax";
import { createComponentContract } from "./component-contract.ts";
import { generateBrowserModule } from "./client-codegen.ts";
import { generateServerFunctionsModule, rpcManifest } from "./server-codegen.ts";
import { generateDeclarations } from "./type-codegen.ts";
import { generateStoreBrowserModule, generateStoreModule } from "./store-codegen.ts";

export interface CompileTargets {
  server: string;
  browser: string;
  declarations: string;
  contract: ReturnType<typeof createComponentContract>;
  rpc: ReturnType<typeof rpcManifest>;
}

export function generateTargets(ast: PageAst): CompileTargets {
  return {
    server:
      ast.kind === "global-store" || ast.kind === "page-store"
        ? generateStoreModule(ast)
        : generateServerFunctionsModule(ast),
    browser:
      ast.kind === "global-store" || ast.kind === "page-store"
        ? generateStoreBrowserModule(ast)
        : generateBrowserModule(ast),
    declarations: generateDeclarations(ast),
    contract: createComponentContract(ast),
    rpc: rpcManifest(ast),
  };
}
