/* @flow */

import _ from 'lodash'
import type { OrderedMap } from 'immutable';
import type MediaFile from './media_file';

export default class FileSeacher {
  files: OrderedMap<number, MediaFile>;

  constructor(files: OrderedMap<number, MediaFile>) {
    this.files = files;
  }

  search(searchKeyword: string): OrderedMap<number,  MediaFile> {
    if (_.isEmpty(searchKeyword))
      return this.files;

    const queries = searchKeyword.split(/ +/).map(word => word.split(":"));
    const result = _.reduce(queries, (files, q) => {
      if (q.length == 1)
        return files.filter(f => this.isIncludeProperty(f, "basename", q));

      switch (q[0]) {
        case "basename":
          return files.filter(f => this.isIncludeProperty(f, "basename", q[1]));
        case "fullpath":
          return files.filter(f => this.isIncludeProperty(f, "fullpath", q[1]));
        case "is":
          return files.filter(f => this.predicateFile(f, q[1]));
      }

    }, this.files);

    return result;
  }

  isIncludeProperty(f: MediaFile, propertyName: string, value: string): boolean {
    return f[propertyName].includes(value);
  }

  predicateFile(f: MediaFile, predicate: string): boolean {
    return !!f[predicate];
  }
}

@@ -629,16 +629,19 @@
 name%22, q
+%5B0%5D
 ));%0A%0A   
@@ -1083,32 +1083,148 @@
 ing): boolean %7B%0A
+    if (value.toLowerCase() === value) %7B%0A      return f%5BpropertyName%5D.toLowerCase().includes(value);%0A    %7D else %7B%0A  
     return f%5Bpro
@@ -1251,16 +1251,22 @@
 value);%0A
+    %7D%0A
   %7D%0A%0A  p

Implement smartcase matching with search keyword