/* eslint-disable import/no-extraneous-dependencies */
import loadash from 'lodash/index.js';
import { pickPageKeys, pickRelationKeys } from '../../constants/pick-keys.mjs';
import settings from '../../constants/settings.mjs';
// eslint-disable-next-line import/no-cycle
import { processExtraFields } from './index.mjs';

const { pick } = loadash;

export function groupBy(list = {}, keyName = 'author_id', single = false) {
  const newObj = Object.values(list).reduce((acc, currentVal) => {
    if (!acc[currentVal[keyName]]) {
      acc[currentVal[keyName]] = [];
    }
    if (single) {
      acc[currentVal[keyName]] = currentVal;
    } else {
      acc[currentVal[keyName]].push(currentVal);
    }

    return acc;
  }, {});

  // remove empty keys if any
  Object.keys(newObj).forEach((k) => (!k || k === 'null' || newObj[k] == null) && delete newObj[k]);
  return newObj;
}

export function clonePageForCards(item, style) {
  const siteName = process.env.GATSBY_SITE_NAME;
  if (item.relation_type === 'operator') {
    if (settings.delete_operator_software[siteName]) {
      delete item.relation.software;
    }
  }
  // remove unwanted sections
  if (
    settings.keep_sections[item.relation_type] &&
    settings.keep_sections[item.relation_type][siteName]
  ) {
    const keepSections = settings.keep_sections[item.relation_type][siteName];
    const deleteSections = Object.keys(item.sections).filter((section) =>
      keepSections.indexOf(section)
    );
    deleteSections.forEach((sectionKey) => {
      delete item.sections[sectionKey];
    });
    // process extra fields for sections we want to keep
    keepSections.forEach((sectionKey) => {
      // eslint-disable-next-line no-prototype-builtins
      if (item.sections[sectionKey] && item.sections[sectionKey].hasOwnProperty('extra_fields')) {
        Object.keys(item.sections[sectionKey].extra_fields).forEach((extraFieldKey) => {
          if (
            item.sections[sectionKey].extra_fields[extraFieldKey] &&
            // eslint-disable-next-line no-prototype-builtins
            item.sections[sectionKey].extra_fields[extraFieldKey].hasOwnProperty('type')
          ) {
            processExtraFields([item.sections[sectionKey].extra_fields[extraFieldKey]]);
          }
        });
      }
    });
  } else {
    delete item.sections;
  }

  if (
    // eslint-disable-next-line no-prototype-builtins
    settings.keep_page_extra_fields.hasOwnProperty(item.relation_type) &&
    settings.keep_page_extra_fields[item.relation_type][siteName]
  ) {
    pickPageKeys.push('extra_fields');
  }

  const object = pick(item, pickPageKeys);

  object.relation = pickRelationKeys[item.relation_type]
    ? pick(item.relation, pickRelationKeys[item.relation_type])
    : item.relation;

  if (
    item.type === 'operator' &&
    style !== 'comparison_table' &&
    style !== 'template_one' &&
    style !== 'template_two' &&
    object.relation &&
    object.relation.bonus
  ) {
    delete object.relation.bonus.deposit_methods;
  }

  return object;
}

export function removeDuplicates(list, key) {
  if (!list || !key) {
    return [];
  }

  return list.reduce((filtered, current) => {
    const duplicateItem = filtered.find((item) => item[key] === current[key]);
    if (!duplicateItem) {
      return filtered.concat([current]);
    }
    return filtered;
  }, []);
}

export function removeTags(str) {
  if (str === null || str === '') return '';
  return str.toString().replace(/(<([^>]+)>)/gi, '');
}

export function removeCurrentPage(list, id) {
  const index = list.findIndex((el) => el.id === id);
  if (index !== -1) {
    list.splice(index, 1);
  }
  return list;
}

export function updateMenuPrefixPath(menu, prefixPath) {
  menu.map((item) => {
    if (item.value && item.page_id) {
      item.value = item.value === '/' ? prefixPath : `${prefixPath}/${item.value}`;
    }

    if (item.children) {
      updateMenuPrefixPath(item.children, prefixPath);
    }

    return true;
  });
}
