"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const codicons = require("./codicons.js");
const filters = require("./filters.js");
const strings = require("./strings.js");
const iconStartMarker = "$(";
const iconsRegex = new RegExp(`\\$\\(${codicons.CSSIcon.iconNameExpression}(?:${codicons.CSSIcon.iconModifierExpression})?\\)`, "g");
const iconNameCharacterRegexp = new RegExp(codicons.CSSIcon.iconNameCharacter);
const escapeIconsRegex = new RegExp(`(\\\\)?${iconsRegex.source}`, "g");
function escapeIcons(text) {
  return text.replace(escapeIconsRegex, (match, escaped) => escaped ? match : `\\${match}`);
}
const markdownEscapedIconsRegex = new RegExp(`\\\\${iconsRegex.source}`, "g");
function markdownEscapeEscapedIcons(text) {
  return text.replace(markdownEscapedIconsRegex, (match) => `\\${match}`);
}
const stripIconsRegex = new RegExp(`(\\s)?(\\\\)?${iconsRegex.source}(\\s)?`, "g");
function stripIcons(text) {
  if (text.indexOf(iconStartMarker) === -1) {
    return text;
  }
  return text.replace(stripIconsRegex, (match, preWhitespace, escaped, postWhitespace) => escaped ? match : preWhitespace || postWhitespace || "");
}
function parseLabelWithIcons(text) {
  const firstIconIndex = text.indexOf(iconStartMarker);
  if (firstIconIndex === -1) {
    return { text };
  }
  return doParseLabelWithIcons(text, firstIconIndex);
}
function doParseLabelWithIcons(text, firstIconIndex) {
  const iconOffsets = [];
  let textWithoutIcons = "";
  function appendChars(chars) {
    if (chars) {
      textWithoutIcons += chars;
      for (const _ of chars) {
        iconOffsets.push(iconsOffset);
      }
    }
  }
  let currentIconStart = -1;
  let currentIconValue = "";
  let iconsOffset = 0;
  let char;
  let nextChar;
  let offset = firstIconIndex;
  const length = text.length;
  appendChars(text.substr(0, firstIconIndex));
  while (offset < length) {
    char = text[offset];
    nextChar = text[offset + 1];
    if (char === iconStartMarker[0] && nextChar === iconStartMarker[1]) {
      currentIconStart = offset;
      appendChars(currentIconValue);
      currentIconValue = iconStartMarker;
      offset++;
    } else if (char === ")" && currentIconStart !== -1) {
      const currentIconLength = offset - currentIconStart + 1;
      iconsOffset += currentIconLength;
      currentIconStart = -1;
      currentIconValue = "";
    } else if (currentIconStart !== -1) {
      if (iconNameCharacterRegexp.test(char)) {
        currentIconValue += char;
      } else {
        appendChars(currentIconValue);
        currentIconStart = -1;
        currentIconValue = "";
      }
    } else {
      appendChars(char);
    }
    offset++;
  }
  appendChars(currentIconValue);
  return { text: textWithoutIcons, iconOffsets };
}
function matchesFuzzyIconAware(query, target, enableSeparateSubstringMatching = false) {
  const { text, iconOffsets } = target;
  if (!iconOffsets || iconOffsets.length === 0) {
    return filters.matchesFuzzy(query, text, enableSeparateSubstringMatching);
  }
  const wordToMatchAgainstWithoutIconsTrimmed = strings.ltrim(text, " ");
  const leadingWhitespaceOffset = text.length - wordToMatchAgainstWithoutIconsTrimmed.length;
  const matches = filters.matchesFuzzy(query, wordToMatchAgainstWithoutIconsTrimmed, enableSeparateSubstringMatching);
  if (matches) {
    for (const match of matches) {
      const iconOffset = iconOffsets[match.start + leadingWhitespaceOffset] + leadingWhitespaceOffset;
      match.start += iconOffset;
      match.end += iconOffset;
    }
  }
  return matches;
}
exports.escapeIcons = escapeIcons;
exports.iconStartMarker = iconStartMarker;
exports.markdownEscapeEscapedIcons = markdownEscapeEscapedIcons;
exports.matchesFuzzyIconAware = matchesFuzzyIconAware;
exports.parseLabelWithIcons = parseLabelWithIcons;
exports.stripIcons = stripIcons;
