"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const RoomstayError_1 = require("errors/RoomstayError");
const GlobalRoomstayEvents = {};
/**
 * Roomstay event manager, always used as a global object, but wrapped in a class.
 *
 * Future improvements - Adding on fail / on
 */
class RoomstayEventManager {
    raise(event) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const eventPrototype = Object.getPrototypeOf(event);
            if (!eventPrototype || !Object.prototype.hasOwnProperty.call(eventPrototype, 'constructor') || !((_b = (_a = eventPrototype.constructor).getLabel) === null || _b === void 0 ? void 0 : _b.call(_a))) {
                // TODO: Custom logger
                throw new RoomstayError_1.RoomstayError('Failed to raise event as object passed is invalid');
            }
            const name = eventPrototype.constructor.getLabel();
            if (GlobalRoomstayEvents[name]) {
                for (const storedEvent of GlobalRoomstayEvents[name]) {
                    try {
                        yield storedEvent.callback(event);
                    }
                    catch (ex) {
                        // TODO: Custom logger
                        console.error(ex);
                    }
                }
            }
        });
    }
    addEvent(eventType, callback, id) {
        var _a, _b;
        if (!callback || typeof callback !== 'function') {
            return false;
        }
        try {
            let name = '';
            if (typeof eventType === 'string') {
                name = eventType;
            }
            else {
                name = (_b = (_a = eventType).getLabel) === null || _b === void 0 ? void 0 : _b.call(_a);
            }
            if (!name) {
                return false;
            }
            if (!Object.prototype.hasOwnProperty.call(GlobalRoomstayEvents, name)) {
                GlobalRoomstayEvents[name] = [];
            }
            const event = {
                id: undefined,
                callback,
            };
            if (id && typeof id === 'string') {
                event.id = id;
                const eventIndex = (() => {
                    const numOfEvents = GlobalRoomstayEvents[name].length;
                    for (let i = 0; i < numOfEvents; i++) {
                        if (GlobalRoomstayEvents[name][i].id === id) {
                            return i;
                        }
                    }
                    return numOfEvents;
                })();
                GlobalRoomstayEvents[name][eventIndex] = event;
            }
            else {
                GlobalRoomstayEvents[name].push(event);
            }
            return true;
        }
        catch (ex) {
            //  TODO: Add logging if user enters something whack
            return false;
        }
    }
}
const manager = new RoomstayEventManager();
exports.default = manager;
//# sourceMappingURL=RoomstayEventManager.js.map