"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.persistIso20022ForLeaves = persistIso20022ForLeaves; const crypto_1 = require("crypto"); const fs_1 = require("fs"); const path_1 = require("path"); const checkpoint_core_1 = require("@dbis/checkpoint-core"); function storeDir() { const raw = process.env.OMNL_ISO20022_STORE_DIR?.trim(); if (raw) return (0, path_1.resolve)(raw); const root = process.env.PROXMOX_ROOT || process.cwd(); return (0, path_1.resolve)(root, 'config/iso20022-omnl/messages'); } function retentionUntil(fromIso) { const y = parseInt(process.env.OMNL_ISO20022_RETENTION_YEARS || '10', 10); const d = new Date(fromIso); d.setFullYear(d.getFullYear() + (Number.isFinite(y) && y > 0 ? y : 10)); return d.toISOString(); } function persistIso20022ForLeaves(leaves, batchId) { if (process.env.CHECKPOINT_ISO_OMNL_STORE === '0') return []; const dir = storeDir(); (0, fs_1.mkdirSync)(dir, { recursive: true }); const out = []; for (const leaf of leaves) { const iso = leaf.iso20022 ?? (0, checkpoint_core_1.mapPaymentLeafToCanonical)(leaf); const xml = (0, checkpoint_core_1.canonicalToPacs008Xml)(iso); const id = (0, crypto_1.randomUUID)(); const receivedAt = new Date().toISOString(); const record = { id, messageType: 'pacs.008', receivedAt, retentionUntil: retentionUntil(receivedAt), uetr: iso.uetr, instructionId: iso.instructionId, settlementOrChainRef: leaf.txHash, accountingRef: `checkpoint-batch-${batchId.toString()}`, payloadSha256: (0, crypto_1.createHash)('sha256').update(xml, 'utf8').digest('hex'), payload: xml, canonical: iso, }; (0, fs_1.writeFileSync)((0, path_1.join)(dir, `${id}.json`), JSON.stringify(record, null, 2), 'utf8'); out.push({ id, chain138TxHash: leaf.txHash, instructionId: iso.instructionId }); } return out; }