"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventHandler = void 0;
const CommandDecorators_1 = require("./CommandDecorators");
const EventSubscriberManager_1 = require("./EventSubscriberManager");
const runtime_generated_1 = require("@edenapp/types/runtime.generated");
const ViewManager_1 = require("../view-manager/ViewManager");
let EventHandler = class EventHandler {
    subscriberManager;
    viewManager;
    constructor(subscriberManager, viewManager) {
        this.subscriberManager = subscriberManager;
        this.viewManager = viewManager;
    }
    subscribe(args) {
        const { eventName, _callerWebContentsId, _callerAppId } = args;
        if (!runtime_generated_1.APP_EVENT_NAMES.includes(eventName)) {
            throw new Error(`Event '${eventName}' is not supported`);
        }
        if (_callerWebContentsId !== undefined) {
            // Request from a view
            const viewId = this.viewManager.getViewIdByWebContentsId(_callerWebContentsId);
            if (viewId === undefined) {
                throw new Error("View not found");
            }
            this.subscriberManager.subscribe(viewId, eventName);
        }
        else if (_callerAppId) {
            // Request from a backend
            this.subscriberManager.subscribeBackend(_callerAppId, eventName);
        }
        else {
            throw new Error("Caller identity not found");
        }
    }
    unsubscribe(args) {
        const { eventName, _callerWebContentsId, _callerAppId } = args;
        if (_callerWebContentsId !== undefined) {
            // Request from a view
            const viewId = this.viewManager.getViewIdByWebContentsId(_callerWebContentsId);
            if (viewId !== undefined) {
                this.subscriberManager.unsubscribe(viewId, eventName);
            }
        }
        else if (_callerAppId) {
            // Request from a backend
            this.subscriberManager.unsubscribeBackend(_callerAppId, eventName);
        }
    }
    exists(args) {
        return runtime_generated_1.APP_EVENT_NAMES.includes(args.eventName);
    }
};
exports.EventHandler = EventHandler;
__decorate([
    (0, CommandDecorators_1.EdenHandler)("subscribe"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], EventHandler.prototype, "subscribe", null);
__decorate([
    (0, CommandDecorators_1.EdenHandler)("unsubscribe"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], EventHandler.prototype, "unsubscribe", null);
__decorate([
    (0, CommandDecorators_1.EdenHandler)("exists"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Boolean)
], EventHandler.prototype, "exists", null);
exports.EventHandler = EventHandler = __decorate([
    (0, CommandDecorators_1.EdenNamespace)("event"),
    __metadata("design:paramtypes", [EventSubscriberManager_1.EventSubscriberManager,
        ViewManager_1.ViewManager])
], EventHandler);
//# sourceMappingURL=EventHandler.js.map