"use strict";
// SPDX-License-Identifier: Apache-2.0
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandBus = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const tsyringe_1 = require("tsyringe");
const Constants_1 = require("../Constants");
const Injectable_1 = __importDefault(require("../injectable/Injectable"));
const CommandHandlerNotFoundException_1 = require("./error/CommandHandlerNotFoundException");
const InvalidCommandHandlerException_1 = require("./error/InvalidCommandHandlerException");
let CommandBus = class CommandBus {
    handlers = new Map();
    constructor() {
        const handlers = Injectable_1.default.getCommandHandlers();
        this.registerHandlers(handlers);
    }
    execute(command) {
        const commandId = this.getCommandId(command);
        const handler = this.handlers.get(commandId);
        if (!handler) {
            throw new CommandHandlerNotFoundException_1.CommandHandlerNotFoundException(commandId);
        }
        // Has to be casted to return type as it its inferred based off the parameter
        return handler.execute(command);
    }
    bind(handler, id) {
        this.handlers.set(id, handler);
    }
    getCommandId(command) {
        const { constructor: commandType } = Object.getPrototypeOf(command);
        const commandMetadata = Reflect.getMetadata(Constants_1.COMMAND_METADATA, commandType);
        if (!commandMetadata) {
            throw new CommandHandlerNotFoundException_1.CommandHandlerNotFoundException(commandType.name);
        }
        return commandMetadata.id;
    }
    registerHandlers(handlers) {
        handlers.forEach((handler) => {
            const target = this.reflectCommandId(handler);
            if (!target) {
                throw new InvalidCommandHandlerException_1.InvalidCommandHandlerException();
            }
            this.bind(handler, target);
        });
    }
    reflectCommandId(handler) {
        const { constructor: handlerType } = Object.getPrototypeOf(handler);
        const command = Reflect.getMetadata(Constants_1.COMMAND_HANDLER_METADATA, handlerType);
        const commandMetadata = Reflect.getMetadata(Constants_1.COMMAND_METADATA, command);
        return commandMetadata.id;
    }
};
exports.CommandBus = CommandBus;
exports.CommandBus = CommandBus = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], CommandBus);
