import { updateArrayTranslations } from '../getters.mjs';

export const processRelations = (
  page,
  pageType,
  transformedPages,
  market,
  data,
  index,
  isPreview,
  translations
) => {
  if (page.relation_type !== 'page' && page.relation_id) {
    const hasRelation =
      data.relations[page.relation_type] &&
      data.relations[page.relation_type].hasOwnProperty(page.relation_id);
    transformedPages[market][isPreview ? 'preview' : page.relation_type][index].relation =
      hasRelation ? data.relations[page.relation_type][page.relation_id] : null;

    switch (page.relation_type) {
      // Add path for payment page from payment method
      case 'operator':
        if (page.relation && hasRelation) {
          // eslint-disable-next-line camelcase, prefer-const
          const { ribbons, extra_fields, bonus, selling_point_short_names } = page.relation;
          bonus &&
            bonus.deposit_methods &&
            bonus.deposit_methods.forEach((e) => {
              const el =
                transformedPages[market].payment_method &&
                transformedPages[market].payment_method.find((el) => e.id === el.relation_id);
              if (el) e.path = el.path;
            });
          const { operator_url, support_email, operator_name } = page.relation.bonus;
          page.relation.url = operator_url || page.relation.url;
          page.relation.email = support_email || page.relation.email;
          page.relation.name = operator_name || page.relation.name;
          updateArrayTranslations(
            page.relation.selling_points,
            selling_point_short_names,
            translations
          );
          // eslint-disable-next-line no-unused-expressions
          ribbons &&
            ribbons.length > 0 &&
            ribbons.forEach((res, index) => {
              ribbons[index] = data.ribbons[res] ? data.ribbons[res].label : res;
            });
          if (ribbons && ribbons.length === 0 && extra_fields.ribbon_text) {
            page.relation.ribbons = extra_fields.ribbon_text.split(',');
          }
        }
        break;
      // Add path for game page from game provider
      case 'game':
        if (hasRelation && transformedPages[market][pageType][index].relation) {
          const providerId = transformedPages[market][pageType][index].relation.game_provider.id;
          const providerPage = transformedPages[market].software_provider
            ? transformedPages[market].software_provider.filter(
                (provider) => providerId === provider.relation_id
              )
            : [];
          transformedPages[market][pageType][index].relation.game_provider.path =
            providerPage.length > 0 ? providerPage[0].path : null;
        }
        break;
      // Add author for page from authors
      case 'author':
        if (!hasRelation) {
          transformedPages[market][pageType][index].relation = data.authors[page.relation_id];
        }
        break;
      case 'payment_method':
        if (page.relation && hasRelation) {
          updateArrayTranslations(
            page.relation.selling_points,
            page.relation.selling_point_short_names,
            translations
          );
        }
        break;
      default:
        break;
    }
  }
};
