import { createSelector, createStructuredSelector } from 'reselect';
import groupBy from 'lodash/groupBy';
import isEmpty from 'lodash/isEmpty';

const CARDS_IN_ROW = 2;

const getIso = state => state.iso || null;

const getData = state => state.lawsAndPolicies || null;

const getActiveSector = state => state.activeSector;

export const getSectors = createSelector([getIso, getData], (iso, { data }) => {
  const sectors = (data && data[iso] && data[iso].sectors) || null;
  const parsedSectors =
    sectors &&
    sectors.map(sector => ({
      label: sector.title,
      value: sector.key
    }));

  return parsedSectors;
});

const getAllTargets = createSelector(
  [getIso, getData],
  (iso, { data }) => (data && data[iso] && data[iso].targets) || []
);

export const getCurrentSector = createSelector(
  [getActiveSector, getSectors],
  (activeSector, sectors) => {
    const defaultSector =
      sectors && sectors.find(sector => sector.value === 'economy-wide');
    if (isEmpty(activeSector)) { return sectors && sectors.length && defaultSector; }

    return (
      sectors && sectors.find(sector => sector.value === activeSector.sector)
    );
  }
);

export const getNdcContent = createSelector(
  [getAllTargets, getSectors, getActiveSector],
  (targets, sectors, currentSector) => {
    if (!targets) return null;
    const activeSector =
      (!isEmpty(currentSector) && currentSector.sector) ||
      (sectors && sectors[0]);

    const ndcTargets = targets.filter(target => target.doc_type === 'ndc');
    const parsedNdcsPerSector = groupBy(ndcTargets, 'sector');

    return (
      parsedNdcsPerSector[activeSector] &&
      parsedNdcsPerSector[activeSector].length &&
      parsedNdcsPerSector[activeSector][0]
    );
  }
);

const getCountryMeta = createSelector([getIso, getData], (iso, { data }) => {
  if (!data) return null;

  return (data && data[iso] && data[iso].country_meta) || null;
});

export const getCountryProfileLink = createSelector(
  [getCountryMeta, getIso],
  (countryMeta, iso) =>
    countryMeta && countryMeta[iso] && countryMeta[iso].country_profile
);

export const getLawsAndPoliciesCount = createSelector(
  [getCountryMeta, getIso],
  (countryMeta, iso) =>
    (countryMeta && countryMeta[iso] && countryMeta[iso].lnp_count) || 0
);

export const getLawsAndPolicies = createSelector(
  [getAllTargets, getSectors, getActiveSector],
  (targets, sectors, currentSector) => {
    if (!targets) return null;
    const activeSector =
      (!isEmpty(currentSector) && currentSector.sector) ||
      (sectors && sectors[0]);

    const lawsTargets = targets.filter(target => target.doc_type === 'law');
    const parsedLawsTargetsPerSector = groupBy(lawsTargets, 'sector');

    const groupedBySources = [];

    if (parsedLawsTargetsPerSector[activeSector]) {
      parsedLawsTargetsPerSector[activeSector].forEach(target => {
        target.sources.forEach(source => {
          groupedBySources.push({
            id: source.id
          });
        });
      });

      const groupedById = groupBy(groupedBySources, 'id');
      const sourcesIds = Object.keys(groupedById);

      const groupedTargetsBySources = {};
      sourcesIds.forEach(sourceId => {
        groupedTargetsBySources[sourceId] = { source: {}, content: [] };
      });

      sourcesIds.forEach(sourceId => {
        parsedLawsTargetsPerSector[activeSector].forEach(target => {
          const sourceInfo = target.sources.find(
            source => source.id === sourceId
          );
          if (!isEmpty(sourceInfo)) {
            groupedTargetsBySources[sourceId].source = sourceInfo;
            groupedTargetsBySources[sourceId].content.push(target);
          }
        });
      });

      const targetsParsed = Object.values(groupedTargetsBySources);

      const groupedTargetsParsed = targetsParsed.map(target => ({
        source: target.source,
        content: groupBy(target.content, 'type')
      }));

      return groupedTargetsParsed;
    }

    return [];
  }
);

const getLawsTargets = createSelector([getAllTargets], targets =>
  targets.filter(target => target.doc_type === 'law')
);

const getSectorLabels = createSelector(
  [getSectors, getLawsTargets],
  (sectors, lawTargets) =>
    sectors &&
    lawTargets &&
    sectors.map(sector => ({
      label: `${sector.label} (${lawTargets.filter(
        target => target.sector === sector.value
      ).length})`,
      value: sector.value
    }))
);

const getNationalPoliciesCount = createSelector(
  [getLawsTargets, getCurrentSector],
  (lawsTargets, currentSector) =>
    lawsTargets.filter(target => target.sector === currentSector.value).length
);

export const getCardsInRow = () => CARDS_IN_ROW;

export const getAllData = createStructuredSelector({
  sectors: getSectorLabels,
  ndcContent: getNdcContent,
  lawsTargets: getLawsAndPolicies,
  currentSector: getCurrentSector,
  countryProfileLink: getCountryProfileLink,
  lawsAndPoliciesCount: getLawsAndPoliciesCount,
  nationalPoliciesCount: getNationalPoliciesCount
});

@@ -1001,16 +1001,22 @@
 ctor)) %7B
+%0A     
  return 
@@ -1058,16 +1058,20 @@
 tSector;
+%0A   
  %7D%0A%0A    
@@ -4294,33 +4294,32 @@
 p(sector =%3E 
-(
 %7B%0A      
 label: %60$%7Bse
@@ -4310,35 +4310,24 @@
     
-label: %60$%7Bsector.label%7D ($%7B
+const matches = 
 lawT
@@ -4408,13 +4408,325 @@
 ngth
-%7D)%60,%0A
+;%0A      let labelText = '';%0A      if (matches) %7B%0A        labelText =%0A          matches === 1%0A            ? %60$%7Bsector.label%7D ($%7Bmatches%7D target)%60%0A            : %60$%7Bsector.label%7D ($%7Bmatches%7D targets)%60;%0A      %7D else %7B%0A        labelText = %60$%7Bsector.label%7D (no targets)%60;%0A      %7D%0A      return %7B%0A        label: labelText,%0A  
     
@@ -4751,18 +4751,26 @@
 lue%0A    
+  %7D;%0A    
 %7D
-)
 )%0A);%0A%0Aco

Update text in the dropdown