"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.SecurityInPortLock = void 0;
const LogErrorDecorator_1 = require("../../../../core/decorator/LogErrorDecorator");
const request_1 = require("../../request");
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const LockCommand_1 = require("../../../../app/usecase/command/security/operations/lock/LockCommand");
const ReleaseCommand_1 = require("../../../../app/usecase/command/security/operations/release/ReleaseCommand");
const LockedBalanceOfQuery_1 = require("../../../../app/usecase/query/security/lockedBalanceOf/LockedBalanceOfQuery");
const LockCountQuery_1 = require("../../../../app/usecase/query/security/lockCount/LockCountQuery");
const LocksIdQuery_1 = require("../../../../app/usecase/query/security/locksId/LocksIdQuery");
const GetLockQuery_1 = require("../../../../app/usecase/query/security/getLock/GetLockQuery");
const BaseSecurityInPort_1 = require("../BaseSecurityInPort");
class SecurityInPortLock extends BaseSecurityInPort_1.BaseSecurityInPort {
    async lock(request) {
        const { securityId, amount, targetId, expirationTimestamp } = request;
        ValidatedArgs_1.default.handleValidation("LockRequest", request);
        return await this.commandBus.execute(new LockCommand_1.LockCommand(amount, targetId, securityId, expirationTimestamp));
    }
    async release(request) {
        const { securityId, lockId, targetId } = request;
        ValidatedArgs_1.default.handleValidation("ReleaseRequest", request);
        return await this.commandBus.execute(new ReleaseCommand_1.ReleaseCommand(lockId, targetId, securityId));
    }
    async getLockedBalanceOf(request) {
        ValidatedArgs_1.default.handleValidation("GetLockedBalanceRequest", request);
        const res = await this.queryBus.execute(new LockedBalanceOfQuery_1.LockedBalanceOfQuery(request.securityId, request.targetId));
        const balance = { value: res.payload.toString() };
        return balance;
    }
    async getLockCount(request) {
        ValidatedArgs_1.default.handleValidation("GetLockCountRequest", request);
        return (await this.queryBus.execute(new LockCountQuery_1.LockCountQuery(request.securityId, request.targetId))).payload;
    }
    async getLocksId(request) {
        ValidatedArgs_1.default.handleValidation("GetLocksIdRequest", request);
        const res = (await this.queryBus.execute(new LocksIdQuery_1.LocksIdQuery(request.securityId, request.targetId, request.start, request.end))).payload;
        const lockIds = res.map((id) => id.toString());
        return lockIds;
    }
    async getLock(request) {
        ValidatedArgs_1.default.handleValidation("GetLockRequest", request);
        const res = (await this.queryBus.execute(new GetLockQuery_1.GetLockQuery(request.securityId, request.targetId, request.id)))
            .payload;
        const lock = {
            id: res.id,
            amount: res.amount.toString(),
            expirationDate: res.expiredTimestamp.toString(),
        };
        return lock;
    }
}
exports.SecurityInPortLock = SecurityInPortLock;
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.LockRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortLock.prototype, "lock", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.ReleaseRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortLock.prototype, "release", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetLockedBalanceRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortLock.prototype, "getLockedBalanceOf", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetLockCountRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortLock.prototype, "getLockCount", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetLocksIdRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortLock.prototype, "getLocksId", null);
__decorate([
    LogErrorDecorator_1.LogError,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [request_1.GetLockRequest]),
    __metadata("design:returntype", Promise)
], SecurityInPortLock.prototype, "getLock", null);
