"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_string = require('../primordials/string.js');
const require_primordials_regexp = require('../primordials/regexp.js');
const require_ansi_strip = require('../ansi/strip.js');

//#region src/strings/width.ts
/**
* @file `stringWidth` — calculate visual terminal width. Based on string-width
*   by Sindre Sorhus
*   (https://socket.dev/npm/package/string-width/overview/7.2.0, MIT). Why this
*   lives in its own leaf:
*
*   - It carries a heavy module-level setup: `Intl.Segmenter` instance,
*     feature-detected regex patterns (with `'v'` flag fallback to `'u'`), and
*     a lazy `eastAsianWidth` accessor.
*   - The function body is ~150 lines of carefully-commented Unicode handling —
*     keeping it isolated makes it easy to review changes without touching the
*     rest of the strings surface. See the comments inside for the algorithm
*     details and Unicode Standard Annex #11 references.
*/
let _eastAsianWidth;
/*@__NO_SIDE_EFFECTS__*/
function getEastAsianWidth() {
	if (_eastAsianWidth === void 0) _eastAsianWidth = (/* @__PURE__ */ require("../external/get-east-asian-width")).eastAsianWidth;
	return _eastAsianWidth;
}
const segmenter = new Intl.Segmenter();
let zeroWidthClusterRegex;
let leadingNonPrintingRegex;
let emojiRegex;
try {
	zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Mark}|\p{Surrogate})+$/v;
	leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/v;
	emojiRegex = /^\p{RGI_Emoji}$/v;
} catch {
	zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Mark})+$/u;
	leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}]+/u;
	emojiRegex = /^\p{Extended_Pictographic}$/u;
}
/* c8 ignore stop */
/**
* Get the visual width of a string in terminal columns.
*
* Calculates how many columns a string will occupy when displayed in a
* terminal, accounting for: - ANSI escape codes (stripped before calculation) -
* Wide characters (CJK ideographs, fullwidth forms) that take 2 columns - Emoji
* (including complex sequences) that take 2 columns - Combining marks and
* zero-width characters (take 0 columns) - East Asian Width properties
* (Fullwidth, Wide, Halfwidth, Narrow, etc.)
*
* @example
*   ;```ts
*   stringWidth('hello') // 5
*   stringWidth('⚡') // 2 (lightning bolt is wide)
*   stringWidth('✦') // 1 (star is narrow)
*   stringWidth('漢字') // 4 (CJK × 2 cols each)
*   stringWidth('\x1b[31mred\x1b[0m') // 3 (ANSI stripped)
*   stringWidth('👍🏽') // 2 (emoji + skin tone = 1 cluster)
*   stringWidth('é') // 1 (combining accent = 0 width)
*   ```
*
* @param text - The string to measure.
*
* @returns The visual width in terminal columns
*/
/*@__NO_SIDE_EFFECTS__*/
function stringWidth(text) {
	if (typeof text !== "string" || !text.length) return 0;
	/* c8 ignore next */
	const plainText = /* @__PURE__ */ require_ansi_strip.stripAnsi(text);
	if (!plainText.length) return 0;
	let width = 0;
	const eastAsianWidthOptions = { ambiguousAsWide: false };
	const eastAsianWidth = /* @__PURE__ */ getEastAsianWidth();
	for (const { segment } of segmenter.segment(plainText)) {
		if (require_primordials_regexp.RegExpPrototypeTest(zeroWidthClusterRegex, segment)) continue;
		if (require_primordials_regexp.RegExpPrototypeTest(emojiRegex, segment)) {
			width += 2;
			continue;
		}
		const codePoint = require_primordials_string.StringPrototypeCodePointAt(segment.replace(leadingNonPrintingRegex, ""), 0);
		if (codePoint === void 0) continue;
		/* c8 ignore next - External eastAsianWidth call */
		width += eastAsianWidth(codePoint, eastAsianWidthOptions);
		if (segment.length > 1) for (const char of segment.slice(1)) {
			const charCode = require_primordials_string.StringPrototypeCharCodeAt(char, 0);
			if (charCode >= 65280 && charCode <= 65519) {
				const trailingCodePoint = require_primordials_string.StringPrototypeCodePointAt(char, 0);
				if (trailingCodePoint !== void 0)
 /* c8 ignore next - External eastAsianWidth call */
				width += eastAsianWidth(trailingCodePoint, eastAsianWidthOptions);
			}
		}
	}
	return width;
}

//#endregion
exports.getEastAsianWidth = getEastAsianWidth;
exports.stringWidth = stringWidth;