{"version":3,"file":"extractKeyAndLocaleFromPath.mjs","names":[],"sources":["../../../src/syncPluginKit/extractKeyAndLocaleFromPath.ts"],"sourcesContent":["import type { Locale } from '@intlayer/types/allLocales';\n\nconst escapeRegex = (text: string) =>\n  text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n/**\n * Extract the dictionary key and locale of a file path by matching it against\n * a mask pattern where `{{__KEY__}}` and `{{__LOCALE__}}` mark the positions\n * of the key and locale segments.\n *\n * Returns `null` when the path does not match the mask, or when the mask\n * contains no placeholder at all (no named capture group can be produced).\n * Missing placeholders fall back to `'index'` for the key and to\n * `defaultLocale` for the locale.\n */\nexport const extractKeyAndLocaleFromPath = (\n  filePath: string,\n  maskPattern: string,\n  locales: Locale[],\n  defaultLocale: Locale\n): { key: string; locale: Locale } | null => {\n  const keyPlaceholder = '{{__KEY__}}';\n  const localePlaceholder = '{{__LOCALE__}}';\n\n  // fast-glob strips leading \"./\" from returned paths; normalize both sides\n  const normalize = (path: string) =>\n    path.startsWith('./') ? path.slice(2) : path;\n\n  const normalizedFilePath = normalize(filePath);\n  const normalizedMask = normalize(maskPattern);\n\n  const localesAlternation = locales.join('|');\n\n  // Escape special regex chars, then convert glob wildcards to regex equivalents.\n  // Must replace ** before * to avoid double-replacing.\n  let regexString = `^${escapeRegex(normalizedMask)}$`;\n  regexString = regexString.replace(/\\\\\\*\\\\\\*/g, '.*'); // ** → match any path segments\n  regexString = regexString.replace(/\\\\\\*/g, '[^/]*'); // * → match within a single segment\n\n  regexString = regexString.replace(\n    escapeRegex(localePlaceholder),\n    `(?<locale>${localesAlternation})`\n  );\n\n  if (normalizedMask.includes(keyPlaceholder)) {\n    regexString = regexString.replace(\n      escapeRegex(keyPlaceholder),\n      '(?<key>.+)'\n    );\n  }\n\n  const maskRegex = new RegExp(regexString);\n  const match = maskRegex.exec(normalizedFilePath);\n\n  if (!match?.groups) {\n    return null;\n  }\n\n  const key = (match.groups.key as string | undefined) ?? 'index';\n  const locale = (match.groups.locale as Locale | undefined) ?? defaultLocale;\n\n  return { key, locale };\n};\n"],"mappings":";AAEA,MAAM,eAAe,SACnB,KAAK,QAAQ,uBAAuB,MAAM;;;;;;;;;;;AAY5C,MAAa,+BACX,UACA,aACA,SACA,kBAC2C;CAC3C,MAAM,iBAAiB;CACvB,MAAM,oBAAoB;CAG1B,MAAM,aAAa,SACjB,KAAK,WAAW,IAAI,IAAI,KAAK,MAAM,CAAC,IAAI;CAE1C,MAAM,qBAAqB,UAAU,QAAQ;CAC7C,MAAM,iBAAiB,UAAU,WAAW;CAE5C,MAAM,qBAAqB,QAAQ,KAAK,GAAG;CAI3C,IAAI,cAAc,IAAI,YAAY,cAAc,EAAE;CAClD,cAAc,YAAY,QAAQ,aAAa,IAAI;CACnD,cAAc,YAAY,QAAQ,SAAS,OAAO;CAElD,cAAc,YAAY,QACxB,YAAY,iBAAiB,GAC7B,aAAa,mBAAmB,EAClC;CAEA,IAAI,eAAe,SAAS,cAAc,GACxC,cAAc,YAAY,QACxB,YAAY,cAAc,GAC1B,YACF;CAIF,MAAM,QAAQ,IADQ,OAAO,WACP,CAAC,CAAC,KAAK,kBAAkB;CAE/C,IAAI,CAAC,OAAO,QACV,OAAO;CAMT,OAAO;EAAE,KAHI,MAAM,OAAO,OAA8B;EAG1C,QAFE,MAAM,OAAO,UAAiC;CAEzC;AACvB"}