"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapToCanonical = mapToCanonical;
const ethers_1 = require("ethers");
const checkpoint_core_1 = require("@dbis/checkpoint-core");
function msgTypeCode(parsed) {
    if (parsed.format === 'ktt')
        return checkpoint_core_1.MSG_TYPE.KTT;
    if (parsed.format === 'swift_fin') {
        if (parsed.messageType === 'MT103')
            return checkpoint_core_1.MSG_TYPE.MT103;
        return checkpoint_core_1.MSG_TYPE.UNKNOWN;
    }
    if (parsed.format === 'iso20022') {
        if (parsed.messageType === 'pacs.008')
            return checkpoint_core_1.MSG_TYPE.PACS008;
        if (parsed.messageType === 'pain.001')
            return checkpoint_core_1.MSG_TYPE.PAIN001;
    }
    return checkpoint_core_1.MSG_TYPE.UNKNOWN;
}
function msgTypeLabel(parsed) {
    if (parsed.format === 'ktt')
        return parsed.messageType;
    if (parsed.format === 'swift_fin' && parsed.messageType === 'MT103')
        return 'MT103';
    if (parsed.format === 'iso20022' && parsed.messageType === 'pacs.008')
        return 'pacs.008';
    if (parsed.format === 'iso20022' && parsed.messageType === 'pain.001')
        return 'pain.001';
    return 'chain138.synthetic';
}
function settlementMethod(parsed) {
    if (parsed.format === 'ktt')
        return 'KTT';
    if (parsed.format === 'swift_fin')
        return 'SWIFT';
    return 'ISO20022';
}
function pickString(parsed, ...keys) {
    const obj = parsed.parsed;
    for (const k of keys) {
        const v = obj[k];
        if (typeof v === 'string' && v.trim())
            return v.trim();
    }
    return '';
}
function mapToCanonical(parsed, dedupeKey) {
    const instructionId = pickString(parsed, 'instructionId', 'senderReference', 'transactionReference', 'messageId') ||
        `KTT-${dedupeKey.slice(0, 16)}`;
    const endToEndId = pickString(parsed, 'endToEndId', 'transactionReference', 'senderReference') || instructionId;
    const msgId = pickString(parsed, 'messageId', 'senderReference') || instructionId;
    const uetr = pickString(parsed, 'uetr') || '';
    const debtorId = pickString(parsed, 'orderingCustomer', 'debtor', 'sendingInstitution', 'debtorId') || 'UNKNOWN';
    const creditorId = pickString(parsed, 'beneficiaryCustomer', 'creditor', 'beneficiaryInstitution', 'creditorId') ||
        'UNKNOWN';
    const currencyCode = pickString(parsed, 'currency', 'currencyCode') || 'USD';
    const amountRaw = pickString(parsed, 'amount') || '0';
    const purpose = pickString(parsed, 'remittanceInfo', 'remittance', 'purpose', 'senderToReceiverInfo', 'narrative') ||
        `${parsed.format}/${parsed.messageType}`;
    const instructionIdBytes32 = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(`INSTR:${instructionId}`));
    const uetrBytes32 = uetr
        ? ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(`UETR:${uetr}`))
        : ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(`UETR:${dedupeKey}`));
    let authAttestationHash;
    if (parsed.format === 'ktt') {
        const fromParsed = pickString(parsed, 'authAttestationHash');
        if (fromParsed) {
            authAttestationHash = fromParsed.startsWith('0x') ? fromParsed : (0, checkpoint_core_1.hashShortUtf8)(fromParsed);
        }
        else if (parsed.authAttestationHash) {
            authAttestationHash = parsed.authAttestationHash;
        }
        else if (parsed.testKeyProof && pickString(parsed, 'normalizedBody')) {
            authAttestationHash = (0, checkpoint_core_1.authAttestationHashFromProof)(parsed.testKeyProof, pickString(parsed, 'normalizedBody'));
        }
    }
    const base = {
        msgType: msgTypeLabel(parsed),
        msgTypeCode: msgTypeCode(parsed),
        instructionId,
        instructionIdBytes32,
        endToEndId,
        endToEndIdHash: (0, checkpoint_core_1.hashShortUtf8)(endToEndId),
        msgId,
        uetr: uetr || dedupeKey.slice(0, 36),
        uetrBytes32,
        accountRefId: debtorId,
        counterpartyRefId: creditorId,
        debtorId,
        creditorId,
        purpose,
        settlementMethod: settlementMethod(parsed),
        categoryPurpose: 'CBFF',
        currencyCode,
        amountRaw,
        amountSmallestUnit: amountRaw.replace(/[,.]/g, '') || '0',
        debtorRefHash: (0, checkpoint_core_1.hashShortUtf8)(debtorId),
        creditorRefHash: (0, checkpoint_core_1.hashShortUtf8)(creditorId),
        purposeHash: (0, checkpoint_core_1.hashShortUtf8)(purpose),
        chain138TxHash: pickString(parsed, 'txId', 'chain138TxHash'),
        valueDateIso: pickString(parsed, 'valueDate') || undefined,
        ...(authAttestationHash ? { authAttestationHash } : {}),
    };
    const payloadHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(JSON.stringify({ ...base, sourceFormat: parsed.format, messageType: parsed.messageType })));
    return { ...base, payloadHash };
}
//# sourceMappingURL=mapToCanonical.js.map