"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.forwardSwiftToListener = forwardSwiftToListener;
exports.buildMt103Stub = buildMt103Stub;
exports.buildMt102Stub = buildMt102Stub;
const axios_1 = __importDefault(require("axios"));
async function forwardSwiftToListener(raw) {
    const url = (process.env.SWIFT_LISTENER_URL || 'http://192.168.11.114:8788').replace(/\/$/, '');
    const token = process.env.SWIFT_LISTENER_WEBHOOK_TOKEN || '';
    const headers = { 'Content-Type': 'text/plain' };
    if (token)
        headers.Authorization = `Bearer ${token}`;
    await axios_1.default.post(`${url}/inbound`, raw, { headers, timeout: 60000 });
    return { accepted: true };
}
function buildMt103Stub(fields) {
    const bicLine = fields.bic ? `:57A:${fields.bic}\n` : '';
    return `{1:F01OMNLUS33XXXX0000000000}{2:I103BANKUS33XXXXN}{4:
:20:${fields.senderRef}
:23B:CRED
:32A:${fields.valueDate.replace(/-/g, '').slice(2)}${fields.currency}${fields.amount.replace('.', ',')}
:50K:${fields.orderingCustomer}
:59:/${fields.iban}
${fields.beneficiary}
${bicLine}:70:${fields.remittance}
:71A:OUR
-}`;
}
function buildMt102Stub(fields) {
    return `{1:F01OMNLUS33XXXX0000000000}{2:I102BANKUS33XXXXN}{4:
:20:${fields.senderRef}
:21:NONREF
:32B:${fields.currency}${fields.amount.replace('.', ',')}
:50A:${fields.orderingInstitution}
:59:/${fields.iban}
${fields.beneficiary}
:70:${fields.remittance}
-}`;
}
