"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAndCompleteTransfer = void 0;
const route_1 = require("./route");
const types_1 = require("../types");
// TODO: take out logs
// track the transfer until the destination is initiated
async function checkAndCompleteTransfer(route, receipt, destinationSigner) {
    console.log("Checking transfer state...");
    // overwrite receipt var as we receive updates, will return when it's complete
    // but can be called again if the destination is not finalized
    for await (receipt of route.track(receipt, 120 * 1000)) {
        console.log("Transfer State:", types_1.TransferState[receipt.state]);
    }
    // gucci
    if ((0, types_1.isCompleted)(receipt))
        return;
    // if the route is one we need to complete, do it
    if ((0, route_1.isManual)(route) && (0, types_1.isAttested)(receipt)) {
        const completedTxids = await route.complete(destinationSigner, receipt);
        console.log("Completed transfer with txids: ", completedTxids);
    }
    // give it time to breath and try again
    const wait = 2 * 1000;
    console.log(`Transfer not complete, trying again in a ${wait}ms...`);
    setTimeout(() => checkAndCompleteTransfer(route, receipt, destinationSigner), wait);
}
exports.checkAndCompleteTransfer = checkAndCompleteTransfer;
//# sourceMappingURL=common.js.map