"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.QueryBus = 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 QueryHandlerNotFoundException_1 = require("./error/QueryHandlerNotFoundException");
const InvalidQueryHandlerException_1 = require("./error/InvalidQueryHandlerException");
let QueryBus = class QueryBus {
    handlers = new Map();
    constructor() {
        const handlers = Injectable_1.default.getQueryHandlers();
        this.registerHandlers(handlers);
    }
    execute(query) {
        const queryId = this.getQueryId(query);
        const handler = this.handlers.get(queryId);
        if (!handler) {
            throw new QueryHandlerNotFoundException_1.QueryHandlerNotFoundException(queryId);
        }
        return handler.execute(query);
    }
    bind(handler, id) {
        this.handlers.set(id, handler);
    }
    getQueryId(query) {
        const { constructor: queryType } = Object.getPrototypeOf(query);
        const queryMetadata = Reflect.getMetadata(Constants_1.QUERY_METADATA, queryType);
        if (!queryMetadata) {
            throw new QueryHandlerNotFoundException_1.QueryHandlerNotFoundException(queryType.name);
        }
        return queryMetadata.id;
    }
    registerHandlers(handlers) {
        handlers.forEach((handler) => {
            const target = this.reflectQueryId(handler);
            if (!target) {
                throw new InvalidQueryHandlerException_1.InvalidQueryHandlerException();
            }
            this.bind(handler, target);
        });
    }
    reflectQueryId(handler) {
        const { constructor: handlerType } = Object.getPrototypeOf(handler);
        const query = Reflect.getMetadata(Constants_1.QUERY_HANDLER_METADATA, handlerType);
        const queryMetadata = Reflect.getMetadata(Constants_1.QUERY_METADATA, query);
        return queryMetadata.id;
    }
};
exports.QueryBus = QueryBus;
exports.QueryBus = QueryBus = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], QueryBus);
