"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.EventTarget = void 0;

const vutils_1 = require("@visactor/vutils");

let idCount = 1;

class EventTarget {
    constructor() {
        this.listenersData = {
            listeners: {},
            listenerData: {}
        };
    }
    on(type, listener) {
        if (!this.listenersData) return;
        const list = this.listenersData.listeners[type] || (this.listenersData.listeners[type] = []);
        list.push(listener);
        const id = idCount++;
        return this.listenersData.listenerData[id] = {
            type: type,
            listener: listener,
            remove: () => {
                if (!this.listenersData) return;
                delete this.listenersData.listenerData[id];
                const index = list.indexOf(listener);
                list.splice(index, 1), this.listenersData.listeners[type] && !this.listenersData.listeners[type].length && delete this.listenersData.listeners[type];
            }
        }, id;
    }
    off(idOrType, listener) {
        var _a;
        if (listener) {
            const type = idOrType;
            this.removeEventListener(type, listener);
        } else {
            const id = idOrType;
            if (!this.listenersData) return;
            null === (_a = this.listenersData.listenerData[id]) || void 0 === _a || _a.remove();
        }
    }
    addEventListener(type, listener, option) {
        this.on(type, listener);
    }
    removeEventListener(type, listener) {
        if (this.listenersData) for (const key in this.listenersData.listenerData) {
            const listenerData = this.listenersData.listenerData[key];
            listenerData.type === type && listenerData.listener === listener && this.off(key);
        }
    }
    hasListeners(type) {
        return !!this.listenersData && !!this.listenersData.listeners[type];
    }
    fireListeners(type, event) {
        if (!this.listenersData) return [];
        const list = this.listenersData.listeners[type];
        return list ? list.map((listener => listener.call(this, event))).filter((r => (0, 
        vutils_1.isValid)(r))) : [];
    }
    release() {
        delete this.listenersData;
    }
}

exports.EventTarget = EventTarget;
//# sourceMappingURL=EventTarget.js.map