import { U as theme } from "./agent-paths-BySKyq9I.js";
import { ld as stripAnsi } from "./agent-scope-CLn_Ws3B.js";
import { t as createCliProgress } from "./progress-CDqdg8f8.js";
import { a as stylePromptTitle, i as stylePromptMessage, n as styleProgressTitle, r as stylePromptHint, t as formatWizardIntro } from "./prompt-style-Db5iJoaI.js";
import { t as WizardCancelledError } from "./prompts-CbbpEiEO.js";
import { t as note$1 } from "./note-BVXI9hXT.js";
import { autocompleteMultiselect, cancel, confirm, intro, isCancel, multiselect, outro, password, select, spinner, text } from "@clack/prompts";

//#region src/wizard/clack-prompter.ts
function guardCancel(value) {
	if (isCancel(value)) {
		cancel(stylePromptTitle("Setup cancelled.") ?? "Setup cancelled.");
		throw new WizardCancelledError();
	}
	return value;
}
function normalizeSearchTokens(search) {
	return search.toLowerCase().split(/\s+/).map((token) => token.trim()).filter((token) => token.length > 0);
}
function buildOptionSearchText(option) {
	return `${stripAnsi(option.label ?? "")} ${stripAnsi(option.hint ?? "")} ${String(option.value ?? "")}`.toLowerCase();
}
function formatElapsed(ms) {
	const totalSeconds = Math.max(0, Math.floor(ms / 1e3));
	const minutes = Math.floor(totalSeconds / 60);
	const seconds = totalSeconds % 60;
	return minutes > 0 ? `${minutes}m ${seconds}s` : `${seconds}s`;
}
function tokenizedOptionFilter(search, option) {
	const tokens = normalizeSearchTokens(search);
	if (tokens.length === 0) return true;
	const haystack = buildOptionSearchText(option);
	return tokens.every((token) => haystack.includes(token));
}
function createClackPrompter() {
	return {
		intro: async (title) => {
			intro(formatWizardIntro(title));
		},
		outro: async (message) => {
			outro(stylePromptTitle(message) ?? message);
		},
		note: async (message, title) => {
			note$1(message, title);
		},
		select: async (params) => guardCancel(await select({
			message: stylePromptMessage(params.message),
			options: params.options.map((opt) => {
				const base = {
					value: opt.value,
					label: opt.label,
					disabled: opt.disabled
				};
				return opt.hint === void 0 ? base : {
					...base,
					hint: stylePromptHint(opt.hint)
				};
			}),
			initialValue: params.initialValue
		})),
		multiselect: async (params) => {
			const options = params.options.map((opt) => {
				const base = {
					value: opt.value,
					label: opt.label,
					disabled: opt.disabled
				};
				return opt.hint === void 0 ? base : {
					...base,
					hint: stylePromptHint(opt.hint)
				};
			});
			if (params.searchable) return guardCancel(await autocompleteMultiselect({
				message: stylePromptMessage(params.message),
				options,
				initialValues: params.initialValues,
				filter: tokenizedOptionFilter
			}));
			return guardCancel(await multiselect({
				message: stylePromptMessage(params.message),
				options,
				initialValues: params.initialValues
			}));
		},
		text: async (params) => {
			const validate = params.validate;
			return guardCancel(await text({
				message: stylePromptMessage(params.message),
				initialValue: params.initialValue,
				placeholder: params.placeholder,
				validate: validate ? (value) => validate(value ?? "") : void 0
			}));
		},
		secret: async (params) => {
			const validate = params.validate;
			return guardCancel(await password({
				message: stylePromptMessage(params.message),
				validate: validate ? (value) => validate(value ?? "") : void 0,
				mask: "•"
			}));
		},
		confirm: async (params) => guardCancel(await confirm({
			message: stylePromptMessage(params.message),
			initialValue: params.initialValue
		})),
		progress: (label) => {
			const spin = spinner();
			const startedAt = Date.now();
			let currentMessage = label;
			const render = (message) => `${styleProgressTitle(message)} ${theme.muted(`(${formatElapsed(Date.now() - startedAt)})`)}`;
			spin.start(render(currentMessage));
			const elapsedTimer = setInterval(() => {
				spin.message(render(currentMessage));
			}, 1e3);
			elapsedTimer.unref?.();
			const osc = createCliProgress({
				label,
				indeterminate: true,
				enabled: true,
				fallback: "none"
			});
			return {
				update: (message) => {
					currentMessage = message;
					spin.message(render(currentMessage));
					osc.setLabel(message);
				},
				stop: (message) => {
					clearInterval(elapsedTimer);
					osc.done();
					if (message && message.trim().length > 0) spin.stop(theme.success(message));
					else spin.clear();
				}
			};
		}
	};
}

//#endregion
export { createClackPrompter as t };