import { confirm, input, select } from "@inquirer/prompts";
//#region src/cli/commands/onboard/channels/wizard-to-configurator.ts
/**
* Bridge declarative {@link ChannelSetupWizard} (from bundled plugins) to {@link ChannelConfigurator}.
*/
function readChannelField(cfg, channel, key) {
	const raw = cfg.channels?.[channel];
	if (!raw || typeof raw !== "object") return void 0;
	const v = raw[key];
	return typeof v === "string" ? v : void 0;
}
function parseAllowlistRaw(raw) {
	if (!raw.trim()) return [];
	return raw.split(/[,\s\n]+/).map((s) => s.trim()).filter(Boolean).map((e) => {
		const num = parseInt(e, 10);
		return !isNaN(num) && String(num) === e ? num : e;
	});
}
function mergeChannel(cfg, channel, partial) {
	const channels = { ...cfg.channels };
	channels[channel] = {
		...channels[channel] ?? {},
		...partial,
		enabled: true
	};
	return {
		...cfg,
		channels
	};
}
function channelSetupWizardToConfigurator(wizard, meta) {
	const id = wizard.channel;
	return {
		id,
		name: meta.name,
		description: meta.description,
		isConfigured(cfg) {
			const creds = wizard.credentials ?? [];
			if (creds.length === 0) return false;
			return creds.every((c) => {
				const v = readChannelField(cfg, id, c.key);
				return typeof v === "string" && v.trim().length > 0;
			});
		},
		async configure(config) {
			console.log(`\n${"=".repeat(50)}`);
			console.log(`📱 ${meta.name} setup`);
			console.log(`${"=".repeat(50)}\n`);
			let next = config;
			const values = {};
			const env = wizard.envShortcut;
			if (env) {
				const fromEnv = process.env[env.envVar]?.trim();
				if (fromEnv) {
					if (await confirm({
						message: `${env.envVar} is set in the environment. Use it for this channel?`,
						default: true
					})) {
						const parts = env.configPath.split(".");
						const key = parts[parts.length - 1] ?? "botToken";
						values[key] = fromEnv;
					}
				}
			}
			for (const c of wizard.credentials) {
				if (values[c.key]) continue;
				const existing = readChannelField(next, id, c.key);
				if (existing) {
					if (await confirm({
						message: `Keep existing ${c.label}?`,
						default: true
					})) {
						values[c.key] = existing;
						continue;
					}
				}
				let v = "";
				while (true) {
					v = await input({ message: `${c.label}:` });
					const err = c.validate?.(v) ?? null;
					if (!err) break;
					console.log(`⚠️  ${err}`);
				}
				values[c.key] = v.trim();
			}
			const partial = { ...values };
			if (wizard.dmPolicy) {
				const policy = await select({
					message: "DM / access policy:",
					choices: wizard.dmPolicy.options.map((o) => ({
						value: o.value,
						name: o.label,
						description: o.description
					})),
					default: wizard.dmPolicy.default
				});
				partial.dmPolicy = policy;
				if (policy === "allowlist" && wizard.allowFrom) partial.allowFrom = parseAllowlistRaw(await input({
					message: wizard.allowFrom.hint,
					default: ""
				}));
			}
			next = mergeChannel(next, id, partial);
			if (wizard.finalize) {
				const result = await wizard.finalize.validate(next);
				if (!result.ok) {
					console.log(`⚠️  ${result.error ?? "Validation failed"}`);
					return config;
				}
				console.log(`\n✅ ${wizard.finalize.message}\n`);
			} else console.log("\n✅ Channel configuration saved.\n");
			return next;
		}
	};
}
//#endregion
export { channelSetupWizardToConfigurator };

//# sourceMappingURL=wizard-to-configurator.js.map