/**
 * Audit Resource
 *
 * Auditor-only access to the indexer. Wraps the WASM SDK's
 * `auditQueryTransactions` / `auditQueryBalances` exports, which in turn drive
 * the canonical `privacy_boost_core::operations::audit` flow:
 *
 *   1. Build EIP-712 typed data identifying (auditor, account, action).
 *   2. Connected wallet signs via `eth_signTypedData_v4`.
 *   3. POST to `/api/v1/audit/{transactions,balances}` with the
 *      operator-provided `X-Admin-Key` header.
 *
 * The SDK does no networking or signing in TypeScript — the WASM binding is
 * the single implementation, shared with iOS/Android/CLI.
 */
import { silentLogger } from '../utils/logger';
const TAG = 'Audit';
// `accountId in target` only checks key presence, not whether the value is
// non-empty. The AuditTarget union forbids mixing both fields at the type
// level, but defensive JS callers sometimes pass `{ accountId: '', eoaAddress }`
// — the truthy check picks the populated side in that case. If both fields
// are populated we reject the call rather than silently dropping one, since
// the WASM layer would otherwise audit whichever field happened to be checked
// first and the caller's intent is ambiguous.
function flattenTarget(target) {
    const hasAccountId = 'accountId' in target && !!target.accountId;
    const hasEoaAddress = 'eoaAddress' in target && !!target.eoaAddress;
    if (hasAccountId && hasEoaAddress) {
        throw new Error('Audit target must specify exactly one of accountId or eoaAddress, not both');
    }
    if (hasAccountId) {
        return { accountId: target.accountId };
    }
    if (hasEoaAddress) {
        return { eoaAddress: target.eoaAddress };
    }
    throw new Error('Audit target requires either accountId or eoaAddress');
}
// Replace `adminKey` with a redaction marker before logging. The admin key is
// a cross-auditor server credential (SUPERADMIN_KEY header value); spilling it
// to a browser console, CLI --verbose log, or CI artifact is a privilege leak.
function redactAdminKey(payload) {
    return { ...payload, adminKey: payload.adminKey ? '[redacted]' : payload.adminKey };
}
export class AuditResource {
    getWasm;
    log;
    constructor(getWasm, logger) {
        this.getWasm = getWasm;
        this.log = logger ?? silentLogger;
    }
    /** Run a signed audit transactions query. The connected wallet signs the EIP-712 typed data. */
    async queryTransactions(options) {
        const wasm = this.requireWasm();
        const payload = {
            adminKey: options.adminKey,
            auditGatewayAddress: options.auditGatewayAddress,
            chainId: options.chainId,
            txType: options.txType ?? '',
            tokenId: options.tokenId,
            createdFrom: options.createdFrom,
            createdTo: options.createdTo,
            limit: options.limit ?? 0,
            offset: options.offset ?? 0,
            ...flattenTarget(options.target),
        };
        this.log.debug(TAG, 'queryTransactions', redactAdminKey(payload));
        const result = await wasm.auditQueryTransactions(payload);
        return result;
    }
    /** Run a signed audit balances query. */
    async queryBalances(options) {
        const wasm = this.requireWasm();
        const payload = {
            adminKey: options.adminKey,
            auditGatewayAddress: options.auditGatewayAddress,
            chainId: options.chainId,
            ...flattenTarget(options.target),
        };
        this.log.debug(TAG, 'queryBalances', redactAdminKey(payload));
        const result = await wasm.auditQueryBalances(payload);
        return result;
    }
    requireWasm() {
        const wasm = this.getWasm();
        if (!wasm) {
            throw new Error('Audit operations require the local WASM SDK. They are not available when the SDK ' +
                'runs in iframe key-vault mode.');
        }
        return wasm;
    }
}
//# sourceMappingURL=Audit.js.map