"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWithdrawableBalance = exports.useTimeRemaining = exports.getRemainingTime = exports.useCantonAcceptOrRejectOffer = void 0;
const react_1 = require("react");
const bridge_1 = require("../../bridge");
const coin_canton_1 = require("@ledgerhq/coin-canton");
const helpers_1 = require("@ledgerhq/coin-framework/account/helpers");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
function useCantonAcceptOrRejectOffer({ currency, account, partyId, }) {
    const cantonBridge = (0, bridge_1.getCurrencyBridge)(currency);
    const transferInstruction = (0, react_1.useCallback)(({ contractId, deviceId, reason }, type) => {
        return cantonBridge.transferInstruction(currency, deviceId, account, partyId, contractId, type, reason);
    }, [cantonBridge, currency, account, partyId]);
    return transferInstruction;
}
exports.useCantonAcceptOrRejectOffer = useCantonAcceptOrRejectOffer;
const getRemainingTime = (diff) => {
    if (diff <= 0) {
        return "";
    }
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((diff % (1000 * 60)) / 1000);
    let format = "";
    if (days > 0)
        format += `${days}d `;
    if (hours > 0)
        format += `${hours}h `;
    if (minutes > 0)
        format += `${minutes}m `;
    format += `${seconds}s`;
    return format.trim();
};
exports.getRemainingTime = getRemainingTime;
const useTimeRemaining = (expiresAtMicros = 0, isExpired = false) => {
    const [timeRemaining, setTimeRemaining] = (0, react_1.useState)("");
    (0, react_1.useEffect)(() => {
        if (expiresAtMicros <= 0 || isExpired) {
            setTimeRemaining("");
            return;
        }
        const updateTimeRemaining = () => {
            const now = Date.now();
            const expiresAt = expiresAtMicros / 1000;
            const diff = expiresAt - now;
            setTimeRemaining((0, exports.getRemainingTime)(diff));
        };
        updateTimeRemaining();
        const interval = setInterval(updateTimeRemaining, 1000);
        return () => clearInterval(interval);
    }, [expiresAtMicros, isExpired]);
    return timeRemaining;
};
exports.useTimeRemaining = useTimeRemaining;
const hasCantonResources = (account) => {
    if (account.type === "Account") {
        return (0, coin_canton_1.isCantonAccount)(account);
    }
    return "cantonResources" in account && !!account.cantonResources;
};
/**
 * Hook to calculate withdrawable balance from expired outgoing offers.
 * Withdrawable balance is the sum of amounts from offers the user sent that have expired.
 */
const useWithdrawableBalance = (account, accounts) => {
    return (0, react_1.useMemo)(() => {
        if (!hasCantonResources(account))
            return new bignumber_js_1.default(0);
        const proposals = account.cantonResources?.pendingTransferProposals ?? [];
        // For token accounts, use parent account's xpub; for main accounts, use account's xpub
        const parentAccount = (0, helpers_1.getParentAccount)(account, accounts);
        const mainAccount = parentAccount ?? account;
        const accountXpub = "xpub" in mainAccount ? mainAccount.xpub ?? "" : "";
        const currentTime = Date.now();
        return proposals.reduce((sum, proposal) => {
            const isOutgoing = proposal.sender === accountXpub;
            const isExpired = currentTime > proposal.expires_at_micros / 1000;
            if (isOutgoing && isExpired) {
                return sum.plus(new bignumber_js_1.default(proposal.amount));
            }
            return sum;
        }, new bignumber_js_1.default(0));
    }, [account, accounts]);
};
exports.useWithdrawableBalance = useWithdrawableBalance;
//# sourceMappingURL=react.js.map