{"version":3,"file":"fieldUtils.mjs","sources":["../../../../admin/src/preview/utils/fieldUtils.ts"],"sourcesContent":["import { type FieldContentSourceMap } from '@strapi/admin/strapi-admin';\n\nimport type { PREVIEW_ERROR_MESSAGES } from './constants';\nimport type { PreviewContextValue } from '../pages/Preview';\nimport type { Modules, Schema, Struct, UID } from '@strapi/types';\n\ntype PreviewErrorMessage = keyof typeof PREVIEW_ERROR_MESSAGES;\n\n// Generic error class for preview field operations\nexport class PreviewFieldError extends Error {\n  public readonly messageKey: PreviewErrorMessage;\n\n  constructor(messageKey: PreviewErrorMessage) {\n    super(messageKey);\n    this.name = 'PreviewFieldError';\n    this.messageKey = messageKey;\n  }\n}\n\ntype PathPart = { name: string; index?: number };\n\n// Helper function to parse path with array indices and return clean attribute names\nexport const parsePathWithIndices = (path: string): PathPart[] => {\n  // Split by dots, then parse array indices from each part. For example:\n  // input \"components.4.field.relations.2.name\"\n  // output [{name: \"components\", index: 4}, {name: \"field\"}, {name: \"relations\", index: 2}, {name: \"name\"}]\n  return path\n    .split('.')\n    .map((part) => {\n      const numericIndex = parseInt(part, 10);\n      if (!isNaN(numericIndex) && part === numericIndex.toString()) {\n        // This part is a pure numeric index, return it as an index for the previous part\n        return { name: '', index: numericIndex };\n      }\n      return { name: part };\n    })\n    .reduce((acc: PathPart[], part) => {\n      if (part.name === '' && part.index !== undefined) {\n        // This is an index, attach it to the previous part\n        if (acc.length > 0) {\n          acc[acc.length - 1].index = part.index;\n        }\n      } else {\n        acc.push(part);\n      }\n      return acc;\n    }, []);\n};\n\nexport function getAttributeSchemaFromPath({\n  path,\n  schema,\n  components,\n  document,\n}: {\n  path: string;\n  schema: PreviewContextValue['schema'] | PreviewContextValue['components'][string];\n  components: PreviewContextValue['components'];\n  document: Modules.Documents.AnyDocument;\n}): Schema.Attribute.AnyAttribute {\n  /**\n   * Create the function that will be recursively called.\n   * We don't do recursion on getAttributeSchemaFromPath itself because:\n   * - it takes a path string, not the parsed array that's better for recursion\n   * - even when several levels deep, we still need access to the root schema and components\n   */\n  const visitor = (\n    currentPathParts: PathPart[],\n    currentAttributes: Schema.Attributes,\n    currentData: any\n  ): Schema.Attribute.AnyAttribute => {\n    const [currentPart, ...remainingParts] = currentPathParts;\n\n    // Get the data and schema for the current path\n    const currentAttribute = currentAttributes[currentPart.name];\n\n    if (!currentAttribute) {\n      throw new PreviewFieldError('INVALID_FIELD_PATH');\n    }\n\n    if (currentAttribute.type === 'relation') {\n      throw new PreviewFieldError('RELATIONS_NOT_HANDLED');\n    }\n\n    if (currentAttribute.type === 'component') {\n      const componentAttributes = components[currentAttribute.component].attributes;\n      if (currentAttribute.repeatable) {\n        // We must have the index, otherwise we don't know what data to use\n        if (currentPart.index === undefined) {\n          throw new PreviewFieldError('INVALID_FIELD_PATH');\n        }\n        return visitor(\n          remainingParts,\n          componentAttributes,\n          currentData[currentPart.name][currentPart.index]\n        );\n      }\n\n      // Non repeatable component\n      return visitor(remainingParts, componentAttributes, currentData[currentPart.name]);\n    }\n\n    if (currentAttribute.type === 'dynamiczone') {\n      // We must have the index, otherwise we don't know what component we're dealing with\n      if (currentPart.index === undefined) {\n        throw new PreviewFieldError('INVALID_FIELD_PATH');\n      }\n\n      const componentData = currentData[currentPart.name][currentPart.index];\n      const componentAttributes = components[componentData.__component].attributes;\n      return visitor(remainingParts, componentAttributes, componentData);\n    }\n\n    // Plain regular field. It ends the recursion\n    return currentAttributes[currentPart.name];\n  };\n\n  return visitor(parsePathWithIndices(path), schema.attributes, document);\n}\n\nexport function parseFieldMetaData(strapiSource: string): FieldContentSourceMap | null {\n  const searchParams = new URLSearchParams(strapiSource);\n  const path = searchParams.get('path');\n  const type = searchParams.get('type');\n  const documentId = searchParams.get('documentId');\n  const locale = searchParams.get('locale');\n  const model = searchParams.get('model');\n  const kind = searchParams.get('kind');\n\n  if (!path || !type || !documentId || !model) {\n    return null;\n  }\n\n  return {\n    path,\n    type: type as Schema.Attribute.AnyAttribute['type'],\n    documentId,\n    locale: locale ?? null,\n    model: model as UID.Schema | undefined,\n    kind: kind ? (kind as Struct.ContentTypeKind) : undefined,\n  };\n}\n"],"names":["PreviewFieldError","Error","messageKey","name","parsePathWithIndices","path","split","map","part","numericIndex","parseInt","isNaN","toString","index","reduce","acc","undefined","length","push","getAttributeSchemaFromPath","schema","components","document","visitor","currentPathParts","currentAttributes","currentData","currentPart","remainingParts","currentAttribute","type","componentAttributes","component","attributes","repeatable","componentData","__component","parseFieldMetaData","strapiSource","searchParams","URLSearchParams","get","documentId","locale","model","kind"],"mappings":"AAQA;AACO,MAAMA,iBAAAA,SAA0BC,KAAAA,CAAAA;AAGrC,IAAA,WAAA,CAAYC,UAA+B,CAAE;AAC3C,QAAA,KAAK,CAACA,UAAAA,CAAAA;QACN,IAAI,CAACC,IAAI,GAAG,mBAAA;QACZ,IAAI,CAACD,UAAU,GAAGA,UAAAA;AACpB,IAAA;AACF;AAIA;AACO,MAAME,uBAAuB,CAACC,IAAAA,GAAAA;;;;AAInC,IAAA,OAAOA,KACJC,KAAK,CAAC,GAAA,CAAA,CACNC,GAAG,CAAC,CAACC,IAAAA,GAAAA;QACJ,MAAMC,YAAAA,GAAeC,SAASF,IAAAA,EAAM,EAAA,CAAA;AACpC,QAAA,IAAI,CAACG,KAAAA,CAAMF,YAAAA,CAAAA,IAAiBD,IAAAA,KAASC,YAAAA,CAAaG,QAAQ,EAAA,EAAI;;YAE5D,OAAO;gBAAET,IAAAA,EAAM,EAAA;gBAAIU,KAAAA,EAAOJ;AAAa,aAAA;AACzC,QAAA;QACA,OAAO;YAAEN,IAAAA,EAAMK;AAAK,SAAA;IACtB,CAAA,CAAA,CACCM,MAAM,CAAC,CAACC,GAAAA,EAAiBP,IAAAA,GAAAA;AACxB,QAAA,IAAIA,KAAKL,IAAI,KAAK,MAAMK,IAAAA,CAAKK,KAAK,KAAKG,SAAAA,EAAW;;YAEhD,IAAID,GAAAA,CAAIE,MAAM,GAAG,CAAA,EAAG;gBAClBF,GAAG,CAACA,IAAIE,MAAM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