"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Events = void 0;
exports.setDefaultsForEvent = setDefaultsForEvent;
const utils_js_1 = require("./utils.cjs");
/**
 * Events are used to track usage of your product or service.
 * Events are processed asynchronously by the meters, so they may not be immediately available for querying.
 */
class Events {
    client;
    constructor(client) {
        this.client = client;
    }
    /**
     * Ingests an event or batch of events
     * @param events - The events to ingest
     * @param signal - An optional abort signal
     * @returns The ingested events
     */
    async ingest(events, options) {
        const body = await Promise.all((Array.isArray(events) ? events : [events]).map(setDefaultsForEvent));
        const resp = await this.client.POST('/api/v1/events', {
            body,
            headers: {
                'Content-Type': 'application/cloudevents-batch+json',
            },
            ...options,
        });
        return (0, utils_js_1.transformResponse)(resp);
    }
    /**
     * List events
     * @param params - The query parameters
     * @param options - Optional request options
     * @returns The events
     */
    async list(params, options) {
        const resp = await this.client.GET('/api/v1/events', {
            params: { query: params },
            ...options,
        });
        return (0, utils_js_1.transformResponse)(resp);
    }
    /**
     * List events (V2)
     * @description List ingested events with advanced filtering and cursor pagination.
     * @param params - The query parameters
     * @param options - Optional request options
     * @returns The events
     */
    async listV2(params, options) {
        const resp = await this.client.GET('/api/v2/events', {
            params: { query: params },
            ...options,
        });
        return (0, utils_js_1.transformResponse)(resp);
    }
}
exports.Events = Events;
/**
 * Sets the defaults for an event
 * @param ev - The event to set the defaults for
 * @returns The event with the defaults set
 */
async function setDefaultsForEvent(ev) {
    return {
        ...ev,
        id: ev.id ?? (await generateId()),
        source: ev.source ?? '@openmeter/sdk',
        specversion: ev.specversion ?? '1.0',
        time: ev.time ?? new Date(),
    };
}
let _randomUUID;
// One-off attempt to load node:crypto and capture randomUUID (if present)
async function loadUUIDProvider() {
    if (_randomUUID !== undefined) {
        // already tried
        return _randomUUID;
    }
    try {
        const c = await import('node:crypto');
        if (typeof c.randomUUID === 'function') {
            // available
            _randomUUID = c.randomUUID.bind(c);
        }
    }
    catch {
        // not-available
    }
    return _randomUUID;
}
/**
 * Generates a random ID
 * @returns A random ID
 */
async function generateId() {
    const randomUUID = await loadUUIDProvider();
    if (randomUUID) {
        return randomUUID();
    }
    // Fallback to semi-random ID
    const bytes = new Uint8Array(16);
    for (let i = 0; i < 16; i++) {
        bytes[i] = (Math.random() * 256) | 0;
    }
    bytes[6] = (bytes[6] & 0x0f) | 0x40;
    bytes[8] = (bytes[8] & 0x3f) | 0x80;
    const hex = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('');
    return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
//# sourceMappingURL=events.cjs.map