"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HybxProductionRail = void 0;
const axios_1 = __importDefault(require("axios"));
const config_1 = require("../config");
const integration_foundation_1 = require("@dbis/integration-foundation");
/** Production HYBX rail client for real settlement dispatch */
class HybxProductionRail {
    baseUrl;
    apiKey;
    clientSecret;
    constructor() {
        const settlement = (0, config_1.loadConfig)();
        const allowLive = process.env.SETTLEMENT_ALLOW_HYBX_PRODUCTION === '1' ||
            settlement.production.allowHybxProduction;
        const cfg = (0, integration_foundation_1.loadHybxConfig)({ testMode: !allowLive });
        if (cfg.environment === 'production' && !allowLive) {
            throw new Error('HYBX production requires SETTLEMENT_ALLOW_HYBX_PRODUCTION=1 or production.allowHybxProduction in config');
        }
        this.baseUrl = cfg.baseUrl.replace(/\/$/, '');
        this.apiKey = cfg.apiKey;
        this.clientSecret = cfg.clientSecret;
    }
    async dispatchPayment(payload) {
        const sidecar = process.env.HYBX_MIFOS_FINERACT_SIDECAR_URL?.replace(/\/$/, '');
        const url = sidecar ? `${sidecar}/v1/rtgs/payments` : `${this.baseUrl}/v1/payments`;
        const { data } = await axios_1.default.post(url, {
            externalId: payload.idempotencyKey,
            amount: payload.amount,
            currency: payload.currency,
            beneficiary: {
                iban: payload.creditorIban,
                bic: payload.creditorBic,
                name: payload.beneficiaryName,
            },
            remittanceInformation: payload.remittanceInfo,
            rail: payload.rail,
        }, {
            headers: {
                'Content-Type': 'application/json',
                'X-API-Key': this.apiKey,
                Authorization: `Bearer ${this.clientSecret}`,
            },
            timeout: 120000,
        });
        return {
            paymentId: String(data.paymentId ?? data.id ?? payload.idempotencyKey),
            status: String(data.status ?? 'DISPATCHED'),
        };
    }
}
exports.HybxProductionRail = HybxProductionRail;
