import { init_loader, loadConfig } from "../../../../config/loader.js";
import { existsSync } from "node:fs";
//#region src/cli/commands/doctor/checks/channel-config.ts
init_loader();
function checkTelegram(cfg) {
	const tg = cfg.channels?.telegram;
	if (!tg) return {
		ok: true,
		messages: [],
		hints: []
	};
	const defaultAcc = tg.accounts?.default;
	const token = defaultAcc?.botToken?.trim() ?? "";
	if (!(tg.enabled === true || token.length > 0)) return {
		ok: true,
		messages: [],
		hints: []
	};
	const messages = [];
	const hints = [];
	if (!token) {
		messages.push("Telegram is enabled but no bot token is set.");
		hints.push("Set channels.telegram.accounts.default.botToken.");
	}
	const dm = (defaultAcc?.dmPolicy ?? tg.dmPolicy) || "pairing";
	if (![
		"pairing",
		"allowlist",
		"open",
		"disabled"
	].includes(dm)) messages.push(`Telegram dmPolicy "${dm}" is not valid.`);
	if (dm === "open") {
		messages.push("Telegram DM policy is \"open\" (any user can message the bot).");
		hints.push("Consider \"pairing\" or \"allowlist\" for stricter access.");
	}
	return {
		ok: messages.length === 0,
		messages,
		hints
	};
}
function checkWeixin(cfg) {
	const wx = cfg.channels?.weixin;
	if (!wx || wx.enabled !== true) return {
		ok: true,
		messages: [],
		hints: []
	};
	const messages = [];
	const hints = [];
	if ((wx.accounts ? Object.keys(wx.accounts).filter((k) => k.trim()) : []).length === 0) {
		messages.push("Weixin is enabled but no accounts are defined in config.");
		hints.push("Run: xopc channels login --channel weixin (or add channels.weixin.accounts).");
	}
	return {
		ok: messages.length === 0,
		messages,
		hints
	};
}
async function checkChannelConfig(ctx) {
	if (!existsSync(ctx.configPath)) return {
		id: "channel-config",
		label: "Channels",
		status: "skip",
		message: "No config file; skipped.",
		hints: []
	};
	let cfg;
	try {
		cfg = loadConfig(ctx.configPath);
	} catch {
		return {
			id: "channel-config",
			label: "Channels",
			status: "skip",
			message: "Config could not be loaded; skipped.",
			hints: []
		};
	}
	const tg = checkTelegram(cfg);
	const wx = checkWeixin(cfg);
	const allMsg = [...tg.messages, ...wx.messages];
	const allHints = [...tg.hints, ...wx.hints];
	const tgEnabled = (cfg.channels?.telegram)?.enabled === true || Boolean((cfg.channels?.telegram)?.accounts?.default?.botToken?.trim());
	const wxOn = (cfg.channels?.weixin)?.enabled === true;
	if (!tgEnabled && !wxOn) return {
		id: "channel-config",
		label: "Channels",
		status: "skip",
		message: "No channels enabled; skipped.",
		hints: []
	};
	if (allMsg.length === 0) return {
		id: "channel-config",
		label: "Channels",
		status: "pass",
		message: "Enabled channel configuration looks valid.",
		hints: []
	};
	return {
		id: "channel-config",
		label: "Channels",
		status: allMsg.some((m) => m.includes("no bot token") || m.includes("no accounts")) ? "fail" : "warn",
		message: allMsg.join(" "),
		hints: allHints
	};
}
//#endregion
export { checkChannelConfig };

//# sourceMappingURL=channel-config.js.map