import {uniqueId} from './dom-helpers';

export default class Component {
    constructor(options){
        options = options || {};
        this.eventListens = new Array();
        this.eventListeners = new Array();
        this.el = options.el || document.createElement('div')
        this.cid = uniqueId('c')
    
        this.on = function (type, handler) {
            this.eventListeners.push({
                type: type,
                handler: handler
            });
        };
        
        this.off = function (type, handler) {
            if(!this.eventListeners) return;
            this.eventListeners = this.eventListeners.filter(function(listener){
                return !(listener.type == type && listener.handler)
            });
        }.bind(this);
    
        this.trigger = function (event_key) {
            for (var i = 0; i < this.eventListeners.length; i++) {
                if(this.eventListeners[i].type == "*" || this.eventListeners[i].type == "all" || event_key == this.eventListeners[i].type){
                    this.eventListeners[i].handler(event_key, this);
                }
            }
        };
        
        this.initialize(options);
    }
    
    pick (object, keys) {
        var newObject = {}
        keys.forEach(function(key){
            if(object[key] !== undefined) newObject[key] = object[key];
        });
        
        return newObject;
    }
    
    listenTo(node, event, callback, context){
        context || (context = this);
        var listen = [
            node,
            event,
            callback.bind(context)
        ]
        this.eventListens.push(listen);
        node.addEventListener(event, listen[2]);
    }
    
    listenToOnce(node, event, callback, context){
        context || (context = this);
        var onceCallback = function(e){
            node.removeEventListener(event, onceCallback);
            return callback.apply(context, arguments);
        }
        var listen = [
            node,
            event,
            onceCallback
        ]
        this.eventListens.push(listen);
        node.addEventListener(event, onceCallback);
    }
    
    remove () {
        this.eventListens.forEach(function(listen){
            listen[0].removeEventListener(listen[1], listen[2]);
        });
        delete this.eventListens;
        delete this.eventListeners;
        if(this.el.parentNode) this.el.parentNode.removeChild(this.el);
        delete this.el;
    }
    
    initialize(){}
}
@@ -836,61 +836,54 @@
     
-for (var i = 0; i %3C this.eventListeners.length; i++) 
+this.eventListeners.forEach(function(listener)
 %7B%0A  
@@ -899,38 +899,24 @@
      if(
-this.eventL
+l
 istener
-s%5Bi%5D
 .type ==
@@ -923,38 +923,24 @@
  %22*%22 %7C%7C 
-this.eventL
+l
 istener
-s%5Bi%5D
 .type ==
@@ -962,38 +962,24 @@
 _key == 
-this.eventL
+l
 istener
-s%5Bi%5D
 .type)%7B%0A
@@ -1002,30 +1002,16 @@
     
-this.eventL
+l
 istener
-s%5Bi%5D
 .han
@@ -1043,32 +1043,32 @@
               %7D%0A
-
             %7D%0A  
@@ -1056,32 +1056,33 @@
  %7D%0A            %7D
+)
 %0A        %7D;%0A    

fix bug with event listeners destroying component and still cycling event listeners