// SPDX-License-Identifier: Apache-2.0
import { HoldTokenHolderFacet__factory, HoldManagementFacet__factory } from "@hashgraph/asset-tokenization-contracts";
import { GAS } from "../../../../core/Constants";
import LogService from "../../../../app/service/log/LogService";
export class HoldOperations {
    executor;
    constructor(executor) {
        this.executor = executor;
    }
    async createHoldByPartition(security, partitionId, escrowId, amount, targetId, expirationDate, securityId) {
        LogService.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(), HoldTokenHolderFacet__factory.createInterface(), "createHoldByPartition", [partitionId, hold], GAS.CREATE_HOLD);
    }
    async createHoldFromByPartition(security, partitionId, escrowId, amount, sourceId, targetId, expirationDate, securityId) {
        LogService.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(), HoldTokenHolderFacet__factory.createInterface(), "createHoldFromByPartition", [partitionId, sourceId.toString(), hold, "0x"], GAS.CREATE_HOLD_FROM);
    }
    async controllerCreateHoldByPartition(security, partitionId, escrowId, amount, sourceId, targetId, expirationDate, securityId) {
        LogService.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(), HoldManagementFacet__factory.createInterface(), "controllerCreateHoldByPartition", [partitionId, sourceId.toString(), hold, "0x"], GAS.CONTROLLER_CREATE_HOLD);
    }
    async protectedCreateHoldByPartition(security, partitionId, amount, escrowId, sourceId, targetId, expirationDate, deadline, nonce, signature, securityId) {
        LogService.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(), HoldManagementFacet__factory.createInterface(), "protectedCreateHoldByPartition", [partitionId, sourceId.toString(), protectedHold, signature], GAS.PROTECTED_CREATE_HOLD);
    }
    async releaseHoldByPartition(security, partitionId, holdId, targetId, amount, securityId) {
        LogService.logTrace(`Releasing hold amount ${amount} from account ${targetId.toString()}}`);
        const holdIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            holdId,
        };
        return this.executor.executeContractCall(securityId.toString(), HoldTokenHolderFacet__factory.createInterface(), "releaseHoldByPartition", [holdIdentifier, amount.toBigInt()], GAS.RELEASE_HOLD);
    }
    async reclaimHoldByPartition(security, partitionId, holdId, targetId, securityId) {
        LogService.logTrace(`Reclaiming hold from account ${targetId.toString()}}`);
        const holdIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            holdId,
        };
        return this.executor.executeContractCall(securityId.toString(), HoldTokenHolderFacet__factory.createInterface(), "reclaimHoldByPartition", [holdIdentifier], GAS.RECLAIM_HOLD);
    }
    async executeHoldByPartition(security, sourceId, targetId, amount, partitionId, holdId, securityId) {
        LogService.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(), HoldTokenHolderFacet__factory.createInterface(), "executeHoldByPartition", [holdIdentifier, targetId.toString(), amount.toBigInt()], GAS.EXECUTE_HOLD_BY_PARTITION);
    }
}
