import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-Bje8XVt9.js";
import { i as visibleWidth } from "./ansi-Dqm1lzVL.js";
import { r as stylePromptTitle } from "./prompt-style-mpRhXk_G.js";
import { note } from "@clack/prompts";
//#region src/terminal/note.ts
const MIN_NOTE_COLUMNS = 80;
const URL_PREFIX_RE = /^(https?:\/\/|file:\/\/)/i;
const WINDOWS_DRIVE_RE = /^[a-zA-Z]:[\\/]/;
const FILE_LIKE_RE = /^[a-zA-Z0-9._-]+$/;
function isSuppressedByEnv(value) {
	if (!value) return false;
	const normalized = normalizeLowercaseStringOrEmpty(value);
	if (!normalized) return false;
	return normalized !== "0" && normalized !== "false" && normalized !== "off";
}
function splitLongWord(word, maxLen) {
	if (maxLen <= 0) return [word];
	const chars = Array.from(word);
	const parts = [];
	for (let i = 0; i < chars.length; i += maxLen) parts.push(chars.slice(i, i + maxLen).join(""));
	return parts.length > 0 ? parts : [word];
}
function isCopySensitiveToken(word) {
	if (!word) return false;
	if (URL_PREFIX_RE.test(word)) return true;
	if (word.startsWith("/") || word.startsWith("~/") || word.startsWith("./") || word.startsWith("../")) return true;
	if (WINDOWS_DRIVE_RE.test(word) || word.startsWith("\\\\")) return true;
	if (word.includes("/") || word.includes("\\")) return true;
	return word.includes("_") && FILE_LIKE_RE.test(word);
}
function pushWrappedWordSegments(params) {
	const parts = splitLongWord(params.word, params.available);
	const first = parts.shift() ?? "";
	params.lines.push(params.firstPrefix + first);
	for (const part of parts) params.lines.push(params.continuationPrefix + part);
}
function wrapLine(line, maxWidth) {
	if (line.trim().length === 0) return [line];
	const match = line.match(/^(\s*)([-*\u2022]\s+)?(.*)$/);
	const indent = match?.[1] ?? "";
	const bullet = match?.[2] ?? "";
	const content = match?.[3] ?? "";
	const firstPrefix = `${indent}${bullet}`;
	const nextPrefix = `${indent}${bullet ? " ".repeat(bullet.length) : ""}`;
	const firstWidth = Math.max(10, maxWidth - visibleWidth(firstPrefix));
	const nextWidth = Math.max(10, maxWidth - visibleWidth(nextPrefix));
	const words = content.split(/\s+/).filter(Boolean);
	const lines = [];
	let current = "";
	let prefix = firstPrefix;
	let available = firstWidth;
	for (const word of words) {
		if (!current) {
			if (visibleWidth(word) > available) {
				if (isCopySensitiveToken(word)) {
					current = word;
					continue;
				}
				pushWrappedWordSegments({
					word,
					available,
					firstPrefix: prefix,
					continuationPrefix: nextPrefix,
					lines
				});
				prefix = nextPrefix;
				available = nextWidth;
				continue;
			}
			current = word;
			continue;
		}
		const candidate = `${current} ${word}`;
		if (visibleWidth(candidate) <= available) {
			current = candidate;
			continue;
		}
		lines.push(prefix + current);
		prefix = nextPrefix;
		available = nextWidth;
		if (visibleWidth(word) > available) {
			if (isCopySensitiveToken(word)) {
				current = word;
				continue;
			}
			pushWrappedWordSegments({
				word,
				available,
				firstPrefix: prefix,
				continuationPrefix: prefix,
				lines
			});
			current = "";
			continue;
		}
		current = word;
	}
	if (current || words.length === 0) lines.push(prefix + current);
	return lines;
}
function wrapNoteMessage(message, options = {}) {
	const columns = options.columns ?? resolveNoteColumns(process.stdout.columns);
	const maxWidth = options.maxWidth ?? Math.max(40, Math.min(88, columns - 10));
	return message.split("\n").flatMap((line) => wrapLine(line, maxWidth)).join("\n");
}
function resolveNoteColumns(columns) {
	if (!Number.isFinite(columns) || !columns || columns < MIN_NOTE_COLUMNS) return MIN_NOTE_COLUMNS;
	return columns;
}
function createNoteOutput(columns) {
	if (process.stdout.columns === columns) return process.stdout;
	const output = Object.create(process.stdout);
	Object.defineProperty(output, "columns", {
		value: columns,
		configurable: true
	});
	output.write = process.stdout.write.bind(process.stdout);
	return output;
}
function note$1(message, title) {
	if (isSuppressedByEnv(process.env.OPENCLAW_SUPPRESS_NOTES)) return;
	const columns = resolveNoteColumns(process.stdout.columns);
	note(wrapNoteMessage(message, { columns }), stylePromptTitle(title), {
		output: createNoteOutput(columns),
		format: (line) => line
	});
}
//#endregion
export { resolveNoteColumns as n, wrapNoteMessage as r, note$1 as t };
