import network from "@ledgerhq/live-network";
function adaptOp(backendOp) {
    const { date } = backendOp.tx;
    const newDate = new Date(date);
    return {
        ...backendOp,
        value: BigInt(backendOp.value),
        tx: { ...backendOp.tx, fees: BigInt(backendOp.tx.fees), date: newDate },
    };
}
const ALPACA_URL = "http://0.0.0.0:3000";
const buildBroadcast = networkFamily => async function broadcast(signedOperation) {
    const { data } = await network({
        method: "POST",
        url: `${ALPACA_URL}/${networkFamily}/transaction/broadcast`,
        data: {
            rawTransaction: signedOperation,
        },
    });
    return data.transactionIdentifier;
};
const buildCombine = networkFamily => async function combine(tx, signature, pubKey) {
    const { data } = await network({
        method: "POST",
        url: `${ALPACA_URL}/${networkFamily}/transaction/combine`,
        data: {
            raw_transaction: tx,
            signature: signature,
            pubkey: pubKey,
        },
    });
    return data.signedTransaction;
};
const buildEstimateFees = networkFamily => async function estimateFees(intent) {
    const { data } = await network({
        method: "POST",
        url: `${ALPACA_URL}/${networkFamily}/transaction/estimate`,
        data: {
            intent: {
                ...intent,
                amount: intent.amount.toString(10),
            },
        },
    });
    return {
        value: BigInt(data.fee),
    };
};
const buildValidateIntent = networkFamily => async function validateIntent(transaction) {
    const { data } = await network({
        method: "POST",
        url: `${ALPACA_URL}/${networkFamily}/transaction/validate`,
        data: {
            transaction,
        },
    });
    return data;
};
const buildGetBalance = (networkFamily) => async function getBalance(address) {
    const { data } = await network({
        method: "GET",
        url: `${ALPACA_URL}/${networkFamily}/account/${address}/balance`,
    });
    return [
        {
            value: BigInt(data.value),
            asset: data.asset,
            locked: BigInt(data?.locked ?? "0"),
        },
    ];
};
const buildGetSequence = (networkFamily) => async function getSequence(address) {
    const { data } = await network({
        method: "GET",
        url: `${ALPACA_URL}/${networkFamily}/account/${address}/info`,
    });
    return data;
};
const buildListOperations = networkFamily => async function listOperations(address, pagination = { minHeight: 0, order: "asc" }) {
    const { data } = await network({
        method: "GET",
        url: `${ALPACA_URL}/${networkFamily}/account/${address}/operations`,
        data: {
            from: pagination.minHeight,
        },
    });
    return [data.operations.map(op => adaptOp(op)), ""];
};
const buildLastBlock = networkFamily => async function lastBlock() {
    const { data } = await network({
        method: "GET",
        url: `${ALPACA_URL}/${networkFamily}/lastblock`,
    });
    return {
        height: data.height,
        time: new Date(data.time),
        hash: data.hash,
    };
};
const buildCraftTransaction = networkFamily => async function craftTransaction(intent) {
    const { data } = await network({
        method: "POST",
        url: `${ALPACA_URL}/${networkFamily}/transaction/encode`,
        data: {
            intent: {
                ...intent,
                amount: intent.amount.toString(10),
            },
        },
    });
    return data;
};
export const getNetworkAlpacaApi = (networkFamily) => ({
    broadcast: buildBroadcast(networkFamily),
    combine: buildCombine(networkFamily),
    validateIntent: buildValidateIntent(networkFamily),
    estimateFees: buildEstimateFees(networkFamily),
    getBalance: buildGetBalance(networkFamily),
    getSequence: buildGetSequence(networkFamily),
    listOperations: buildListOperations(networkFamily),
    lastBlock: buildLastBlock(networkFamily),
    craftTransaction: buildCraftTransaction(networkFamily),
    craftRawTransaction: (_transaction, _sender, _publicKey, _sequence) => {
        throw new Error("craftRawTransaction is not supported");
    },
    getBlock(_height) {
        throw new Error("getBlock is not supported");
    },
    getBlockInfo(_height) {
        throw new Error("getBlockInfo is not supported");
    },
    getStakes(_address, _cursor) {
        throw new Error("getStakes is not supported");
    },
    getRewards(_address, _cursor) {
        throw new Error("getRewards is not supported");
    },
    getValidators(_cursor) {
        throw new Error("getValidators is not supported");
    },
});
//# sourceMappingURL=network-alpaca.js.map