import { logger } from "../util/logger.js";
import { resolveWeixinRootDir } from "../storage/state-dir.js";
import fs from "node:fs";
import path from "node:path";
//#region extensions/weixin/src/messaging/debug-mode.ts
/**
* Per-bot debug mode toggle, persisted to disk so it survives gateway restarts.
*
* State file: `resolveWeixinRootDir()/debug-mode.json`
* Format:     `{ "accounts": { "<accountId>": true, ... } }`
*
* When enabled, processOneMessage appends a timing summary after each
* AI reply is delivered to the user.
*/
function resolveDebugModePath() {
	return path.join(resolveWeixinRootDir(), "debug-mode.json");
}
function loadState() {
	try {
		const raw = fs.readFileSync(resolveDebugModePath(), "utf-8");
		const parsed = JSON.parse(raw);
		if (parsed && typeof parsed.accounts === "object") return parsed;
	} catch {}
	return { accounts: {} };
}
function saveState(state) {
	const filePath = resolveDebugModePath();
	fs.mkdirSync(path.dirname(filePath), { recursive: true });
	fs.writeFileSync(filePath, JSON.stringify(state, null, 2), "utf-8");
}
/** Toggle debug mode for a bot account. Returns the new state. */
function toggleDebugMode(accountId) {
	const state = loadState();
	const next = !state.accounts[accountId];
	state.accounts[accountId] = next;
	try {
		saveState(state);
	} catch (err) {
		logger.error(`debug-mode: failed to persist state: ${String(err)}`);
	}
	return next;
}
/** Check whether debug mode is active for a bot account. */
function isDebugMode(accountId) {
	return loadState().accounts[accountId] === true;
}
/**
* Reset internal state — only for tests.
* @internal
*/
function _resetForTest() {
	try {
		fs.unlinkSync(resolveDebugModePath());
	} catch {}
}
//#endregion
export { _resetForTest, isDebugMode, toggleDebugMode };

//# sourceMappingURL=debug-mode.js.map