export default <LibMixins>{

    triggers:{},

    on(name,callback,event='click'){

        if (this.triggers[event] === undefined) this.triggers[event] = {};

        let length = Object.keys(this.triggers[event]).length;
        if (length <= 0) {
            if (this['trigger'+event] === undefined) this['trigger'+event] = this.trigger.bind({
                call: this,
                event
            });
            document.addEventListener(event,this['trigger'+event]);
        }

        this.triggers[event][name] = callback;

    },

    isHas(name,event='click'):boolean{
        if (this.triggers[event] === undefined) return false;
        return  !!this.triggers[event][name];
    },

    off(name,event='click'){

        if (this.triggers[event] === undefined) return undefined;

        if (this.triggers[event][name]) {
            delete this.triggers[event][name];
            this.removeEvent();
            return  true;
        }

    },

    removeEvent(event:string='click'){
        if (!this.triggers[event] || Object.keys(this.triggers[event]).length <= 0) {
            delete this.triggers[event];
            document.removeEventListener(event,this['trigger'+event]);
            delete this['trigger'+event];
        }
    },

    trigger(){

        let event = this.event;
        if (this.call.triggers[event]) {
            let trigger = false;
            for (let key in this.call.triggers[event]) {
                if (this.call.triggers[event].hasOwnProperty(key)) {
                    this.call.triggers[event][key]();
                    trigger = true;
                }
            }

            if (!trigger) this.call.removeEvent(event);
        } else {
            this.call.removeEvent();
        }


    }

}