// ../core/src/search/unifiedSearchContract.ts
var UNIFIED_SEARCH_RESULT_CONTRACT_VERSION = "unified_search_result.v1";
// ../core/src/search/paletteSectionLabels.ts
var PALETTE_SECTION_TITLES = {
  suggested: "Suggested",
  top_results: "Top results",
  open_now: "Open now",
  actions: "Actions",
  pages: "Pages",
  chats: "Chats",
  knowledge: "Knowledge",
  activity: "Activity",
  media: "Media",
  code: "Code",
  other: "Other"
};
var PALETTE_SOURCE_SECTION_IDS = {
  suggested_actions: "suggested",
  current_context: "suggested",
  more_context: "suggested",
  best_matches: "top_results",
  search: "top_results",
  open_work: "open_now",
  actions: "actions",
  documents: "pages",
  pages: "pages",
  chat_threads: "chats",
  ai_chats: "chats",
  chat_messages: "chats",
  entities: "knowledge",
  topics: "knowledge",
  memories: "knowledge",
  memory: "knowledge",
  relationships: "knowledge",
  project_graph: "knowledge",
  activity_runs: "activity",
  media: "media",
  code: "code",
  other: "other"
};
function getPaletteSectionId(key) {
  return PALETTE_SOURCE_SECTION_IDS[key];
}
function getPaletteSectionTitle(key) {
  if (key in PALETTE_SECTION_TITLES) {
    return PALETTE_SECTION_TITLES[key];
  }
  return PALETTE_SECTION_TITLES[getPaletteSectionId(key)];
}
// ../core/src/search/codeSearchQuery.ts
var CODE_SEARCH_QUERY_V1_KINDS = [
  "file",
  "symbol",
  "test",
  "doc",
  "package",
  "service",
  "graph_advisory"
];
var KNOWN_FIELDS = new Set(["kind", "lang", "language", "path", "name", "symbol"]);
var CODE_SEARCH_QUERY_KIND_SET = new Set(CODE_SEARCH_QUERY_V1_KINDS);
function parseCodeSearchQueryV1(raw) {
  const tokens = queryTokens(raw);
  const textTokens = [];
  const quotedTextTokens = [];
  const unknownFieldTokens = [];
  const filters = {
    kind: [],
    language: [],
    path: [],
    name: [],
    symbol: []
  };
  const reasonCodes = [];
  for (const token of tokens) {
    const fielded = parseFieldedToken(token.text);
    if (!fielded || !KNOWN_FIELDS.has(fielded.field)) {
      textTokens.push(token.text);
      if (token.quoted)
        quotedTextTokens.push(token.text);
      if (fielded)
        unknownFieldTokens.push(token.text);
      continue;
    }
    const value = normalizeFilterValue(fielded.value);
    if (!value) {
      textTokens.push(token.text);
      if (token.quoted)
        quotedTextTokens.push(token.text);
      reasonCodes.push(`query_filter:${fielded.field}:empty_fallback`);
      continue;
    }
    if (fielded.field === "kind") {
      if (!CODE_SEARCH_QUERY_KIND_SET.has(value)) {
        textTokens.push(token.text);
        if (token.quoted)
          quotedTextTokens.push(token.text);
        reasonCodes.push("query_filter:kind:invalid_fallback");
        continue;
      }
      filters.kind.push(value);
      reasonCodes.push("query_filter:kind");
      continue;
    }
    if (fielded.field === "lang" || fielded.field === "language") {
      const language = normalizeLanguageFilter(value);
      if (!language) {
        textTokens.push(token.text);
        if (token.quoted)
          quotedTextTokens.push(token.text);
        reasonCodes.push("query_filter:language:invalid_fallback");
        continue;
      }
      filters.language.push(language);
      reasonCodes.push("query_filter:language");
      continue;
    }
    if (fielded.field === "path") {
      filters.path.push(value);
      reasonCodes.push("query_filter:path");
      continue;
    }
    if (fielded.field === "name") {
      filters.name.push(value);
      reasonCodes.push("query_filter:name");
      continue;
    }
    if (fielded.field === "symbol") {
      filters.symbol.push(value);
      reasonCodes.push("query_filter:symbol");
      continue;
    }
  }
  return {
    schemaVersion: 1,
    raw,
    text: textTokens.join(" ").trim(),
    quotedTextTokens: unique(quotedTextTokens),
    filters: {
      kind: unique(filters.kind),
      language: unique(filters.language),
      path: unique(filters.path),
      name: unique(filters.name),
      symbol: unique(filters.symbol)
    },
    unknownFieldTokens: unique(unknownFieldTokens),
    reasonCodes: unique(reasonCodes)
  };
}
function queryTokens(raw) {
  const tokens = [];
  const pattern = /"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|`([^`\\]*(?:\\.[^`\\]*)*)`|(\S+)/g;
  for (const match of raw.matchAll(pattern)) {
    const quoted = match[1] !== undefined || match[2] !== undefined || match[3] !== undefined;
    const text = match[1] ?? match[2] ?? match[3] ?? match[4] ?? "";
    if (text.length > 0)
      tokens.push({ text, quoted });
  }
  return tokens;
}
function parseFieldedToken(token) {
  const match = /^([A-Za-z][A-Za-z0-9_-]*):(.*)$/.exec(token);
  if (!match)
    return;
  return { field: match[1].toLowerCase(), value: match[2] ?? "" };
}
function normalizeFilterValue(value) {
  return value.trim().replace(/^(["'`])(.+)\1$/, "$2").toLowerCase();
}
function normalizeLanguageFilter(value) {
  const normalized = value.toLowerCase();
  const aliases = {
    ts: "typescript",
    tsx: "typescript",
    js: "javascript",
    jsx: "javascript",
    md: "markdown",
    mdx: "markdown"
  };
  const language = aliases[normalized] ?? normalized;
  if (!/^[a-z][a-z0-9_+-]*$/.test(language))
    return;
  return language;
}
function unique(values) {
  return [...new Set(values.filter((value) => value.length > 0))];
}
export {
  parseCodeSearchQueryV1,
  getPaletteSectionTitle,
  getPaletteSectionId,
  UNIFIED_SEARCH_RESULT_CONTRACT_VERSION,
  PALETTE_SOURCE_SECTION_IDS,
  PALETTE_SECTION_TITLES,
  CODE_SEARCH_QUERY_V1_KINDS
};
