// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// engine/telemetry/telemetry-store.ts — SPEC-200: Read/write telemetry config from ~/.planu/
// Stores opt-in preference and installation ID outside the project directory.
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { randomUUID } from 'node:crypto';
const PLANU_DIR = join(homedir(), '.planu');
const TELEMETRY_FILE = join(PLANU_DIR, 'telemetry.json');
export async function readTelemetryConfig() {
    try {
        const raw = await readFile(TELEMETRY_FILE, 'utf-8');
        return JSON.parse(raw);
    }
    catch {
        return null;
    }
}
export async function writeTelemetryConfig(config) {
    await mkdir(PLANU_DIR, { recursive: true });
    await writeFile(TELEMETRY_FILE, JSON.stringify(config, null, 2) + '\n', 'utf-8');
}
export async function getOrCreateInstallationId() {
    const config = await readTelemetryConfig();
    if (config?.installationId) {
        return config.installationId;
    }
    const id = randomUUID();
    await writeTelemetryConfig({
        enabled: false,
        installationId: id,
        promptedAt: '',
    });
    return id;
}
/**
 * Returns true if telemetry is enabled.
 * Default: ENABLED (opt-out model). Disable by setting PLANU_TELEMETRY=off
 * or by explicitly calling configure_telemetry with enabled=false.
 */
export async function isTelemetryEnabled() {
    if (process.env.PLANU_TELEMETRY === 'off') {
        return false;
    }
    const config = await readTelemetryConfig();
    return config?.enabled !== false; // default true unless explicitly disabled
}
/** Returns true if the user has never been shown the opt-in prompt. */
export async function hasNeverBeenPrompted() {
    const config = await readTelemetryConfig();
    return !config?.promptedAt;
}
/** Mark that the user was shown the opt-in prompt (so we don't show it again). */
export async function markAsPrompted() {
    const existing = await readTelemetryConfig();
    const id = existing?.installationId ?? randomUUID();
    await writeTelemetryConfig({
        enabled: existing?.enabled ?? false,
        installationId: id,
        promptedAt: new Date().toISOString(),
    });
}
//# sourceMappingURL=telemetry-store.js.map