import { ALIAS, MAIN } from '../constants';

export const formatKeywordsForGrid = (keywords) => {
  const tempData = [];

  keywords.forEach((keyword) => {
    tempData.push({
      id: keyword.value + keyword.lang,
      lang: keyword.lang,
      value: keyword.value,
      status: MAIN,
    });
    keyword.suggestions?.forEach((suggestion) => {
      tempData.push({
        id: suggestion + keyword.lang,
        lang: keyword.lang,
        value: suggestion,
        status: ALIAS,
      });
    });
  });

  return tempData;
};

export const formatKeywordsForUpdate = (keywords) => {
  const mainKeywords = keywords.filter((keyword) => keyword.status === MAIN);

  const formattedKeywords = mainKeywords.map((keyword) => ({
    lang: keyword.lang,
    value: keyword.value,
    suggestions: keywords
      .filter((suggestion) => suggestion.lang === keyword.lang && suggestion.status === ALIAS)
      .map((suggestion) => suggestion.value),
  }));

  const formattedKeywordsResult = formattedKeywords.map((keyword) => ({
    ...keyword,
    suggestions: keyword.suggestions.length ? keyword.suggestions : null,
  }));

  return formattedKeywordsResult;
};

export const formatLanguages = (languages) => {
  const formattedLanguages = {};

  languages?.forEach((lang) => {
    formattedLanguages[lang.value] = lang.label;
  });

  return formattedLanguages;
};
