import { readTelemetryConfig, writeTelemetryConfig, getOrCreateInstallationId, } from '../engine/telemetry/index.js';
import { compactResult, compactError, formatKeyValue } from './output-formatter.js';
export async function handleConfigureTelemetry(input) {
    try {
        const installationId = await getOrCreateInstallationId();
        const existing = await readTelemetryConfig();
        if (input.action === 'status') {
            const observatoryStatus = existing?.enableObservatory === true
                ? ' Observatory: ENABLED (cross-project insights active).'
                : ' Observatory: DISABLED (use enableObservatory=true to opt in).';
            const result = {
                action: 'status',
                enabled: existing?.enabled ?? false,
                installationId,
                message: (existing?.enabled
                    ? 'Telemetry is ENABLED. Anonymous usage events are being sent to help improve Planu.'
                    : 'Telemetry is DISABLED. No data is being collected.') + observatoryStatus,
            };
            return compactResult(formatKeyValue(result));
        }
        if (input.action === 'enable') {
            await writeTelemetryConfig({
                enabled: true,
                installationId,
                promptedAt: existing?.promptedAt ?? new Date().toISOString(),
                enableObservatory: input.enableObservatory ?? existing?.enableObservatory,
            });
            const observatoryNote = input.enableObservatory === true
                ? ' Observatory ENABLED: cross-project insights will be fetched from planu.dev.'
                : '';
            const result = {
                action: 'enable',
                enabled: true,
                installationId,
                message: 'Telemetry ENABLED. Thank you! Only anonymous usage data is collected — ' +
                    'no paths, emails, or personal info. Your installation ID is ' +
                    `${installationId}. Run configure_telemetry with action=disable to opt out anytime.` +
                    observatoryNote,
            };
            return compactResult(formatKeyValue(result));
        }
        // disable
        await writeTelemetryConfig({
            enabled: false,
            installationId,
            promptedAt: existing?.promptedAt ?? new Date().toISOString(),
            enableObservatory: existing?.enableObservatory,
        });
        const result = {
            action: 'disable',
            enabled: false,
            installationId,
            message: 'Telemetry DISABLED. No usage data will be sent.',
        };
        return compactResult(formatKeyValue(result));
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        return compactError(`Error: ${message}`);
    }
}
//# sourceMappingURL=configure-telemetry.js.map