import { a as stripAnsi, i as init_strip_ansi, n as isFullWidth, r as isWide } from "./string-width-D78SVDLD.mjs";
import { t as cli_cursor_default } from "./cli-cursor-Dab4mDU2.mjs";
import { t as ansi_styles_default } from "./ansi-styles-B57ln_zO.mjs";
import { t as wrapAnsi } from "./wrap-ansi-DoQ74EEn.mjs";
import process from "node:process";

//#region ../../node_modules/.pnpm/environment@1.1.0/node_modules/environment/index.js
const isBrowser = globalThis.window?.document !== void 0;
const isNode = globalThis.process?.versions?.node !== void 0;
const isBun = globalThis.process?.versions?.bun !== void 0;
const isDeno = globalThis.Deno?.version?.deno !== void 0;
const isElectron = globalThis.process?.versions?.electron !== void 0;
const isJsDom = globalThis.navigator?.userAgent?.includes("jsdom") === true;
const isWebWorker = typeof WorkerGlobalScope !== "undefined" && globalThis instanceof WorkerGlobalScope;
const isDedicatedWorker = typeof DedicatedWorkerGlobalScope !== "undefined" && globalThis instanceof DedicatedWorkerGlobalScope;
const isSharedWorker = typeof SharedWorkerGlobalScope !== "undefined" && globalThis instanceof SharedWorkerGlobalScope;
const isServiceWorker = typeof ServiceWorkerGlobalScope !== "undefined" && globalThis instanceof ServiceWorkerGlobalScope;
const platform = globalThis.navigator?.userAgentData?.platform;
const isMacOs = platform === "macOS" || globalThis.navigator?.platform === "MacIntel" || globalThis.navigator?.userAgent?.includes(" Mac ") === true || globalThis.process?.platform === "darwin";
const isWindows$1 = platform === "Windows" || globalThis.navigator?.platform === "Win32" || globalThis.process?.platform === "win32";
const isLinux = platform === "Linux" || globalThis.navigator?.platform?.startsWith("Linux") === true || globalThis.navigator?.userAgent?.includes(" Linux ") === true || globalThis.process?.platform === "linux";
const isIos = platform === "iOS" || globalThis.navigator?.platform === "MacIntel" && globalThis.navigator?.maxTouchPoints > 1 || /iPad|iPhone|iPod/.test(globalThis.navigator?.platform);
const isAndroid = platform === "Android" || globalThis.navigator?.platform === "Android" || globalThis.navigator?.userAgent?.includes(" Android ") === true || globalThis.process?.platform === "android";

//#endregion
//#region ../../node_modules/.pnpm/ansi-escapes@7.1.0/node_modules/ansi-escapes/base.js
const ESC = "\x1B[";
const isTerminalApp = !isBrowser && process.env.TERM_PROGRAM === "Apple_Terminal";
const isWindows = !isBrowser && process.platform === "win32";
const cwdFunction = isBrowser ? () => {
	throw new Error("`process.cwd()` only works in Node.js, not the browser.");
} : process.cwd;
const cursorUp = (count = 1) => ESC + count + "A";
const cursorLeft = ESC + "G";
const cursorSavePosition = isTerminalApp ? "\x1B7" : ESC + "s";
const cursorRestorePosition = isTerminalApp ? "\x1B8" : ESC + "u";
const cursorGetPosition = ESC + "6n";
const cursorNextLine = ESC + "E";
const cursorPrevLine = ESC + "F";
const cursorHide = ESC + "?25l";
const cursorShow = ESC + "?25h";
const eraseLines = (count) => {
	let clear = "";
	for (let i = 0; i < count; i++) clear += eraseLine + (i < count - 1 ? cursorUp() : "");
	if (count) clear += cursorLeft;
	return clear;
};
const eraseEndLine = ESC + "K";
const eraseStartLine = ESC + "1K";
const eraseLine = ESC + "2K";
const eraseDown = ESC + "J";
const eraseUp = ESC + "1J";
const eraseScreen = ESC + "2J";
const scrollUp = ESC + "S";
const scrollDown = ESC + "T";
const clearViewport = `${eraseScreen}${ESC}H`;
const clearTerminal = isWindows ? `${eraseScreen}${ESC}0f` : `${eraseScreen}${ESC}3J${ESC}H`;
const enterAlternativeScreen = ESC + "?1049h";
const exitAlternativeScreen = ESC + "?1049l";

//#endregion
//#region ../../node_modules/.pnpm/is-fullwidth-code-point@5.1.0/node_modules/is-fullwidth-code-point/index.js
function isFullwidthCodePoint(codePoint) {
	if (!Number.isInteger(codePoint)) return false;
	return isFullWidth(codePoint) || isWide(codePoint);
}

