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