import giCategories from '../data/glycemic-index-categories';

export const categoryPathMap = () => {
  const map = {};
  giCategories.forEach((item)=>{
    map[item.category] = item.page;
  });
  return map;
};

export const pathCategoryMap = () => {
  const map = {};
  giCategories.forEach((item)=>{
    map[item.page] = item.category;
  });
  return map;
};

export const categoryFromPath = (path) => {
  // we assume all paths that use this function have a prefix of 'glycemic-index'
  if (path && path.indexOf('/glycemic-index/') > -1) {
    let categoryPath;
    const pathName = path.substring('/glycemic-index/'.length, path.length-1);
    // check path for GI type filter ( high-gi / medium-gi / low-gi )
    if (pathName.indexOf('-gi/') > -1) {
      categoryPath = pathName.substring(pathName.indexOf('-gi/') + '-gi/'.length);
    } else {
      categoryPath = pathName;
    }
    return pathCategoryMap()[categoryPath];
  }
  return undefined;
};

export const giTypeFilterFromPath = (path) => {
  let giFilter;
  // we assume all paths that use this function have a prefix of 'glycemic-index'
  if (path && path.indexOf('/glycemic-index/') > -1) {
    const pathName = path.substring('/glycemic-index/'.length, path.length-1);
    // check path for GI type filter ( high-gi / medium-gi / low-gi )
    if (pathName.indexOf('-gi/') > -1) {
      giFilter = pathName.substring(0, pathName.indexOf('-gi/') + '-gi/'.length-1);
    }
  }
  return giFilter;
};

export const isOfGiType = (giValue, giType) => {
  switch (giType) {
    case 'high-gi':
      return (giValue > 69);
      break;
    case 'medium-gi':
      return (giValue > 55 && giValue < 70);
      break;
    case 'low-gi':
      return (giValue < 56);
      break;
    default:
      return false;
  }
};

@@ -1251,44 +1251,52 @@
 eck 
+if 
 path 
-for GI type filter
+is a gi filter%0A    if
  ( 
+%5B'
 high-gi
- / 
+','
 medi
@@ -1296,35 +1296,136 @@
 ','medium-gi
- / 
+','
 low-gi
- )
+'%5D.indexOf(pathName) %3E -1 )%7B%0A      giFilter = pathName;%0A    %7D%0A    // check if path contains a gi filter
 %0A    if (pat

fix bug with root path gi-index values