"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
class CallbackManager {
    constructor() {
        this.callbacks = {};
        this.maxReservedId = 999;
        this.uniqueId = this.maxReservedId + 1;
    }
    get uniqueIdValue() {
        return this.uniqueId += 1;
    }
    static normaliseId(id) {
        if (typeof id === 'number') {
            return id;
        }
        return id.toUpperCase();
    }
    addHandler(id, callback, userData) {
        try {
            const normalisedId = CallbackManager.normaliseId(id);
            this.callbacks[normalisedId] = lodash_1.get(this.callbacks, [normalisedId], []);
            if (callback) {
                this.callbacks[normalisedId].push({ callback, userData, id: normalisedId });
            }
            return normalisedId;
        }
        catch (e) {
            console.log(e);
        }
        return -1;
    }
    removeHandler(id, callBack) {
        let rVal = 0;
        try {
            const normalisedId = CallbackManager.normaliseId(id);
            if (this.callbacks[normalisedId]) {
                let thisCallbackObject;
                for (let index = 0; index < this.callbacks[normalisedId].length; index += 1) {
                    try {
                        thisCallbackObject = this.callbacks[normalisedId][index];
                        if (thisCallbackObject.callback === callBack) {
                            delete this.callbacks[normalisedId][index];
                            rVal += 1;
                        }
                    }
                    catch (e) {
                        rVal = -1;
                    }
                }
            }
        }
        catch (e) {
            rVal = -1;
        }
        return rVal;
    }
    removeAllHandlers(id) {
        let rVal = 0;
        try {
            const normalisedId = CallbackManager.normaliseId(id);
            if (this.callbacks[normalisedId]) {
                delete this.callbacks[normalisedId];
                rVal += 1;
            }
        }
        catch (e) {
            rVal = -1;
        }
        return rVal;
    }
    handleEvent(id, eventData) {
        /**
         * Call functions associated withe this id, calls functions associated with id == 0
         * if no other callback set. Callbacks are deleted when handled, except for id = 0;
         */
        let normalisedId = id;
        normalisedId = CallbackManager.normaliseId(id);
        let rVal = 0;
        const thisID = normalisedId;
        if (this.callbacks[normalisedId]) {
            let thisCallbackObject;
            for (let index = 0; index < this.callbacks[normalisedId].length; index += 1) {
                try {
                    if (index in this.callbacks[normalisedId]) {
                        thisCallbackObject = this.callbacks[normalisedId][index];
                        if ((thisCallbackObject) && (thisCallbackObject.callback)) {
                            thisCallbackObject.callback.call(null, thisCallbackObject.id, eventData, thisCallbackObject.userData);
                            rVal += 1;
                        }
                    }
                }
                catch (e) {
                }
            }
            try {
                // Don't delete if it is a number and <= this.maxReservedID
                if (thisID && (typeof thisID === 'number') && (thisID > this.maxReservedId)) {
                    this.callbacks[normalisedId] = [];
                    delete this.callbacks[normalisedId];
                }
            }
            catch (e) {
            }
        }
        return rVal;
    }
}
exports.CallbackManager = CallbackManager;
