"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-unused-vars */
const UpdateConfigVersionRequest_1 = __importDefault(require("../request/management/UpdateConfigVersionRequest"));
const LogErrorDecorator_1 = require("../../../core/decorator/LogErrorDecorator");
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const updateConfigVersionCommand_1 = require("../../../app/usecase/command/management/updateConfigVersion/updateConfigVersionCommand");
const QueryBus_1 = require("../../../core/query/QueryBus");
const Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const CommandBus_1 = require("../../../core/command/CommandBus");
const request_1 = require("../request");
const UpdateResolverRequest_1 = __importDefault(require("../request/management/UpdateResolverRequest"));
const updateResolverCommand_1 = require("../../../app/usecase/command/management/updateResolver/updateResolverCommand");
const ContractId_1 = __importDefault(require("../../../domain/context/contract/ContractId"));
const GetConfigInfoQuery_1 = require("../../../app/usecase/query/management/GetConfigInfoQuery");
const LazyInjectDecorator_1 = require("../../../core/decorator/LazyInjectDecorator");
const request_2 = require("../request");
const updateConfigCommand_1 = require("../../../app/usecase/command/management/updateConfig/updateConfigCommand");
const MirrorNodeAdapter_1 = require("../../out/mirror/MirrorNodeAdapter");
let ManagementInPort = class ManagementInPort {
    commandBus;
    queryBus;
    mirrorNode;
    constructor(commandBus = Injectable_1.default.resolve(CommandBus_1.CommandBus), queryBus = Injectable_1.default.resolve(QueryBus_1.QueryBus), mirrorNode = Injectable_1.default.resolve(MirrorNodeAdapter_1.MirrorNodeAdapter)) {
        this.commandBus = commandBus;
        this.queryBus = queryBus;
        this.mirrorNode = mirrorNode;
    }
    async updateConfigVersion(request) {
        const { configVersion, securityId } = request;
        ValidatedArgs_1.default.handleValidation("UpdateConfigVersionRequest", request);
        return await this.commandBus.execute(new updateConfigVersionCommand_1.UpdateConfigVersionCommand(configVersion, securityId));
    }
    async updateConfig(request) {
        const { configId, configVersion, securityId } = request;
        ValidatedArgs_1.default.handleValidation("UpdateConfigRequest", request);
        return await this.commandBus.execute(new updateConfigCommand_1.UpdateConfigCommand(configId, configVersion, securityId));
    }
    async updateResolver(request) {
        const { configId, securityId, resolver, configVersion } = request;
        ValidatedArgs_1.default.handleValidation("UpdateResolverRequest", request);
        return await this.commandBus.execute(new updateResolverCommand_1.UpdateResolverCommand(configVersion, securityId, configId, new ContractId_1.default(resolver)));
    }
    async getConfigInfo(request) {
        ValidatedArgs_1.default.handleValidation("GetConfigInfoRequest", request);
        const { payload } = await this.queryBus.execute(new GetConfigInfoQuery_1.GetConfigInfoQuery(request.securityId));
        const { resolverAddress, configId, configVersion } = payload;
        const resolverId = (await this.mirrorNode.getContractInfo(resolverAddress)).id;
        return {
            resolverAddress: resolverId,
            configId,
            configVersion,
        };
    }
};
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [UpdateConfigVersionRequest_1.default]),
    __metadata("design:returntype", Promise)
], ManagementInPort.prototype, "updateConfigVersion", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_2.UpdateConfigRequest]),
    __metadata("design:returntype", Promise)
], ManagementInPort.prototype, "updateConfig", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [UpdateResolverRequest_1.default]),
    __metadata("design:returntype", Promise)
], ManagementInPort.prototype, "updateResolver", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetConfigInfoRequest]),
    __metadata("design:returntype", Promise)
], ManagementInPort.prototype, "getConfigInfo", null);
ManagementInPort = __decorate([
    __param(2, (0, LazyInjectDecorator_1.lazyInject)(MirrorNodeAdapter_1.MirrorNodeAdapter)),
    __metadata("design:paramtypes", [CommandBus_1.CommandBus,
        QueryBus_1.QueryBus,
        MirrorNodeAdapter_1.MirrorNodeAdapter])
], ManagementInPort);
const Management = new ManagementInPort();
exports.default = Management;
