"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Telemetry = void 0;
const chalk_1 = __importDefault(require("chalk"));
const package_json_1 = __importDefault(require("../package.json"));
const logger_1 = __importDefault(require("./logger"));
const fetch_1 = require("./fetch");
const globalConfig_1 = require("./globalConfig");
const TELEMETRY_ENDPOINT = 'https://api.promptfoo.dev/telemetry';
const TELEMETRY_TIMEOUT_MS = 1000;
class Telemetry {
    constructor() {
        this.events = [];
    }
    get disabled() {
        return process.env.PROMPTFOO_DISABLE_TELEMETRY === '1';
    }
    record(eventName, properties) {
        if (!this.disabled) {
            this.events.push({
                event: eventName,
                packageVersion: package_json_1.default.version,
                properties,
            });
        }
    }
    maybeShowNotice() {
        if (!this.disabled && (0, globalConfig_1.maybeRecordFirstRun)()) {
            logger_1.default.info(chalk_1.default.gray('Anonymous telemetry is enabled. For more info, see https://www.promptfoo.dev/docs/configuration/telemetry'));
        }
    }
    async send() {
        if (!this.disabled && this.events.length > 0) {
            try {
                const response = await (0, fetch_1.fetchWithTimeout)(TELEMETRY_ENDPOINT, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(this.events),
                }, TELEMETRY_TIMEOUT_MS);
                if (response.ok) {
                    this.events = [];
                }
            }
            catch (err) { }
        }
    }
}
exports.Telemetry = Telemetry;
const telemetry = new Telemetry();
exports.default = telemetry;
//# sourceMappingURL=telemetry.js.map