{"version":3,"file":"data-getters.mjs","sources":["../../../lib/services/getters/data-getters.ts"],"sourcesContent":["// @ts-nocheck\n/* eslint-disable padded-blocks */\nimport {\n  has, isNil, isArray, isString, isNumber, isObject,\n} from 'lodash';\n\n/**\n * Converts an array of strings or an array of objects ({key, default} pairs)\n * to an object with the array's elements as keys initialized to a default value\n */\nfunction convertKeyArrayToObject(keys) {\n  if (!keys) return undefined;\n\n  return keys.reduce((map, obj) => {\n    const key = obj.key || obj;\n    const defaultValue = obj.default || undefined;\n    map[key] = defaultValue; // eslint-disable-line\n    return map;\n  }, {});\n}\n\nconst getters = {\n  /*\n  ***** GENERIC METHODS TO TRANSFORM DATA MODEL *****\n  */\n  getString: (parent, property) => {\n    // Initialize empty string\n    let string;\n\n    // Check if property exists and if it is a non-empty string\n    if (has(parent, property) && isString(parent[property]) && !isNil(parent[property])) string = parent[property];\n\n    // Return the value or an empty string\n    return string;\n  },\n  getNumber: (parent, property) => {\n    // Initialize empty string\n    let number;\n\n    // Check if property exists and if it is a non-empty number\n    if (has(parent, property) && isNumber(parent[property]) && !isNil(parent[property])) number = parent[property];\n\n    // Return the value or an empty number\n    return number;\n  },\n  getObject: (parent, property, keys) => {\n    // Initialize default object\n    // The initial object contains all keys set to undefined,\n    // matching the behavior from v1.2.4 for backward compatibility\n    const object = convertKeyArrayToObject(keys) || {};\n\n    // Check if property exists and if it is an object\n    if (has(parent, property) && isObject(parent[property])) {\n      // For each key in the object\n      // keys can be of shape ['key1', 'key2', ...]\n      // or [{key: 'key1', default: undefined}, {key: 'key2', default: undefined}, ...]\n      // make sure to cleanly iterate through all key strings.\n      for (const key of keys.map(k => k.key || k)) {\n\n        // Check if property.key exists and if it is not null\n        if (has(parent[property], key) && !isNil(parent[property][key])) object[key] = parent[property][key];\n      }\n    }\n\n    // Return the value or an empty object\n    return object;\n  },\n  getObjectLanguage: (parent, property, placeholder?) => {\n    // Declare object\n    let object;\n\n    // Check if property exists and if it is an object\n    if (has(parent, property) && isObject(parent[property])) {\n\n      // Initialize empty object\n      object = {};\n\n      // For each key in the object\n      Object.keys(parent[property]).forEach((key) => {\n\n        // Check if property.key exists and if it is not null\n        if (has(parent[property], key) && !isNil(parent[property][key])) object[key] = parent[property][key];\n      });\n    } else if (placeholder) object = { en: placeholder };\n\n    // Return the value or the placeholder object\n    return object;\n  },\n  getArrayOfNumbers: (parent, property) => {\n    // Initialize empty array\n    const array = [];\n\n    // Check if property exists and if it is an array\n    if (has(parent, property) && isArray(parent[property])) {\n\n      // For each element of the array\n      for (const element of parent[property]) {\n\n        // Check if element is a non-empty string\n        if (!isNil(element) && isNumber(element)) array.push(element);\n      }\n    }\n\n    // Return the value or an empty array\n    return array;\n  },\n  getArrayOfStrings: (parent, property) => {\n    // Initialize empty array\n    const array = [];\n\n    // Check if property exists and if it is an array\n    if (has(parent, property) && isArray(parent[property])) {\n\n      // For each element of the array\n      for (const element of parent[property]) {\n\n        // Check if element is a non-empty string\n        if (!isNil(element) && isString(element)) array.push(element);\n      }\n    }\n\n    // Return the value or an empty array\n    return array;\n  },\n  getArrayOfObjects: (parent, property, keys) => {\n    // Initialize empty array\n    const array = [];\n\n    // Check if property exists and if it is an array\n    if (has(parent, property) && isArray(parent[property])) {\n\n      // For each element of the array\n      for (const element of parent[property]) {\n\n        // Check if element is a non-empty object\n        if (!isNil(element) && isObject(element)) {\n\n          // Initialize empty object\n          const object = {};\n\n          // Set all keys to undefined\n          for (const key of keys) {\n\n            // Check if element.key exists and if it is not null\n            if (has(element, key) && !isNil(element[key])) object[key] = element[key];\n          }\n\n          // Add the object if it is not empty\n          if (!isNil(object)) array.push(object);\n        }\n      }\n    }\n\n    // Return the value or an empty array\n    return array;\n  },\n  /*\n  ***** SPECIFIC METHODS TO TRANSFORM DATA MODEL *****\n  */\n  getCount: (parent) => {\n    let count = 0;\n    if (has(parent, 'count') && !isNil(parent.count)) count = parent.count;\n    return count;\n  },\n  getDistributions: (parent) => {\n    const distributions = [];\n    if (has(parent, 'distributions') && isArray(parent.distributions)) {\n      for (const d of parent.distributions) {\n        let distribution;\n        if (!isNil(d) && isObject(d)) {\n          distribution = d;\n          distributions.push(distribution);\n        }\n      }\n    }\n    return distributions;\n  },\n  getOriginalLanguage: (parent) => {\n    let originalLanguage;\n    if (has(parent, 'translation_meta') && has(parent, 'translation_meta.details') && !isNil(parent.translation_meta.details) && isObject(parent.translation_meta.details)) {\n      Object.keys(parent.translation_meta.details).forEach((key) => {\n        if (has(parent.translation_meta.details[key], 'original_language')) originalLanguage = parent.translation_meta.details[key].original_language;\n      });\n    }\n    return originalLanguage;\n  },\n  getTranslationMetaData: (parent) => {\n    const translationMetaData = {\n      fullAvailableLanguages: [],\n      details: {},\n      status: undefined,\n    };\n    if (!has(parent, 'translation_meta')) return translationMetaData;\n    if (isNil(parent.translation_meta) || !isObject(parent.translation_meta)) return translationMetaData;\n    if (has(parent, 'translation_meta.full_available_languages') && !isNil(parent.translation_meta.full_available_languages)) {\n      for (const l of parent.translation_meta.full_available_languages) {\n        if (!isNil(parent.translation_meta.full_available_languages[l])) translationMetaData.fullAvailableLanguages.push(l);\n      }\n    }\n    if (has(parent, 'translation_meta.details') && !isNil(parent.translation_meta.details)) {\n      Object.keys(parent.translation_meta.details).forEach((key) => {\n        if (!isNil(parent.translation_meta.details[key])) translationMetaData.details[key] = parent.translation_meta.details[key];\n      });\n    }\n    if (has(parent, 'translation_meta.status') && !isNil(parent.translation_meta.status)) translationMetaData.status = parent.translation_meta.status;\n    return translationMetaData;\n  },\n};\nexport default getters;\n"],"names":["convertKeyArrayToObject","keys","map","obj","key","defaultValue","getters","parent","property","string","has","isString","isNil","number","isNumber","object","isObject","k","placeholder","array","isArray","element","count","distributions","d","distribution","originalLanguage","translationMetaData","l"],"mappings":";AAUA,SAASA,EAAwBC,GAAM;AACrC,MAAKA;AAEL,WAAOA,EAAK,OAAO,CAACC,GAAKC,MAAQ;AACzB,YAAAC,IAAMD,EAAI,OAAOA,GACjBE,IAAeF,EAAI,WAAW;AACpC,aAA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