/**
 * src/integrations/zatca/tlv.ts
 * TLV (Tag-Length-Value) encoder for ZATCA Phase 1 QR codes.
 *
 * Per ZATCA simplified-tax-invoice spec, the QR carries 5 tags:
 *   1: Seller name (UTF-8)
 *   2: Seller VAT registration number
 *   3: Invoice timestamp (ISO 8601, with timezone)
 *   4: Invoice total with VAT (string, 2 decimal places)
 *   5: VAT total (string, 2 decimal places)
 *
 * Phase-1 length encoding is single-byte (max 255 bytes per value), which is
 * fine for typical seller names. Phase 2 adds tags 6–8 (cryptographic stamp,
 * signature, public key) — those land when the signing module ships.
 */
function encodeTLV(tag, value) {
    if (tag < 1 || tag > 0xff)
        throw new Error(`TLV tag out of range: ${tag}`);
    const valueBuf = Buffer.from(value, 'utf-8');
    if (valueBuf.length > 0xff) {
        throw new Error(`TLV value for tag ${tag} exceeds 255 bytes (got ${valueBuf.length}). ` +
            `Phase-1 spec uses single-byte length; truncate or wait for Phase 2.`);
    }
    return Buffer.concat([Buffer.from([tag, valueBuf.length]), valueBuf]);
}
/** Build the Phase-1 QR payload as a base64-encoded TLV blob. */
export function buildPhase1QrPayload(fields) {
    const buf = Buffer.concat([
        encodeTLV(1, fields.sellerName),
        encodeTLV(2, fields.sellerVatNumber),
        encodeTLV(3, fields.invoiceTimestamp),
        encodeTLV(4, fields.totalWithVat),
        encodeTLV(5, fields.vatTotal),
    ]);
    return buf.toString('base64');
}
//# sourceMappingURL=tlv.js.map