"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var width_exports = {};
__export(width_exports, {
  getEastAsianWidth: () => getEastAsianWidth,
  stringWidth: () => stringWidth
});
module.exports = __toCommonJS(width_exports);
var import_strip = require("../ansi/strip");
var import_regexp = require("../primordials/regexp");
var import_string = require("../primordials/string");
let _eastAsianWidth;
// @__NO_SIDE_EFFECTS__
function getEastAsianWidth() {
  if (_eastAsianWidth === void 0) {
    _eastAsianWidth = require("../external/get-east-asian-width").eastAsianWidth;
  }
  return _eastAsianWidth;
}
const segmenter = new Intl.Segmenter();
let zeroWidthClusterRegex;
let leadingNonPrintingRegex;
let emojiRegex;
try {
  zeroWidthClusterRegex = new RegExp("^(?:\\p{Default_Ignorable_Code_Point}|\\p{Control}|\\p{Mark}|\\p{Surrogate})+$", "v");
  leadingNonPrintingRegex = new RegExp("^[\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Mark}\\p{Surrogate}]+", "v");
  emojiRegex = new RegExp("^\\p{RGI_Emoji}$", "v");
} catch {
  zeroWidthClusterRegex = new RegExp("^(?:\\p{Default_Ignorable_Code_Point}|\\p{Control}|\\p{Mark})+$", "u");
  leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}]+/u;
  emojiRegex = new RegExp("^\\p{Extended_Pictographic}$", "u");
}
// @__NO_SIDE_EFFECTS__
function stringWidth(text) {
  if (typeof text !== "string" || !text.length) {
    return 0;
  }
  const plainText = (0, import_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 ((0, import_regexp.RegExpPrototypeTest)(zeroWidthClusterRegex, segment)) {
      continue;
    }
    if ((0, import_regexp.RegExpPrototypeTest)(emojiRegex, segment)) {
      width += 2;
      continue;
    }
    const baseSegment = segment.replace(leadingNonPrintingRegex, "");
    const codePoint = (0, import_string.StringPrototypeCodePointAt)(baseSegment, 0);
    if (codePoint === void 0) {
      continue;
    }
    width += eastAsianWidth(codePoint, eastAsianWidthOptions);
    if (segment.length > 1) {
      for (const char of segment.slice(1)) {
        const charCode = (0, import_string.StringPrototypeCharCodeAt)(char, 0);
        if (charCode >= 65280 && charCode <= 65519) {
          const trailingCodePoint = (0, import_string.StringPrototypeCodePointAt)(char, 0);
          if (trailingCodePoint !== void 0) {
            width += eastAsianWidth(trailingCodePoint, eastAsianWidthOptions);
          }
        }
      }
    }
  }
  return width;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getEastAsianWidth,
  stringWidth
});