//#endregion
//#region ../../node_modules/.pnpm/slice-ansi@7.1.0/node_modules/slice-ansi/index.js
const ESCAPES = new Set([27, 155]);
const CODE_POINT_0 = "0".codePointAt(0);
const CODE_POINT_9 = "9".codePointAt(0);
const endCodesSet = /* @__PURE__ */ new Set();
const endCodesMap = /* @__PURE__ */ new Map();
for (const [start, end] of ansi_styles_default.codes) {
	endCodesSet.add(ansi_styles_default.color.ansi(end));
	endCodesMap.set(ansi_styles_default.color.ansi(start), ansi_styles_default.color.ansi(end));
}
function getEndCode(code) {
	if (endCodesSet.has(code)) return code;
	if (endCodesMap.has(code)) return endCodesMap.get(code);
	code = code.slice(2);
	if (code.includes(";")) code = code[0] + "0";
	const returnValue = ansi_styles_default.codes.get(Number.parseInt(code, 10));
	if (returnValue) return ansi_styles_default.color.ansi(returnValue);
	return ansi_styles_default.reset.open;
}
function findNumberIndex(string) {
	for (let index = 0; index < string.length; index++) {
		const codePoint = string.codePointAt(index);
		if (codePoint >= CODE_POINT_0 && codePoint <= CODE_POINT_9) return index;
	}
	return -1;
}
function parseAnsiCode(string, offset) {
	string = string.slice(offset, offset + 19);
	const startIndex = findNumberIndex(string);
	if (startIndex !== -1) {
		let endIndex = string.indexOf("m", startIndex);
		if (endIndex === -1) endIndex = string.length;
		return string.slice(0, endIndex + 1);
	}
}
function tokenize(string, endCharacter = Number.POSITIVE_INFINITY) {
	const returnValue = [];
	let index = 0;
	let visibleCount = 0;
	while (index < string.length) {
		const codePoint = string.codePointAt(index);
		if (ESCAPES.has(codePoint)) {
			const code = parseAnsiCode(string, index);
			if (code) {
				returnValue.push({
					type: "ansi",
					code,
					endCode: getEndCode(code)
				});
				index += code.length;
				continue;
			}
		}
		const isFullWidth$1 = isFullwidthCodePoint(codePoint);
		const character = String.fromCodePoint(codePoint);
		returnValue.push({
			type: "character",
			value: character,
			isFullWidth: isFullWidth$1
		});
		index += character.length;
		visibleCount += isFullWidth$1 ? 2 : character.length;
		if (visibleCount >= endCharacter) break;
	}
	return returnValue;
}
function reduceAnsiCodes(codes) {
	let returnValue = [];
	for (const code of codes) if (code.code === ansi_styles_default.reset.open) returnValue = [];
	else if (endCodesSet.has(code.code)) returnValue = returnValue.filter((returnValueCode) => returnValueCode.endCode !== code.code);
	else {
		returnValue = returnValue.filter((returnValueCode) => returnValueCode.endCode !== code.endCode);
		returnValue.push(code);
	}
	return returnValue;
}
function undoAnsiCodes(codes) {
	return reduceAnsiCodes(codes).map(({ endCode }) => endCode).reverse().join("");
}
function sliceAnsi(string, start, end) {
	const tokens = tokenize(string, end);
	let activeCodes = [];
	let position = 0;
	let returnValue = "";
	let include = false;
	for (const token of tokens) {
		if (end !== void 0 && position >= end) break;
		if (token.type === "ansi") {
			activeCodes.push(token);
			if (include) returnValue += token.code;
		} else {
			if (!include && position >= start) {
				include = true;
				activeCodes = reduceAnsiCodes(activeCodes);
				returnValue = activeCodes.map(({ code }) => code).join("");
			}
			if (include) returnValue += token.value;
			position += token.isFullWidth ? 2 : token.value.length;
		}
	}
	returnValue += undoAnsiCodes(activeCodes);
	return returnValue;
}

//#endregion
//#region ../../node_modules/.pnpm/log-update@6.1.0/node_modules/log-update/index.js
init_strip_ansi();
const defaultTerminalHeight = 24;
const getWidth = ({ columns = 80 }) => columns;
const fitToTerminalHeight = (stream, text) => {
	const terminalHeight = stream.rows ?? defaultTerminalHeight;
	const lines = text.split("\n");
	const toRemove = Math.max(0, lines.length - terminalHeight);
	return toRemove ? sliceAnsi(text, stripAnsi(lines.slice(0, toRemove).join("\n")).length + 1) : text;
};
function createLogUpdate(stream, { showCursor = false } = {}) {
	let previousLineCount = 0;
	let previousWidth = getWidth(stream);
	let previousOutput = "";
	const reset = () => {
		previousOutput = "";
		previousWidth = getWidth(stream);
		previousLineCount = 0;
	};
	const render = (...arguments_) => {
		if (!showCursor) cli_cursor_default.hide();
		let output = fitToTerminalHeight(stream, arguments_.join(" ") + "\n");
		const width = getWidth(stream);
		if (output === previousOutput && previousWidth === width) return;
		previousOutput = output;
		previousWidth = width;
		output = wrapAnsi(output, width, {
			trim: false,
			hard: true,
			wordWrap: false
		});
		stream.write(eraseLines(previousLineCount) + output);
		previousLineCount = output.split("\n").length;
	};
	render.clear = () => {
		stream.write(eraseLines(previousLineCount));
		reset();
	};
	render.done = () => {
		reset();
		if (!showCursor) cli_cursor_default.show();
	};
	return render;
}
const logUpdate = createLogUpdate(process.stdout);
const logUpdateStderr = createLogUpdate(process.stderr);

//#endregion
export { createLogUpdate };
//# sourceMappingURL=log-update-txb7Or7B.mjs.map