const axios = require('../../axios');

/**
 * @param {Record<string, any>} kaskoConfig
 * @returns {Promise<{
 *  id: string,
 *  productName: string,
 *  contents: Record<string, any>,
 *  items: any[],
 *  manifest: Record<string, any>
 * }>}
 */
async function getTouchpoint(kaskoConfig) {
  const url = `${kaskoConfig.feApiUrl}/touchpoints/${kaskoConfig.touchpointId}`;
  const { data } = await axios
    .get(url, {
      params: {
        language: kaskoConfig.language,
        key: kaskoConfig.publicKey,
      },
    })
    .catch((error) => {
      throw error;
    });

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

  return {
    id: data.id || kaskoConfig.touchpointId,
    productName: data.title,
    contents: data.content.data || {},
    items: data.items || [],
    manifest: data.webapp_manifest || {},
  };
}

module.exports = getTouchpoint;
