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

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

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

  return {
    id: data.id,
    name: data.content.item_title || data.id,
    contents: data.content.data || {},
    fields: data.field_definition || [],
    manifest: data.webapp_manifest || {},
  };
}

module.exports = getItem;
