import { createSelector } from 'reselect';
import { filterQuery } from 'app/utils';
import deburr from 'lodash/deburr';
import isEmpty from 'lodash/isEmpty';

const getCountries = state => (state.countries && state.countries.data) || null;
const getIndicatorsData = state =>
  (state.compareAll.data && state.compareAll.data.indicators) || null;
export const getLoading = state => state.compareAll.loading || null;
export const getSearch = (state, { search }) => deburr(search.search) || '';
export const getQuery = (state, { search }) => search || '';

const getData = createSelector(
  [getCountries, getIndicatorsData],
  (countries, indicators) => {
    if (!countries || !indicators || !indicators.length) return null;
    const ndcIndicator = indicators.find(i => i.slug === 'submission');
    const emissionsIndicator = indicators.find(i => i.slug === 'ndce_ghg');
    const ltsIndicator = indicators.find(i => i.slug === 'lts_submission');
    const rows = countries.map(c => {
      const countryNDC = ndcIndicator.locations[c.iso_code3];
      const countryLTS = ltsIndicator.locations[c.iso_code3];
      const countryEmissions = emissionsIndicator.locations[c.iso_code3];
      const getIconValue = (conditionYes, conditionIntends) => {
        if (conditionYes) return 'yes';
        if (conditionIntends) return 'intends';
        return 'no';
      };
      return {
        Country: { name: c.wri_standard_name, iso: c.iso_code3 },
        'Share of global GHG emissions':
          countryEmissions && countryEmissions.value,
        INDC: getIconValue(
          countryNDC.value === 'INDC Submitted' ||
            countryNDC.value === 'First NDC Submitted' ||
            countryNDC.value === 'Second NDC Submitted'
        ),
        NDC: getIconValue(
          countryNDC.value === 'First NDC Submitted' ||
            countryNDC.value === 'Second NDC Submitted'
        ),
        '2nd NDC': getIconValue(countryNDC.value === 'Second NDC Submitted'),
        LTS: getIconValue(
          countryLTS && countryLTS.value === 'Long-term Strategy Submitted'
        )
      };
    });
    return rows;
  }
);

export const getColumns = createSelector([getData], rows => {
  if (!rows) return [];
  //   'Country', X
  //   'Share of global GHG emissions', X
  //   'Pre-2020 pledge', Missing
  //   'INDC', X
  //   'NDC', X
  //   '2nd NDC', X
  //   'Targets in National Policies', Missing
  //   'LTS' X
  return rows[0] && Object.keys(rows[0]);
});

export const getFilteredDataBySearch = createSelector(
  [getData, getSearch],
  (data, search) => {
    if (!data || isEmpty(data)) return null;
    return filterQuery(data, search, [], { Country: 'name' });
  }
);

export const getSelectedTargets = createSelector([getQuery], query => {
  if (!query || !query.targets) return [];
  const selectedTargets = query.targets.split(',');
  return selectedTargets;
});

@@ -1364,22 +1364,37 @@
  %7D;%0A
+%0A
       
-return %7B
+const isLTSSubmitted =
 %0A   
@@ -1402,109 +1402,110 @@
     
-C
+c
 ountry
-: %7B name: c.wri_standard_name, iso: c.iso_code3 %7D,%0A        'Share of global GHG emissions':%0A  
+LTS && countryLTS.value === 'Long-term Strategy Submitted';%0A      const isSecondNDCSubmitted =%0A
     
@@ -1515,25 +1515,19 @@
  country
-Emissions
+NDC
  && coun
@@ -1533,90 +1533,63 @@
 ntry
-Emissions.value,%0A        INDC: getIconValue(%0A          countryNDC.value === 'I
+NDC.value === 'Second NDC Submitted';%0A      const is
 NDC
- 
 Subm
@@ -1585,36 +1585,34 @@
 t isNDCSubmitted
-' %7C%7C
+ =
 %0A            cou
@@ -1596,36 +1596,55 @@
 itted =%0A        
-    
+countryNDC &&%0A        (
 countryNDC.value
@@ -1676,54 +1676,131 @@
 ' %7C%7C
-%0A            countryNDC.value === 'Second 
+ isSecondNDCSubmitted);%0A      const isINDCSubmitted =%0A        countryNDC && (countryNDC.value === 'INDC Submitted' %7C%7C is
 NDC
- 
 Subm
@@ -1800,36 +1800,42 @@
 NDCSubmitted
-'
+);%0A
 %0A      
-  ),
+return %7B
 %0A        NDC
@@ -1835,128 +1835,194 @@
     
-NDC: getIconValue(%0A          countryNDC.value === 'First NDC Submitted' %7C%7C%0A            countryNDC.value === 'Second 
+Country: %7B name: c.wri_standard_name, iso: c.iso_code3 %7D,%0A        'Share of global GHG emissions':%0A          countryEmissions && countryEmissions.value,%0A        INDC: getIconValue(isI
 NDC
- 
 Subm
@@ -2018,33 +2018,34 @@
 (isINDCSubmitted
-'
+),
 %0A        ),%0A    
@@ -2037,16 +2037,48 @@
         
+NDC: getIconValue(isNDCSubmitted
 ),%0A     
@@ -2108,41 +2108,19 @@
 lue(
-countryNDC.value === '
+is
 Second
- 
 NDC
- 
 Subm
@@ -2124,17 +2124,16 @@
 ubmitted
-'
 ),%0A     
@@ -2157,93 +2157,22 @@
 lue(
-%0A          countryLTS && countryLTS.value === 'Long-term Strategy Submitted'%0A        
+isLTSSubmitted
 )%0A  

Fix missng country ndc data error on compare-all-targets