"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HoldOperations = void 0;
const asset_tokenization_contracts_1 = require("@hashgraph/asset-tokenization-contracts");
const Constants_1 = require("../../../../core/Constants");
const LogService_1 = __importDefault(require("../../../../app/service/log/LogService"));
class HoldOperations {
    executor;
    constructor(executor) {
        this.executor = executor;
    }
    async createHoldByPartition(security, partitionId, escrowId, amount, targetId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Holding ${amount} tokens from account ${targetId.toString()} until ${expirationDate} with escrow ${escrowId}`);
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: expirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldTokenHolderFacet__factory.createInterface(), "createHoldByPartition", [partitionId, hold], Constants_1.GAS.CREATE_HOLD);
    }
    async createHoldFromByPartition(security, partitionId, escrowId, amount, sourceId, targetId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Holding ${amount} tokens from account ${sourceId.toString()} until ${expirationDate} with escrow ${escrowId}`);
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: expirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldTokenHolderFacet__factory.createInterface(), "createHoldFromByPartition", [partitionId, sourceId.toString(), hold, "0x"], Constants_1.GAS.CREATE_HOLD_FROM);
    }
    async controllerCreateHoldByPartition(security, partitionId, escrowId, amount, sourceId, targetId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Controller Holding ${amount} tokens from account ${sourceId.toString()} until ${expirationDate} with escrow ${escrowId}`);
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: expirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldManagementFacet__factory.createInterface(), "controllerCreateHoldByPartition", [partitionId, sourceId.toString(), hold, "0x"], Constants_1.GAS.CONTROLLER_CREATE_HOLD);
    }
    async protectedCreateHoldByPartition(security, partitionId, amount, escrowId, sourceId, targetId, expirationDate, deadline, nonce, signature, securityId) {
        LogService_1.default.logTrace(`Protected Holding ${amount} tokens from account ${sourceId.toString()} until ${expirationDate} with escrow ${escrowId}`);
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: expirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        const protectedHold = {
            hold: hold,
            deadline: deadline.toBigInt(),
            nonce: nonce.toBigInt(),
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldManagementFacet__factory.createInterface(), "protectedCreateHoldByPartition", [partitionId, sourceId.toString(), protectedHold, signature], Constants_1.GAS.PROTECTED_CREATE_HOLD);
    }
    async releaseHoldByPartition(security, partitionId, holdId, targetId, amount, securityId) {
        LogService_1.default.logTrace(`Releasing hold amount ${amount} from account ${targetId.toString()}}`);
        const holdIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            holdId,
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldTokenHolderFacet__factory.createInterface(), "releaseHoldByPartition", [holdIdentifier, amount.toBigInt()], Constants_1.GAS.RELEASE_HOLD);
    }
    async reclaimHoldByPartition(security, partitionId, holdId, targetId, securityId) {
        LogService_1.default.logTrace(`Reclaiming hold from account ${targetId.toString()}}`);
        const holdIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            holdId,
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldTokenHolderFacet__factory.createInterface(), "reclaimHoldByPartition", [holdIdentifier], Constants_1.GAS.RECLAIM_HOLD);
    }
    async executeHoldByPartition(security, sourceId, targetId, amount, partitionId, holdId, securityId) {
        LogService_1.default.logTrace(`Executing hold with Id ${holdId} from account ${sourceId.toString()} to account ${targetId.toString()}`);
        const holdIdentifier = {
            partition: partitionId,
            tokenHolder: sourceId.toString(),
            holdId,
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.HoldTokenHolderFacet__factory.createInterface(), "executeHoldByPartition", [holdIdentifier, targetId.toString(), amount.toBigInt()], Constants_1.GAS.EXECUTE_HOLD_BY_PARTITION);
    }
}
exports.HoldOperations = HoldOperations;
