import { a as resolveStateDir } from "./entry.js";
import { n as resolveOpenClawVersion, t as VERSION } from "./version-BcB7HG3l.js";
import { createRequire } from "node:module";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { randomUUID } from "node:crypto";
import { PostHog } from "posthog-node";

//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
	let target = {};
	for (var name in all) {
		__defProp(target, name, {
			get: all[name],
			enumerable: true
		});
	}
	if (!no_symbols) {
		__defProp(target, Symbol.toStringTag, { value: "Module" });
	}
	return target;
};

//#endregion
//#region src/telemetry/config.ts
const TELEMETRY_FILENAME = "telemetry.json";
function telemetryConfigPath() {
	return join(resolveStateDir(), TELEMETRY_FILENAME);
}
function readTelemetryConfig() {
	const configPath = telemetryConfigPath();
	try {
		if (!existsSync(configPath)) return { enabled: true };
		const raw = JSON.parse(readFileSync(configPath, "utf-8"));
		return {
			enabled: raw.enabled !== false,
			noticeShown: raw.noticeShown === true,
			privacyMode: raw.privacyMode !== false,
			anonymousId: typeof raw.anonymousId === "string" ? raw.anonymousId : void 0,
			name: typeof raw.name === "string" && raw.name ? raw.name : void 0,
			email: typeof raw.email === "string" && raw.email ? raw.email : void 0,
			avatar: typeof raw.avatar === "string" && raw.avatar ? raw.avatar : void 0,
			denchOrgId: typeof raw.denchOrgId === "string" && raw.denchOrgId ? raw.denchOrgId : void 0
		};
	} catch {
		return { enabled: true };
	}
}
function writeTelemetryConfig(config) {
	const configPath = telemetryConfigPath();
	const merged = {
		...readTelemetryConfig(),
		...config
	};
	try {
		mkdirSync(dirname(configPath), { recursive: true });
		writeFileSync(configPath, JSON.stringify(merged, null, 2) + "\n", "utf-8");
	} catch {}
}
function markNoticeShown() {
	writeTelemetryConfig({ noticeShown: true });
}
/**
* Read optional person identity fields from telemetry.json.
* Returns null when no identity fields are set.
*/
function readPersonInfo() {
	const config = readTelemetryConfig();
	if (!config.name && !config.email && !config.avatar && !config.denchOrgId) return null;
	const info = {};
	if (config.name) info.name = config.name;
	if (config.email) info.email = config.email;
	if (config.avatar) info.avatar = config.avatar;
	if (config.denchOrgId) info.denchOrgId = config.denchOrgId;
	return info;
}
let _cachedAnonymousId = null;
/**
* Return the persisted install-scoped anonymous ID from telemetry.json,
* generating and writing one on first access.
*/
function getOrCreateAnonymousId() {
	if (_cachedAnonymousId) return _cachedAnonymousId;
	const configPath = telemetryConfigPath();
	try {
		let raw = {};
		if (existsSync(configPath)) raw = JSON.parse(readFileSync(configPath, "utf-8"));
		if (typeof raw.anonymousId === "string" && raw.anonymousId) {
			_cachedAnonymousId = raw.anonymousId;
			return raw.anonymousId;
		}
		const id = randomUUID();
		raw.anonymousId = id;
		mkdirSync(dirname(configPath), { recursive: true });
		writeFileSync(configPath, JSON.stringify(raw, null, 2) + "\n", "utf-8");
		_cachedAnonymousId = id;
		return id;
	} catch {
		const id = randomUUID();
		_cachedAnonymousId = id;
		return id;
	}
}

//#endregion
//#region src/telemetry/telemetry.ts
var telemetry_exports = /* @__PURE__ */ __exportAll({
	isTelemetryEnabled: () => isTelemetryEnabled,
	shutdownTelemetry: () => shutdownTelemetry,
	track: () => track
});
const POSTHOG_KEY = "phc_Nt8s1Ny8062TY9aKT8LWiB4nBuoHNYQXRU9MypWfQCW";
const POSTHOG_HOST = "https://us.i.posthog.com";
let client = null;
let _identified = false;
function isTelemetryEnabled() {
	if (process.env.DO_NOT_TRACK === "1") return false;
	if (process.env.DENCHCLAW_TELEMETRY_DISABLED === "1") return false;
	if (process.env.CI) return false;
	try {
		if (!readTelemetryConfig().enabled) return false;
	} catch {}
	return true;
}
function getMachineContext() {
	return {
		os: process.platform,
		arch: process.arch,
		node_version: process.version,
		denchclaw_version: VERSION,
		openclaw_version: resolveOpenClawVersion()
	};
}
function ensureClient() {
	if (!client) client = new PostHog(POSTHOG_KEY, {
		host: POSTHOG_HOST,
		flushAt: 5,
		flushInterval: 1e4
	});
	return client;
}
function track(event, properties) {
	if (!isTelemetryEnabled()) return;
	if (process.env.DENCHCLAW_TELEMETRY_DEBUG === "1") {
		process.stderr.write(`[telemetry:debug] ${JSON.stringify({
			event,
			properties
		}, null, 2)}\n`);
		return;
	}
	const ph = ensureClient();
	if (!ph) return;
	const distinctId = getOrCreateAnonymousId();
	if (!_identified) {
		_identified = true;
		const person = readPersonInfo();
		if (person) ph.identify({
			distinctId,
			properties: personInfoToPostHogProps(person)
		});
	}
	ph.capture({
		distinctId,
		event,
		properties: {
			...getMachineContext(),
			...properties
		}
	});
}
function personInfoToPostHogProps(person) {
	const props = {};
	if (person.name) props.$name = person.name;
	if (person.email) props.$email = person.email;
	if (person.avatar) props.$avatar = person.avatar;
	if (person.denchOrgId) props.dench_org_id = person.denchOrgId;
	return props;
}
async function shutdownTelemetry() {
	if (client) {
		await client.shutdown();
		client = null;
	}
}

//#endregion
export { markNoticeShown as a, getOrCreateAnonymousId as i, telemetry_exports as n, readTelemetryConfig as o, track as r, writeTelemetryConfig as s, isTelemetryEnabled as t };