{"version":3,"file":"useFormSchema.mjs","sources":["../../../lib/data-provider-interface/composables/useFormSchema.ts"],"sourcesContent":["import { createSharedComposable } from '@vueuse/core'\nimport { computed, MaybeRefOrGetter, ref, toValue, watch } from 'vue'\nimport { useDpiContext } from './useDpiContext'\nimport { useI18n } from 'vue-i18n'\nimport translate from '../utils/translation-helper'\n\nconst useFormSchemaGlobal = createSharedComposable(() => {\n  const state = ref({\n    schema: {\n      datasets: {},\n      distributions: {},\n      catalogues: {}\n    },\n    usersCatalogs: {}\n  })\n\n  /**\n   * Retrieves the schema for a given property.\n   *\n   * @param property - The property whose schema should be retrieved.\n   *                  Can be a string or a ref to a string.\n   * @returns A computed ref to the schema of the given property.\n   */\n  const getSchema = (property: MaybeRefOrGetter<string>) => computed(() => {\n    const _property = toValue(property)\n\n    if (property === 'catalogues' || property === 'datasets' || property === 'distributions') {\n      return state.value.schema[_property as keyof typeof state.value.schema]\n    }\n\n    return undefined\n  })\n\n  return { state, getSchema }\n})\n\nexport interface UseFormSchemaOptions {\n  t?: (key: string) => string\n  te?: (key: string) => boolean\n}\n\nexport function useFormSchema(options?: UseFormSchemaOptions) {\n  const dpiContext = useDpiContext()\n  const { t, te } = options || useI18n({ useScope: 'global' })\n  const { state, getSchema } = useFormSchemaGlobal()\n\n  const dpiConfig = computed(() => dpiContext.value.specification)\n\n  function createSchema({ property, page }: { property: string, page: string }) {\n    const pageProperties = dpiConfig.value.pageConent?.[property as keyof typeof dpiConfig.value.pageConent][page] as unknown as string[]\n    const propertyDefinitions = dpiConfig.value.inputDefinition?.[property as keyof typeof dpiConfig.value.inputDefinition]\n\n    // Create copy of state because extractSchema does in-place modifications.\n    // Just to be safe.\n    const stateCopy = JSON.parse(JSON.stringify(state.value))\n    extractSchema({ state: stateCopy, pageProperties, propertyDefinitions, property, page })\n\n    state.value = stateCopy\n  }\n\n  function extractSchema({\n    state,\n    pageProperties,\n    propertyDefinitions,\n    property,\n    page,\n  }: {\n    state: any\n    pageProperties: string[]\n    propertyDefinitions: any\n    property: string\n    page: string\n  }) {\n    // important: create new empty schema each time so already existing schema will be overwritten on route/view-change\n    const newSchema = [];\n\n    for (let index = 0; index < pageProperties.length; index += 1) {\n      const propertyKey = pageProperties[index];\n      try {\n        newSchema.push(propertyDefinitions[propertyKey]);\n      } catch (err) {\n        console.warn(\n          `DCATAP doens't include a property called: ${propertyKey}`\n        );\n      }\n    }\n\n    state.schema[property][page] = newSchema;\n  }\n\n  function translateSchema({ property, page }: { property: string, page: string }) {\n    const schemaCopy = { ...state.value.schema }\n    // @ts-ignore\n    translate(schemaCopy[property][page], property, t, te);\n\n    // Update the global state with the new schema\n    // @ts-ignore\n    state.value.schema[property][page] = schemaCopy[property][page]\n  }\n\n  return { createSchema, translateSchema, getSchema }\n\n}"],"names":["useFormSchemaGlobal","createSharedComposable","state","ref","property","computed","_property","toValue","useFormSchema","options","dpiContext","useDpiContext","t","te","useI18n","getSchema","dpiConfig","createSchema","page","pageProperties","_a","propertyDefinitions","_b","stateCopy","extractSchema","newSchema","index","propertyKey","translateSchema","schemaCopy","translate"],"mappings":";;;;;AAMA,MAAMA,IAAsBC,EAAuB,MAAM;AACvD,QAAMC,IAAQC,EAAI;AAAA,IAChB,QAAQ;AAAA,MACN,UAAU,CAAC;AAAA,MACX,eAAe,CAAC;AAAA,MAChB,YAAY,CAAC;AAAA,IACf;AAAA,IACA,eAAe,CAAC;AAAA,EAAA,CACjB;AAmBM,SAAA,EAAE,OAAAD,GAAO,WAVE,CAACE,MAAuCC,EAAS,MAAM;AACjE,UAAAC,IAAYC,EAAQH,CAAQ;AAElC,QAAIA,MAAa,gBAAgBA,MAAa,cAAcA,MAAa;AAChE,aAAAF,EAAM,MAAM,OAAOI,CAA4C;AAAA,EAGjE,CACR;AAGH,CAAC;AAOM,SAASE,EAAcC,GAAgC;AAC5D,QAAMC,IAAaC,KACb,EAAE,GAAAC,GAAG,IAAAC,MAAOJ,KAAWK,EAAQ,EAAE,UAAU,SAAA,CAAU,GACrD,EA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