"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.archiveIso20022 = archiveIso20022;
exports.requestTokenMint = requestTokenMint;
exports.requestTokenTransfer = requestTokenTransfer;
const axios_1 = __importDefault(require("axios"));
async function archiveIso20022(payload) {
    const base = (process.env.TOKEN_AGGREGATION_URL || 'http://localhost:3000').replace(/\/$/, '');
    const key = process.env.OMNL_API_KEY || '';
    const headers = { 'Content-Type': 'application/json' };
    if (key)
        headers.Authorization = `Bearer ${key}`;
    const { data } = await axios_1.default.post(`${base}/api/v1/omnl/iso20022/messages`, {
        messageType: payload.messageType,
        direction: payload.direction,
        payload: payload.raw,
        metadata: { settlementRef: payload.settlementRef },
    }, { headers, timeout: 60000 });
    return { messageId: String(data.messageId ?? data.id ?? payload.settlementRef) };
}
async function requestTokenMint(payload) {
    const base = (process.env.TOKEN_AGGREGATION_URL || 'http://localhost:3000').replace(/\/$/, '');
    const key = process.env.OMNL_API_KEY || '';
    const headers = { 'Content-Type': 'application/json' };
    if (key)
        headers.Authorization = `Bearer ${key}`;
    try {
        const { data } = await axios_1.default.post(`${base}/api/v1/omnl/settlement/token-load`, payload, { headers, timeout: 120000 });
        return {
            txHash: data.txHash,
            status: String(data.status ?? (payload.dryRun ? 'DRY_RUN' : 'SUBMITTED')),
        };
    }
    catch (err) {
        if (axios_1.default.isAxiosError(err) && err.response?.status === 404) {
            return {
                status: payload.dryRun ? 'DRY_RUN' : 'PENDING_CHAIN_ENDPOINT',
            };
        }
        throw err;
    }
}
async function requestTokenTransfer(payload) {
    const base = (process.env.TOKEN_AGGREGATION_URL || 'http://localhost:3000').replace(/\/$/, '');
    const key = process.env.OMNL_API_KEY || '';
    const headers = { 'Content-Type': 'application/json' };
    if (key)
        headers.Authorization = `Bearer ${key}`;
    try {
        const { data } = await axios_1.default.post(`${base}/api/v1/omnl/settlement/token-transfer`, payload, { headers, timeout: 120000 });
        return {
            txHash: data.txHash,
            status: String(data.status ?? (payload.dryRun ? 'DRY_RUN' : 'SUBMITTED')),
        };
    }
    catch (err) {
        if (axios_1.default.isAxiosError(err) && err.response?.status === 404) {
            return {
                status: payload.dryRun ? 'DRY_RUN' : 'PENDING_CHAIN_ENDPOINT',
            };
        }
        throw err;
    }
}
