{"version":3,"file":"index.cjs","names":["exists","join","v","colorizePath","PROXY_TEMPLATE","MIDDLEWARE_TEMPLATE","ensureDirectory","writeFileToRoot","x","readFileFromRoot","detectNextAppDir","isVersionAtLeast","colorize","ANSIColors","restructureAppIntoLocale","ROOT_LAYOUT_TEMPLATE_TS","ROOT_LAYOUT_TEMPLATE_JS","findAppFile","wrapLayoutWithProvider","LOCALE_LAYOUT_TEMPLATE_TS","LOCALE_LAYOUT_TEMPLATE_JS","LOCALE_PAGE_TEMPLATE_TS","LOCALE_PAGE_TEMPLATE_JS"],"sources":["../../../../../src/init/frameworkSetup/nextAppRouter/index.ts"],"sourcesContent":["import { join } from 'node:path';\nimport * as ANSIColors from '@intlayer/config/colors';\nimport { colorize, colorizePath, logger, v, x } from '@intlayer/config/logger';\nimport {\n  ensureDirectory,\n  exists,\n  readFileFromRoot,\n  writeFileToRoot,\n} from '../../utils/fileSystem';\nimport type { FrameworkAdapter, FrameworkSetupContext } from '../types';\nimport { detectNextAppDir, findAppFile, isVersionAtLeast } from './detect';\nimport { restructureAppIntoLocale } from './restructure';\nimport {\n  LOCALE_LAYOUT_TEMPLATE_JS,\n  LOCALE_LAYOUT_TEMPLATE_TS,\n  LOCALE_PAGE_TEMPLATE_JS,\n  LOCALE_PAGE_TEMPLATE_TS,\n  MIDDLEWARE_TEMPLATE,\n  PROXY_TEMPLATE,\n  ROOT_LAYOUT_TEMPLATE_JS,\n  ROOT_LAYOUT_TEMPLATE_TS,\n} from './templates';\nimport { type TransformResult, wrapLayoutWithProvider } from './transforms';\n\n/**\n * Creates the locale-detection proxy (Next >= 16) or middleware (Next < 16),\n * but only when neither already exists, so a user's custom file is never\n * touched. Placed next to the app directory's parent (`src/` when present).\n */\nconst ensureProxyOrMiddleware = async (\n  rootDir: string,\n  srcDir: string,\n  isNext16: boolean\n): Promise<void> => {\n  const baseDir = srcDir || '.';\n  const proxyCandidates = [\n    'proxy.ts',\n    'proxy.js',\n    'middleware.ts',\n    'middleware.js',\n  ];\n\n  for (const candidate of proxyCandidates) {\n    if (await exists(rootDir, join(baseDir, candidate))) {\n      logger(\n        `${v} ${colorizePath(join(baseDir, candidate))} already exists, leaving it untouched`\n      );\n      return;\n    }\n  }\n\n  const fileName = isNext16 ? 'proxy.ts' : 'middleware.ts';\n  const content = isNext16 ? PROXY_TEMPLATE : MIDDLEWARE_TEMPLATE;\n  const targetPath = join(baseDir, fileName);\n\n  if (srcDir) await ensureDirectory(rootDir, srcDir);\n  await writeFileToRoot(rootDir, targetPath, content);\n  logger(`${v} Created ${colorizePath(targetPath)} for locale detection`);\n};\n\n/** Logs the outcome of a layout provider-wrap transform. */\nconst logTransformOutcome = (\n  filePath: string,\n  result: TransformResult\n): void => {\n  switch (result.status) {\n    case 'wrapped':\n      logger(`${v} Added Intlayer provider to ${colorizePath(filePath)}`);\n      break;\n    case 'already':\n      logger(\n        `${v} ${colorizePath(filePath)} already wraps the Intlayer provider`\n      );\n      break;\n    case 'skipped-client':\n      logger(\n        `${x} ${colorizePath(filePath)} is a client component — add the Intlayer provider manually.`,\n        { level: 'warn' }\n      );\n      break;\n    case 'skipped':\n      logger(\n        `${x} Could not safely add the Intlayer provider to ${colorizePath(filePath)} — please wire it manually.`,\n        { level: 'warn' }\n      );\n      break;\n  }\n};\n\n/** Transforms an existing file in place, writing it back only when changed. */\nconst transformExistingFile = async (\n  rootDir: string,\n  filePath: string,\n  transform: (code: string) => TransformResult\n): Promise<void> => {\n  const code = await readFileFromRoot(rootDir, filePath);\n  const result = transform(code);\n  if (result.status === 'wrapped') {\n    await writeFileToRoot(rootDir, filePath, result.code);\n  }\n  logTransformOutcome(filePath, result);\n};\n\n/** Creates a file from a template only when it does not already exist. */\nconst createIfMissing = async (\n  rootDir: string,\n  filePath: string,\n  content: string,\n  description: string\n): Promise<boolean> => {\n  if (await exists(rootDir, filePath)) return false;\n  await writeFileToRoot(rootDir, filePath, content);\n  logger(`${v} Created ${description} ${colorizePath(filePath)}`);\n  return true;\n};\n\n/**\n * Next.js App Router adapter. Scaffolds the locale proxy/middleware, restructures\n * the app into a `[locale]` segment, and wraps the layout with the unified\n * Intlayer provider — all idempotently and without overwriting recognizable\n * user code. Pages are never wrapped: `IntlayerProvider` in the locale layout\n * covers them.\n */\nexport const nextAppRouterAdapter: FrameworkAdapter = {\n  name: 'Next.js App Router',\n\n  detect: async ({ rootDir, allDeps }) => {\n    if (!allDeps.next) return false;\n    return (await detectNextAppDir(rootDir)) !== null;\n  },\n\n  setup: async ({ rootDir, allDeps, useTypeScript }: FrameworkSetupContext) => {\n    const appDirInfo = await detectNextAppDir(rootDir);\n    if (!appDirInfo) return;\n\n    const { appDir, srcDir } = appDirInfo;\n    const isNext16 = isVersionAtLeast(allDeps.next, 16);\n    const scriptExtension = useTypeScript ? 'tsx' : 'jsx';\n\n    logger(\n      colorize('Setting up Next.js App Router integration...', ANSIColors.CYAN)\n    );\n\n    // 1. Locale detection proxy / middleware (create only when absent).\n    await ensureProxyOrMiddleware(rootDir, srcDir, isNext16);\n\n    // 2. Move routable files under a `[locale]` segment (idempotent).\n    const restructureResult = await restructureAppIntoLocale(rootDir, appDir);\n    if (restructureResult.status === 'moved') {\n      logger(\n        `${v} Restructured app routes under ${colorizePath(join(appDir, '[locale]'))}: ${restructureResult.movedEntries\n          .map((entry) => colorize(entry, ANSIColors.MAGENTA))\n          .join(', ')}`\n      );\n    } else if (restructureResult.status === 'already-structured') {\n      logger(\n        `${v} ${colorizePath(join(appDir, restructureResult.localeSegment))} already exists, skipping restructure`\n      );\n    }\n\n    // When the app already uses a different locale-prefix scheme (e.g. an\n    // `[...locale]` / `[[...locale]]` catch-all), respect the user's routing:\n    // don't scaffold a second, conflicting `[locale]` segment.\n    if (\n      restructureResult.status === 'already-structured' &&\n      restructureResult.localeSegment !== '[locale]'\n    ) {\n      logger(\n        `${v} Detected existing locale routing (${colorize(restructureResult.localeSegment, ANSIColors.MAGENTA)}), skipping locale layout/page scaffold`\n      );\n      return;\n    }\n\n    const localeDir = join(appDir, '[locale]');\n    await ensureDirectory(rootDir, localeDir);\n\n    // 3. Minimal pass-through root layout (the `[locale]` layout owns `<html>`).\n    await createIfMissing(\n      rootDir,\n      join(appDir, `layout.${scriptExtension}`),\n      useTypeScript ? ROOT_LAYOUT_TEMPLATE_TS : ROOT_LAYOUT_TEMPLATE_JS,\n      'root layout'\n    );\n\n    // 4. Locale layout — transform an existing one, else scaffold from template.\n    const existingLayout = await findAppFile(rootDir, localeDir, 'layout');\n    if (existingLayout) {\n      await transformExistingFile(\n        rootDir,\n        existingLayout,\n        wrapLayoutWithProvider\n      );\n    } else {\n      await createIfMissing(\n        rootDir,\n        join(localeDir, `layout.${scriptExtension}`),\n        useTypeScript ? LOCALE_LAYOUT_TEMPLATE_TS : LOCALE_LAYOUT_TEMPLATE_JS,\n        'locale layout'\n      );\n    }\n\n    // 5. Locale page — only scaffolded when absent. An existing page needs no\n    // transform: the locale layout's `IntlayerProvider` already covers it.\n    const existingPage = await findAppFile(rootDir, localeDir, 'page');\n    if (!existingPage) {\n      await createIfMissing(\n        rootDir,\n        join(localeDir, `page.${scriptExtension}`),\n        useTypeScript ? LOCALE_PAGE_TEMPLATE_TS : LOCALE_PAGE_TEMPLATE_JS,\n        'locale page'\n      );\n    }\n  },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA6BA,MAAM,0BAA0B,OAC9B,SACA,QACA,aACkB;CAClB,MAAM,UAAU,UAAU;CAQ1B,KAAK,MAAM,aAAa;EANtB;EACA;EACA;EACA;CAGoC,GACpC,IAAI,MAAMA,qCAAO,aAASC,gBAAK,SAAS,SAAS,CAAC,GAAG;EACnD,oCACE,GAAGC,0BAAE,OAAGC,0CAAaF,gBAAK,SAAS,SAAS,CAAC,EAAE,sCACjD;EACA;CACF;CAGF,MAAM,WAAW,WAAW,aAAa;CACzC,MAAM,UAAU,WAAWG,qEAAiBC;CAC5C,MAAM,iBAAaJ,gBAAK,SAAS,QAAQ;CAEzC,IAAI,QAAQ,MAAMK,8CAAgB,SA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