{"version":3,"file":"schemas.mjs","sources":["../../../admin/src/utils/schemas.ts"],"sourcesContent":["import omit from 'lodash/omit';\n\nimport { LOCALIZED_FIELDS, doesPluginOptionsHaveI18nLocalized } from './fields';\n\nimport type { Schema } from '@strapi/types';\n\n/* -------------------------------------------------------------------------------------------------\n * mutateCTBContentTypeSchema\n * -----------------------------------------------------------------------------------------------*/\n\n// TODO: refactor for CTB refactors\nconst mutateCTBContentTypeSchema = (\n  nextSchema: {\n    pluginOptions: Schema.ContentType['pluginOptions'];\n    attributes: Schema.Attribute.AnyAttribute[];\n    uid?: string;\n  },\n  prevSchema?: {\n    pluginOptions: Schema.ContentType['pluginOptions'];\n    attributes: Schema.Attribute.AnyAttribute[];\n    uid?: string;\n  }\n) => {\n  if (!prevSchema) {\n    return nextSchema;\n  }\n\n  // Don't perform mutations components\n  if (!doesPluginOptionsHaveI18nLocalized(nextSchema.pluginOptions)) {\n    return nextSchema;\n  }\n\n  const isNextSchemaLocalized = nextSchema.pluginOptions.i18n.localized;\n  const isPrevSchemaLocalized = doesPluginOptionsHaveI18nLocalized(prevSchema?.pluginOptions)\n    ? prevSchema?.pluginOptions.i18n.localized\n    : false;\n\n  // No need to perform modification on the schema, if the i18n feature was not changed\n  // at the ct level\n  if (isNextSchemaLocalized && isPrevSchemaLocalized) {\n    return nextSchema;\n  }\n\n  if (isNextSchemaLocalized) {\n    const attributes = addLocalisationToFields(nextSchema.attributes);\n\n    return {\n      ...nextSchema,\n      attributes,\n    };\n  }\n\n  // Remove the i18n object from the pluginOptions\n  if (!isNextSchemaLocalized) {\n    const pluginOptions = omit(nextSchema.pluginOptions, 'i18n');\n    const attributes = disableAttributesLocalisation(nextSchema.attributes);\n\n    return {\n      ...nextSchema,\n      pluginOptions,\n      attributes,\n    };\n  }\n\n  return nextSchema;\n};\n\n/* -------------------------------------------------------------------------------------------------\n * addLocalisationToFields\n * -----------------------------------------------------------------------------------------------*/\n\nconst addLocalisationToFields = (attributes: Schema.Attribute.AnyAttribute[]) => {\n  return attributes.map((currentAttribute) => {\n    if (LOCALIZED_FIELDS.includes(currentAttribute.type)) {\n      const i18n = { localized: true };\n\n      const pluginOptions = currentAttribute.pluginOptions\n        ? { ...currentAttribute.pluginOptions, i18n }\n        : { i18n };\n\n      return { ...currentAttribute, pluginOptions };\n    }\n\n    return currentAttribute;\n  });\n};\n\n/* -------------------------------------------------------------------------------------------------\n * disableAttributesLocalisation\n * -----------------------------------------------------------------------------------------------*/\n\nconst disableAttributesLocalisation = (attributes: Schema.Attribute.AnyAttribute[]) => {\n  return attributes.map((currentAttribute) => {\n    return omit(currentAttribute, 'pluginOptions.i18n');\n  });\n};\n\nexport { mutateCTBContentTypeSchema };\n"],"names":["mutateCTBContentTypeSchema","nextSchema","prevSchema","doesPluginOptionsHaveI18nLocalized","pluginOptions","isNextSchemaLocalized","i18n","localized","isPrevSchemaLocalized","attributes","addLocalisationToFields","omit","disableAttributesLocalisation","map","currentAttribute","LOCALIZED_FIELDS","includes","type"],"mappings":";;;AAMA;;AAEkG;AAGlG,MAAMA,0BAAAA,GAA6B,CACjCC,UAAAA,EAKAC,UAAAA,GAAAA;AAMA,IAAA,IAAI,CAACA,UAAAA,EAAY;QACf,OAAOD,UAAAA;AACT,IAAA;;AAGA,IAAA,IAAI,CAACE,kCAAAA,CAAmCF,UAAAA,CAAWG,aAAa,CAAA,EAAG;QACjE,OAAOH,UAAAA;AACT,IAAA;AAEA,IAAA,MAAMI,wBAAwBJ,UAAAA,CAAWG,aAAa,CAACE,IAAI,CAACC,SAAS;AACrE,IAAA,MAAMC,wBAAwBL,kCAAAA,CAAmCD,UAAAA,EAAYE,iBACzEF,UAAAA,EAAYE,aAAAA,CAAcE,KAAKC,SAAAA,GAC/B,KAAA;;;AAIJ,IAAA,IAAIF,yBAAyBG,qBAAAA,EAAuB;QAClD,OAAOP,UAAAA;AACT,IAAA;AAEA,IAAA,IAAII,qBAAAA,EAAuB;QACzB,MAAMI,UAAAA,GAAaC,uBAAAA,CAAwBT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