"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addressRoute = void 0;
const express_1 = __importDefault(require("express"));
const logger_1 = __importDefault(require("../../logger"));
const chain_state_1 = require("../../providers/chain-state");
const apiUtils_1 = require("../apiUtils");
const router = express_1.default.Router({ mergeParams: true });
async function streamCoins(req, res) {
    try {
        const { chain, network, address } = req.params;
        const { unspent, limit = 10, since } = req.query;
        const payload = {
            chain,
            network,
            address,
            req,
            res,
            args: { ...req.query, unspent, limit, since }
        };
        await chain_state_1.ChainStateProvider.streamAddressTransactions(payload);
    }
    catch (err) {
        logger_1.default.error('Error streaming coins: %o', err.stack || err.message || err);
        (0, apiUtils_1.respondWithError)(res, err);
    }
}
router.get('/:address', streamCoins);
router.get('/:address/txs', streamCoins);
router.get('/:address/coins', streamCoins);
router.get('/:address/balance', async function (req, res) {
    const { address, chain, network } = req.params;
    try {
        const result = await chain_state_1.ChainStateProvider.getBalanceForAddress({
            chain,
            network,
            address,
            args: req.query
        });
        return res.send(result || { confirmed: 0, unconfirmed: 0, balance: 0 });
    }
    catch (err) {
        logger_1.default.error('Error getting address balance: %o', err.stack || err.message || err);
        return (0, apiUtils_1.respondWithError)(res, err);
    }
});
exports.addressRoute = {
    router,
    path: '/address'
};
//# sourceMappingURL=address.js.map