"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const prepareTransaction_1 = require("../prepareTransaction");
const alpaca_1 = require("../alpaca");
const utils_1 = require("../utils");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
jest.mock("../alpaca", () => ({
    getAlpacaApi: jest.fn(),
}));
jest.mock("../utils", () => ({
    transactionToIntent: jest.fn(),
}));
describe("genericPrepareTransaction", () => {
    const network = "testnet";
    const kind = "local";
    const account = {
        id: "test-account",
        address: "0xabc",
    };
    const baseTransaction = {
        amount: new bignumber_js_1.default(100_000),
        fees: new bignumber_js_1.default(500),
        recipient: "0xrecipient",
    };
    const txIntent = { mock: "intent" };
    beforeEach(() => {
        jest.clearAllMocks();
        utils_1.transactionToIntent.mockReturnValue(txIntent);
    });
    it("updates fees if they differ", async () => {
        const newFee = new bignumber_js_1.default(700);
        alpaca_1.getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: newFee }),
        });
        const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
        const result = await prepareTransaction(account, { ...baseTransaction });
        expect(result.fees.toString()).toBe(newFee.toString());
        expect(utils_1.transactionToIntent).toHaveBeenCalledWith(account, expect.objectContaining(baseTransaction));
    });
    it("returns original transaction if fees are the same", async () => {
        const sameFee = baseTransaction.fees;
        alpaca_1.getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: sameFee }),
        });
        const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
        const result = await prepareTransaction(account, baseTransaction);
        expect(result).toBe(baseTransaction);
    });
    it("sets fee if original fees are undefined", async () => {
        const newFee = new bignumber_js_1.default(1234);
        alpaca_1.getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: newFee }),
        });
        const txWithoutFees = { ...baseTransaction, fees: undefined };
        const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
        const result = await prepareTransaction(account, txWithoutFees);
        expect(result.fees.toString()).toBe(newFee.toString());
        expect(result).not.toBe(txWithoutFees);
    });
    it("returns original if fees are BigNumber-equal but different instance", async () => {
        const sameValue = new bignumber_js_1.default(baseTransaction.fees.toString()); // different instance
        alpaca_1.getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: sameValue }),
        });
        const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
        const result = await prepareTransaction(account, baseTransaction);
        expect(result).toBe(baseTransaction); // still same reference
    });
});
//# sourceMappingURL=prepareTransaction.test.js.map