"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultRefundAPI = void 0;
const utils_1 = require("../utils");
class DefaultRefundAPI {
    constructor(api, btcNetwork, electrsAPI, wrappedCurrency, transactionAPI) {
        this.api = api;
        this.btcNetwork = btcNetwork;
        this.electrsAPI = electrsAPI;
        this.wrappedCurrency = wrappedCurrency;
        this.transactionAPI = transactionAPI;
    }
    execute(requestId, btcTxId) {
        return __awaiter(this, void 0, void 0, function* () {
            const parsedRequestId = (0, utils_1.ensureHashEncoded)(this.api, requestId);
            const txInclusionDetails = yield (0, utils_1.getTxProof)(this.electrsAPI, btcTxId);
            const requestTx = this.api.tx.refund.executeRefund(parsedRequestId, txInclusionDetails.merkleProof, txInclusionDetails.rawTx);
            yield this.transactionAPI.sendLogged(requestTx, this.api.events.refund.ExecuteRefund, true);
        });
    }
    list() {
        return __awaiter(this, void 0, void 0, function* () {
            const head = yield this.api.rpc.chain.getFinalizedHead();
            const refundRequests = yield this.api.query.refund.refundRequests.entriesAt(head);
            return refundRequests
                .filter(([_, req]) => req.isSome.valueOf)
                .map((v) => (0, utils_1.parseRefundRequest)(v[1].unwrap(), this.btcNetwork, this.wrappedCurrency));
        });
    }
    getRequestById(refundId) {
        return __awaiter(this, void 0, void 0, function* () {
            const head = yield this.api.rpc.chain.getFinalizedHead();
            try {
                return (0, utils_1.parseRefundRequest)((yield this.api.query.refund.refundRequests.at(head, refundId)).unwrap(), this.btcNetwork, this.wrappedCurrency);
            }
            catch (error) {
                return Promise.reject(new Error(`Error fetching refund request by issue id: ${error}`));
            }
        });
    }
    getRequestByIssueId(issueId) {
        return __awaiter(this, void 0, void 0, function* () {
            const list = yield this.list();
            const request = list.find((request) => request.issueId === issueId);
            if (!request) {
                return Promise.reject(new Error(`No refund request fund for issue ID ${issueId}`));
            }
            return request;
        });
    }
}
exports.DefaultRefundAPI = DefaultRefundAPI;
//# sourceMappingURL=refund.js.map