import * as LibTaxes from '@opencollective/taxes';
import config from 'config';
import { pick } from 'lodash';

import { types as CollectiveTypes } from '../constants/collectives';
import { VAT_OPTIONS } from '../constants/vat';

import logger from './logger';
import { md5 } from './utils';

/**
 * Returns an URL for the given collective params
 * @param {String} collectiveSlug
 * @param {String} collectiveType
 * @param {String|null} image
 * @param {Object} args
 *    - height
 *    - format
 */
export const getCollectiveAvatarUrl = (collectiveSlug, collectiveType, image, args) => {
  const sections = [config.host.images, collectiveSlug];

  if (image) {
    sections.push(md5(image).substring(0, 7));
  }

  sections.push(collectiveType === CollectiveTypes.USER ? 'avatar' : 'logo');

  if (args.height) {
    sections.push(args.height);
  }

  return `${sections.join('/')}.${args.format || 'png'}`;
};

export const COLLECTIVE_SETTINGS_KEYS_LIST = [
  'apply',
  'bitcoin',
  'categories',
  'collectivePage',
  'disableCustomContributions',
  'dismissedHelpMessages',
  'editor',
  'enableWebhooks',
  'features',
  'feesOnTop',
  'fund',
  'githubOrg',
  'githubOrgs',
  'githubRepo',
  'githubUsers',
  'goals',
  'hideCreditCardPostalCode',
  'hostCollective',
  'hostFeePercent',
  'HostId',
  'invoice',
  'invoiceTitle',
  'isHostCollective',
  'lang',
  'matchingFund',
  'paymentMethods',
  'recommendedCollectives',
  'style',
  'superCollectiveTag',
  'taxDeductibleDonations',
  'tos',
  'twitter',
  'VAT',
  'virtualCardsMaxDailyCount',
  'W9',
];

/**
 * Whitelist the collective settings that can be updated.
 * TODO: Whitelist all collective fields (only VAT is atm)
 */
export function whitelistSettings(settings) {
  if (!settings) {
    return null;
  }

  const preparedSettings = { ...settings };

  if (preparedSettings.VAT) {
    preparedSettings.VAT = pick(preparedSettings.VAT, ['number', 'type']);
  }

  // Generate warnings for invalid settings
  Object.keys(settings).forEach(key => {
    if (!COLLECTIVE_SETTINGS_KEYS_LIST.includes(key)) {
      logger.warn(`Invalid collective setting key detected: ${key}`);
    }
  });

  return preparedSettings;
}

/**
 * Returns false if settings are valid or an error as string otherwise
 * @param {object|null} settings
 */
export function validateSettings(settings) {
  if (!settings) {
    return false;
  }

  // Validate VAT
  if (settings.VAT) {
    if (typeof settings.VAT !== 'object') {
      return 'Invalid type for VAT settings';
    } else if (settings.VAT.number && !LibTaxes.checkVATNumberFormat(settings.VAT.number).isValid) {
      return 'Invalid VAT number';
    } else if (settings.VAT.type && settings.VAT.type !== VAT_OPTIONS.HOST && settings.VAT.type !== VAT_OPTIONS.OWN) {
      return 'Invalid VAT configuration';
    }
  }

  if (settings) {
    return false;
  }
}

export const collectiveSlugReservedlist = [
  'about',
  'admin',
  'applications',
  'become-a-sponsor',
  'chapters',
  'collective',
  'collectives',
  'contact',
  'contribute',
  'create',
  'create-account',
  'confirm',
  'delete',
  'deleteCollective',
  'discover',
  'donate',
  'edit',
  'expenses',
  'event',
  'events',
  'faq',
  'gift-card',
  'gift-cards',
  'gift-cards-next',
  'gift-of-giving',
  'help',
  'home',
  'host',
  'hosts',
  'how-it-works',
  'join',
  'join-free',
  'learn-more',
  'member-invitations',
  'member',
  'members',
  'order',
  'orders',
  'pledge',
  'pledges',
  'pricing',
  'privacypolicy',
  'redeem',
  'redeemed',
  'register',
  'search',
  'signin',
  'signup',
  'subscriptions',
  'tos',
  'transactions',
  'widgets',
];

/**
 * Check if given `slug` could conflict with existing routes or
 * if it's a reserved keyword.
 *
 * The list is mostly based on frontend `src/server/pages.js` file and
 * `src/pages/static` content.
 *
 * @param {String} slug
 */
export function isCollectiveSlugReserved(slug) {
  return collectiveSlugReservedlist.includes(slug);
}

@@ -2925,16 +2925,52 @@
 about',%0A
+  'accept-financial-contributions',%0A
   'admin
@@ -2972,16 +2972,16 @@
 admin',%0A
-
   'appli
@@ -3055,24 +3055,37 @@
 llectives',%0A
+  'confirm',%0A
   'contact',
@@ -3133,29 +3133,16 @@
 count',%0A
-  'confirm',%0A
   'delet
@@ -3207,22 +3207,8 @@
 t',%0A
-  'expenses',%0A
   'e
@@ -3222,24 +3222,51 @@
   'events',%0A
+  'expense',%0A  'expenses',%0A
   'faq',%0A  '
@@ -3483,16 +3483,32 @@
 mbers',%0A
+  'onboarding',%0A
   'order
@@ -3591,16 +3591,16 @@
 edeem',%0A
-
   'redee
@@ -3605,16 +3605,30 @@
 eemed',%0A
+  'redirect',%0A
   'regis

Add blocked slugs