import Ember from 'ember';

let subscribers = [];
const blogFilters = {};

function keywordMatch (filters, content) {
  return filters['filter-keywords'] && ~content.indexOf(filters['filter-keywords']);
}

function lengthMatch (filters, content) {
  return (filters['filter-short'] && content.length < 1700)
    || (filters['filter-medium'] && content.length >= 1700 && content.length < 4000)
    || (filters['filter-long'] && content.length >= 4000);
}

function categoryMatch (filters, category) {
  return filters['filter-quick'] && category === 'qt'
    || filters['filter-code'] && category === 'c'
    || filters['filter-opinion'] && category === 'o';
}

const filterPosts = filters => item => {
  return !Object.keys(filters).length
    || keywordMatch(filters, item.get('content'))
    || lengthMatch(filters, item.get('content'))
    || categoryMatch(filters, item.get('category'));
};

export default Ember.Service.extend({
  subscribe(func) {
    subscribers.push(func);
  },
  unsubscribe(func) {
    subscribers = subscribers.reduce((arr, item) => {
      if (item !== func) {
        arr.push(item);
      }
      return arr;
    }, []);
  },
  push({ key, value }) {
    if (value) {
      blogFilters[key] = value;
    } else {
      delete blogFilters[key];
    }
    subscribers.forEach(func => {
      func();
    });
  },
  filter(posts) {
    return posts.filter(filterPosts(blogFilters));
  }
});

@@ -109,31 +109,29 @@
 ontent) %7B%0A  
-return 
+if (!
 filters%5B'fil
@@ -144,19 +144,39 @@
 ywords'%5D
- &&
+) return true;%0A  return
  ~conten
@@ -255,24 +255,125 @@
  content) %7B%0A
+  if (!(filters%5B'filter-short'%5D %7C%7C filters%5B'filter-medium'%5D %7C%7C filters%5B'filter-long'%5D)) return true;%0A
   return (fi
@@ -612,16 +612,118 @@
 gory) %7B%0A
+  if (!(filters%5B'filter-quick'%5D %7C%7C filters%5B'filter-code'%5D %7C%7C filters%5B'filter-opinion'%5D)) return true;%0A
   return
@@ -918,23 +918,20 @@
  =%3E %7B%0A  
-return 
+if (
 !Object.
@@ -950,23 +950,39 @@
 ).length
-%0A    %7C%7C
+) return true;%0A  return
  keyword
@@ -1021,18 +1021,18 @@
 '))%0A    
-%7C%7C
+&&
  lengthM
@@ -1070,18 +1070,18 @@
 '))%0A    
-%7C%7C
+&&
  categor

Fix filters