const kaskoFetch = require('../../kasko-fetch');
const extractPaths = require('../../utils/translations/extractPaths');

/**
 * @param {string} url
 * @param {Object.<string, *>} params
 * @returns {Promise<{productName: string, touchpointTranslations: Record<string, *>, items: [], webappManifest: Record<string, *>}>}
 */
async function getTouchpointData(url, params) {
  const data = await kaskoFetch(url, {}, params).catch((error) => {
    throw error;
  });

  if (!data) {
    throw new Error('No touchpoint data');
  }

  const productName = data.title;
  const touchpointTranslations = extractPaths(data.content.data || []);
  const items = data.items || [];
  const webappManifest = data.webapp_manifest || {};

  return {
    productName,
    touchpointTranslations,
    items,
    webappManifest,
  };
}

module.exports = getTouchpointData;
