"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.postSwapCancelled = exports.postSwapAccepted = void 0;
const crypto_1 = __importDefault(require("crypto"));
const live_network_1 = __importDefault(require("@ledgerhq/live-network"));
const mock_1 = require("./mock");
const isIntegrationTestEnv_1 = require("./utils/isIntegrationTestEnv");
const _1 = require(".");
function createSwapIntentHashes({ provider, fromAccountAddress, toAccountAddress, fromAmount, }) {
    // for example '2025-08-01' used to add a one day unique nonce to the swap intent hash
    const currentday = new Date().toISOString().split("T")[0];
    const swapIntentWithProvider = crypto_1.default
        .createHash("sha256")
        .update(JSON.stringify({
        provider,
        fromAccountAddress,
        toAccountAddress,
        fromAmount,
        currentday,
    }))
        .digest("hex");
    const swapIntentWithoutProvider = crypto_1.default
        .createHash("sha256")
        .update(JSON.stringify({
        fromAccountAddress,
        toAccountAddress,
        fromAmount,
        currentday,
    }))
        .digest("hex");
    return { swapIntentWithProvider, swapIntentWithoutProvider };
}
const postSwapAccepted = async ({ provider, swapId = "", transactionId, swapAppVersion, fromAccountAddress, toAccountAddress, fromAmount, ...rest }) => {
    if ((0, isIntegrationTestEnv_1.isIntegrationTestEnv)())
        return (0, mock_1.mockPostSwapAccepted)({ provider, swapId, transactionId, ...rest });
    /**
     * Since swapId is required by the endpoint, don't call it if we don't have
     * this info
     */
    if (!swapId) {
        return null;
    }
    const { swapIntentWithProvider, swapIntentWithoutProvider } = createSwapIntentHashes({
        provider,
        fromAccountAddress,
        toAccountAddress,
        fromAmount,
    });
    try {
        const ipHeader = (0, _1.getSwapUserIP)();
        const headers = {
            ...(ipHeader || {}),
            ...(swapAppVersion ? { "x-swap-app-version": swapAppVersion } : {}),
        };
        await (0, live_network_1.default)({
            method: "POST",
            url: `${(0, _1.getSwapAPIBaseURL)()}/swap/accepted`,
            data: { provider, swapId, swapIntentWithProvider, swapIntentWithoutProvider, ...rest },
            ...(Object.keys(headers).length > 0 ? { headers } : {}),
        });
    }
    catch (error) {
        console.error(error);
    }
    return null;
};
exports.postSwapAccepted = postSwapAccepted;
const postSwapCancelled = async ({ provider, swapId = "", swapAppVersion, fromAccountAddress, toAccountAddress, fromAmount, seedIdFrom, seedIdTo, refundAddress, payoutAddress, data, ...rest }) => {
    if ((0, isIntegrationTestEnv_1.isIntegrationTestEnv)())
        return (0, mock_1.mockPostSwapCancelled)({ provider, swapId, ...rest });
    /**
     * Since swapId is required by the endpoint, don't call it if we don't have
     * this info
     */
    if (!swapId) {
        return null;
    }
    const { swapIntentWithProvider, swapIntentWithoutProvider } = createSwapIntentHashes({
        provider,
        fromAccountAddress,
        toAccountAddress,
        fromAmount,
    });
    // Check if the refundAddress and payoutAddress match the account addresses, just to eliminate this supposition
    const payloadAddressMatchAccountAddress = fromAccountAddress === refundAddress && toAccountAddress === payoutAddress;
    try {
        const ipHeader = (0, _1.getSwapUserIP)();
        const headers = {
            ...(ipHeader || {}),
            ...(swapAppVersion ? { "x-swap-app-version": swapAppVersion } : {}),
        };
        const shouldIncludeAddresses = rest.statusCode === "WrongDeviceForAccountPayout" ||
            rest.statusCode === "WrongDeviceForAccountRefund" ||
            rest.statusCode === "FeeNotLoaded";
        const requestData = {
            provider,
            swapId,
            swapIntentWithProvider,
            swapIntentWithoutProvider,
            payloadAddressMatchAccountAddress,
            fromAmount,
            ...(shouldIncludeAddresses && {
                fromAccountAddress,
                toAccountAddress,
                payloadRefundAddress: refundAddress,
                payloadPayoutAddress: payoutAddress,
            }),
            maybeSeedMatch: seedIdFrom === seedIdTo,
            data,
            ...rest,
        };
        await (0, live_network_1.default)({
            method: "POST",
            url: `${(0, _1.getSwapAPIBaseURL)()}/swap/cancelled`,
            data: requestData,
            ...(Object.keys(headers).length > 0 ? { headers } : {}),
        });
    }
    catch (error) {
        console.error(error);
    }
    return null;
};
exports.postSwapCancelled = postSwapCancelled;
//# sourceMappingURL=postSwapState.js.map