import "../../chunks/chunk.QU3DSPNU.js";
import { SEARCH_CONFIG, SEARCH_TIERS } from "./constants.js";
import { RegexError } from "./errors.js";
function compileRegexPattern(pattern) {
  if (pattern.length > SEARCH_CONFIG.MAX_REGEX_LENGTH) {
    throw new RegexError(
      `Regex pattern too long (max ${SEARCH_CONFIG.MAX_REGEX_LENGTH} characters)`,
      pattern,
      { maxLength: SEARCH_CONFIG.MAX_REGEX_LENGTH }
    );
  }
  if (/([+*?]|\{\d+,?\d*\})\)*([+*?]|\{\d+,?\d*\})/.test(pattern)) {
    throw new RegexError(
      "Regex pattern contains nested quantifiers which could cause catastrophic backtracking",
      pattern
    );
  }
  try {
    return new RegExp(pattern, "i");
  } catch (error) {
    throw new RegexError(
      `Invalid regex pattern: ${error instanceof Error ? error.message : "unknown error"}`,
      pattern
    );
  }
}
function checkRegexMatch(item, regex, originalQuery) {
  const tagName = item.tagName.toLowerCase();
  const name = item.name.toLowerCase();
  const className = item.className.toLowerCase();
  const description = item.description?.toLowerCase() || "";
  const matches = regex.test(tagName) || regex.test(name) || regex.test(className) || regex.test(description);
  if (!matches) {
    return { matches: false, tier: 0, matchedTerms: 0 };
  }
  const lowerQuery = originalQuery.toLowerCase();
  if (regex.test(tagName)) {
    if (tagName === lowerQuery) {
      return { matches: true, tier: SEARCH_TIERS.EXACT_MATCH, matchedTerms: 1 };
    }
    if (tagName.startsWith(lowerQuery)) {
      return { matches: true, tier: SEARCH_TIERS.STARTS_WITH, matchedTerms: 1 };
    }
    return { matches: true, tier: SEARCH_TIERS.TAG_CONTAINS, matchedTerms: 1 };
  }
  return { matches: true, tier: SEARCH_TIERS.NAME_CONTAINS, matchedTerms: 1 };
}
function checkMultiTermMatch(item, searchTerms, matchAll) {
  const tagName = item.tagName.toLowerCase();
  const name = item.name.toLowerCase();
  const className = item.className.toLowerCase();
  const description = item.description?.toLowerCase() || "";
  let matchedTerms = 0;
  let bestTier = SEARCH_TIERS.DESCRIPTION_CONTAINS + 1;
  for (const term of searchTerms) {
    let termMatched = false;
    let termTier = SEARCH_TIERS.DESCRIPTION_CONTAINS + 1;
    if (tagName === `gds-${term}` || tagName === term) {
      termMatched = true;
      termTier = Math.min(termTier, SEARCH_TIERS.EXACT_MATCH);
    } else if (tagName.startsWith(term) || tagName.startsWith(`gds-${term}`)) {
      termMatched = true;
      termTier = Math.min(termTier, SEARCH_TIERS.STARTS_WITH);
    } else if (tagName.includes(term)) {
      termMatched = true;
      termTier = Math.min(termTier, SEARCH_TIERS.TAG_CONTAINS);
    } else if (name.includes(term) || className.includes(term)) {
      termMatched = true;
      termTier = Math.min(termTier, SEARCH_TIERS.NAME_CONTAINS);
    } else if (description.includes(term)) {
      termMatched = true;
      termTier = Math.min(termTier, SEARCH_TIERS.DESCRIPTION_CONTAINS);
    }
    if (termMatched) {
      matchedTerms++;
      bestTier = Math.min(bestTier, termTier);
    }
  }
  const matches = matchAll ? matchedTerms === searchTerms.length : matchedTerms > 0;
  return { matches, tier: matches ? bestTier : 999, matchedTerms };
}
function parseSearchQuery(query, splitTerms, useRegex) {
  if (useRegex) {
    const pattern = query.startsWith("/") && query.endsWith("/") ? query.slice(1, -1) : query;
    const regexPattern = compileRegexPattern(pattern);
    return { searchTerms: [query], regexPattern };
  }
  if (splitTerms) {
    const terms = query.toLowerCase().split(/[\s,]+/).filter((term) => term.length > 0);
    return { searchTerms: terms, regexPattern: null };
  }
  return { searchTerms: [query.toLowerCase()], regexPattern: null };
}
function performSearch(components, icons, query, searchTerms, regexPattern, matchAll, splitTerms, maxResults, buildResourceUris) {
  const results = [];
  const checkMatches = (item) => {
    if (regexPattern) {
      return checkRegexMatch(item, regexPattern, query);
    }
    return checkMultiTermMatch(item, searchTerms, matchAll);
  };
  for (const component of components) {
    const { matches, tier, matchedTerms } = checkMatches(component);
    if (matches) {
      results.push({
        name: component.name,
        tagName: component.tagName,
        className: component.className,
        description: component.description,
        category: "component",
        availableDocs: component.files,
        resourceUris: buildResourceUris(component, "component"),
        tier,
        matchedTerms
      });
    }
  }
  for (const icon of icons) {
    const { matches, tier, matchedTerms } = checkMatches(icon);
    if (matches) {
      results.push({
        name: icon.name,
        tagName: icon.tagName,
        className: icon.className,
        description: icon.description,
        category: "icon",
        availableDocs: icon.files,
        resourceUris: buildResourceUris(icon, "icon"),
        tier,
        matchedTerms
      });
    }
  }
  results.sort((a, b) => {
    if (a.tier !== b.tier) return a.tier - b.tier;
    if (splitTerms && a.matchedTerms !== b.matchedTerms) {
      return b.matchedTerms - a.matchedTerms;
    }
    return a.tagName.localeCompare(b.tagName);
  });
  return results.slice(0, maxResults).map(({ tier, matchedTerms, ...result }) => result);
}
export {
  parseSearchQuery,
  performSearch
};
