{"version":3,"file":"parseContentDeclarationFileName.mjs","names":[],"sources":["../../../src/utils/parseContentDeclarationFileName.ts"],"sourcesContent":["import { basename } from 'node:path';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\nexport type ContentDeclarationFileName = {\n  /** Dictionary key derived from the file name, locale suffix excluded */\n  key: string;\n  /** Locale declared as a file name suffix, e.g. `about.fr.content.md` -> `fr` */\n  locale?: LocalesValues;\n};\n\n/** Fallback used when the file name matches no configured content extension */\nconst CONTENT_EXTENSION_PATTERN = /\\.content\\.[^.]+$/;\n\n/**\n * Splits a content declaration file name into its dictionary key and its\n * optional locale suffix.\n *\n * @example\n * parseContentDeclarationFileName('src/about.content.ts', configuration)\n * // { key: 'about' }\n *\n * parseContentDeclarationFileName('src/about.fr.content.md', configuration)\n * // { key: 'about', locale: 'fr' }\n */\nexport const parseContentDeclarationFileName = (\n  filePath: string,\n  configuration: IntlayerConfig\n): ContentDeclarationFileName => {\n  const fileName = basename(filePath);\n\n  // Longest match first so `.content.mdx` wins over a hypothetical `.mdx`\n  const matchedExtension = configuration.content.fileExtensions\n    .filter((extension) => fileName.endsWith(extension))\n    .sort((a, b) => b.length - a.length)[0];\n\n  const nameWithoutExtension = matchedExtension\n    ? fileName.slice(0, -matchedExtension.length)\n    : fileName.replace(CONTENT_EXTENSION_PATTERN, '');\n\n  const lastSegmentIndex = nameWithoutExtension.lastIndexOf('.');\n\n  // A name made of a single segment is the key itself, never a locale\n  if (lastSegmentIndex <= 0) return { key: nameWithoutExtension };\n\n  const lastSegment = nameWithoutExtension.slice(lastSegmentIndex + 1);\n  const matchedLocale = configuration.internationalization.locales.find(\n    (locale) => locale.toLowerCase() === lastSegment.toLowerCase()\n  );\n\n  if (!matchedLocale) return { key: nameWithoutExtension };\n\n  return {\n    key: nameWithoutExtension.slice(0, lastSegmentIndex),\n    locale: matchedLocale,\n  };\n};\n"],"mappings":";;;;AAYA,MAAM,4BAA4B;;;;;;;;;;;;AAalC,MAAa,mCACX,UACA,kBAC+B;CAC/B,MAAM,WAAW,SAAS,QAAQ;CAGlC,MAAM,mBAAmB,cAAc,QAAQ,eAC5C,QAAQ,cAAc,SAAS,SAAS,SAAS,CAAC,CAAC,CACnD,MAAM,GAAG,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;CAEvC,MAAM,uBAAuB,mBACzB,SAAS,MAAM,GAAG,CAAC,iBAAiB,MAAM,IAC1C,SAAS,QAAQ,2BAA2B,EAAE;CAElD,MAAM,mBAAmB,qBAAqB,YAAY,GAAG;CAG7D,IAAI,oBAAoB,GAAG,OAAO,EAAE,KAAK,qBAAqB;CAE9D,MAAM,cAAc,qBAAqB,MAAM,mBAAmB,CAAC;CACnE,MAAM,gBAAgB,cAAc,qBAAqB,QAAQ,MAC9D,WAAW,OAAO,YAAY,MAAM,YAAY,YAAY,CAC/D;CAEA,IAAI,CAAC,eAAe,OAAO,EAAE,KAAK,qBAAqB;CAEvD,OAAO;EACL,KAAK,qBAAqB,MAAM,GAAG,gBAAgB;EACnD,QAAQ;CACV;AACF"}