text: import { get, trim } from 'lodash';
  import slugify from 'slugify';
  
  import {
    CollectiveCategory,
    CollectiveTagsCategories,
    CollectiveType,
    OPENSOURCE_COLLECTIVE_ID,
  } from '../lib/constants/collectives';
  
  /**
   * For a given host and/or a list of tags, returns the main tag for the category of the
   * collective. If none matches, defaults to `CollectiveCategory.COLLECTIVE`
   */
  export const getCollectiveMainTag = (hostCollectiveId = null, tags = [], type, settings = null) => {
    // All collectives from "Open Source Collective 501c3" are set to "Open source" category
    if (hostCollectiveId === OPENSOURCE_COLLECTIVE_ID) {
      return CollectiveCategory.OPEN_SOURCE;
    }
  
    // Try to guess the main category from tags
    if (tags) {
      const tagWithCategory = tags.find(tag => CollectiveTagsCategories[tag]);
      if (tagWithCategory) {
        const category = CollectiveTagsCategories[tagWithCategory];
        return CollectiveCategory[category];
      }
    }
  
    // Try to get from the type
    if (type === CollectiveType.EVENT) {
      return CollectiveCategory.EVENT;
    } else if (type === CollectiveType.ORGANIZATION) {
      return CollectiveCategory.ORGANIZATION;
    } else if (type === CollectiveType.USER) {
      return CollectiveCategory.USER;
    } else if (type === CollectiveType.PROJECT) {
      return CollectiveCategory.PROJECT;
    } else if (type === CollectiveType.FUND) {
      return CollectiveCategory.FUND;
    }
  
    // Funds MVP, to refactor
    if (settings && settings.fund) {
      return CollectiveCategory.FUND;
    }
  
    // Default to 'Collective'
    return CollectiveCategory.COLLECTIVE;
  };
  
  export const getCollectiveTypeForUrl = collective => {
    if (!collective) {
      return;
    }
  
    if (collective.type === 'EVENT') {
      return 'events';
    }
    if (collective.type === 'PROJECT') {
      return 'projects';
    }
  };
  
  export const hostIsTaxDeductibeInTheUs = host => {
    return get(host, 'settings.taxDeductibleDonations');
  };
  
  export const suggestSlug = value => {
    const slugOptions = {
      replacement: '-',
      lower: true,
      strict: true,
    };
  
    return trim(slugify(value, slugOptions), '-');
  };
  
  export const getTopContributors = contributors => {
    const topOrgs = [];
    const topIndividuals = [];
  
    for (const contributor of contributors) {
      // We only care about financial contributors that donated $$$
      if (!contributor.isBacker || !contributor.totalAmountDonated) {
        continue;
      }
  
      // Put contributors in the array corresponding to their types
      if (contributor.type === CollectiveType.USER) {
        topIndividuals.push(contributor);
      } else if (contributor.type === CollectiveType.ORGANIZATION || contributor.type === CollectiveType.COLLECTIVE) {
        topOrgs.push(contributor);
      }
  
      if (topIndividuals.length >= 10 && topOrgs.length >= 10) {
        break;
      }
    }
  
    // If one of the two categories is not filled, complete with more contributors from the other
    const nbColsPerCategory = 2;
    const nbFreeColsFromOrgs = nbColsPerCategory - Math.ceil(topOrgs.length / 5);
    const nbFreeColsFromIndividuals = nbColsPerCategory - Math.ceil(topOrgs.length / 5);
    let takeNbOrgs = 10;
    let takeNbIndividuals = 10;
  
    if (nbFreeColsFromOrgs > 0) {
      takeNbIndividuals += nbFreeColsFromOrgs * 5;
    } else if (nbFreeColsFromIndividuals > 0) {
      takeNbOrgs += nbFreeColsFromIndividuals * 5;
    }
  
    return [topOrgs.slice(0, takeNbOrgs), topIndividuals.slice(0, takeNbIndividuals)];
  };
  
  export const isEmptyCollectiveLocation = account => {
    if (!account?.location) {
      return true;
    } else {
      const { country, lat, long } = account.location;
      return !(country || (lat && long));
    }
  };
  