const isRelativeLike = (s) => /^[./]/.test(s) && !/^https?:|^data:|^mailto:|^#/.test(s);
const MARKDOWN_EXTS = /* @__PURE__ */ new Set([".md", ".mdx", ".markdown"]);
const isMarkdownPath = (s) => {
  const dot = s.lastIndexOf(".");
  return dot !== -1 && MARKDOWN_EXTS.has(s.slice(dot).toLowerCase());
};
function extractMarkdownRefs(source) {
  const refs = [];
  const linkRe = /!?\[[^\]]*\]\(([^)]+)\)/g;
  let m;
  const lines = source.split("\n");
  const lineOf = (idx) => source.slice(0, idx).split("\n").length;
  while ((m = linkRe.exec(source)) !== null) {
    const target = m[1].split(" ")[0].trim();
    if (!isRelativeLike(target) || !isMarkdownPath(target)) continue;
    refs.push({ sourceSymbol: null, targetName: target, referenceType: "import", startLine: lineOf(m.index), verifiability: "inferred" });
  }
  return refs;
}
function extractHtmlRefs(source) {
  const refs = [];
  const attrRe = /(?:href|src)=["']([^"'#?]+)["']/gi;
  const importRe = /@import\s+["']([^"']+)["']/g;
  const urlRe = /url\(\s*["']?([^"')]+)["']?\s*\)/g;
  const lineOf = (idx) => source.slice(0, idx).split("\n").length;
  for (const re of [attrRe, importRe, urlRe]) {
    let m;
    while ((m = re.exec(source)) !== null) {
      const target = m[1].trim();
      if (!isRelativeLike(target)) continue;
      refs.push({ sourceSymbol: null, targetName: target, referenceType: "import", startLine: lineOf(m.index), verifiability: "inferred" });
    }
  }
  return refs;
}
function extractCssRefs(source) {
  const refs = [];
  const importRe = /@import\s+(?:url\(\s*)?["']?([^"');\s]+)["']?(?:\s*\))?/g;
  const urlRe = /url\(\s*["']?([^"')]+)["']?\s*\)/g;
  const lineOf = (idx) => source.slice(0, idx).split("\n").length;
  for (const re of [importRe, urlRe]) {
    let m;
    while ((m = re.exec(source)) !== null) {
      const target = m[1].trim();
      if (!isRelativeLike(target)) continue;
      refs.push({ sourceSymbol: null, targetName: target, referenceType: "import", startLine: lineOf(m.index), verifiability: "inferred" });
    }
  }
  return refs;
}
function stripJsoncSyntax(raw) {
  let result = "";
  let i = 0;
  const len = raw.length;
  while (i < len) {
    const ch = raw[i];
    if (ch === '"') {
      let j = i + 1;
      while (j < len && raw[j] !== '"') {
        if (raw[j] === "\\") j++;
        j++;
      }
      result += raw.slice(i, j + 1);
      i = j + 1;
      continue;
    }
    if (ch === "'") {
      let j = i + 1;
      let inner = "";
      while (j < len && raw[j] !== "'") {
        if (raw[j] === "\\") {
          inner += raw[j] + raw[j + 1];
          j += 2;
        } else {
          inner += raw[j] === '"' ? '\\"' : raw[j];
          j++;
        }
      }
      result += '"' + inner + '"';
      i = j + 1;
      continue;
    }
    if (ch === "/" && i + 1 < len && raw[i + 1] === "/") {
      i += 2;
      while (i < len && raw[i] !== "\n") i++;
      continue;
    }
    if (ch === "/" && i + 1 < len && raw[i + 1] === "*") {
      i += 2;
      while (i + 1 < len && !(raw[i] === "*" && raw[i + 1] === "/")) i++;
      i += 2;
      continue;
    }
    result += ch;
    i++;
  }
  result = result.replace(/,\s*([}\]])/g, "$1");
  result = result.replace(/([\{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g, '$1"$2":');
  return result;
}
function extractJsonRefs(source) {
  const refs = [];
  let parsed;
  try {
    parsed = JSON.parse(stripJsoncSyntax(source));
  } catch {
    return refs;
  }
  const check = (val, line) => {
    if (typeof val === "string" && isRelativeLike(val) && val.includes("/")) {
      refs.push({ sourceSymbol: null, targetName: val, referenceType: "import", startLine: line, verifiability: "inferred" });
    }
  };
  for (const key of ["$ref", "$schema", "extends"]) {
    if (typeof parsed?.[key] === "string") check(parsed[key], 1);
    else if (Array.isArray(parsed?.[key])) parsed[key].forEach((v) => check(v, 1));
  }
  return refs;
}
function extractYamlRefs(source) {
  const refs = [];
  const lines = source.split("\n");
  const yamlPathRe = /["']?(\.\.?\/[^"'\s#]+?\.(?:yaml|yml))["']?/g;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    let m;
    yamlPathRe.lastIndex = 0;
    while ((m = yamlPathRe.exec(line)) !== null) {
      const target = m[1].trim();
      refs.push({
        sourceSymbol: null,
        targetName: target,
        referenceType: "import",
        startLine: i + 1,
        verifiability: "inferred"
      });
    }
  }
  return refs;
}
class StaticFileParser {
  languageName = "static";
  get supportedExtensions() {
    return [".md", ".mdx", ".markdown", ".html", ".htm", ".xhtml", ".css", ".scss", ".sass", ".less", ".json", ".jsonc", ".json5", ".yaml", ".yml"];
  }
  async parse(filePath, source) {
    const ext = filePath.split(".").pop()?.toLowerCase() ?? "";
    let references = [];
    if (ext === "md" || ext === "mdx" || ext === "markdown") {
      references = extractMarkdownRefs(source);
    } else if (ext === "html" || ext === "htm" || ext === "xhtml") {
      references = extractHtmlRefs(source);
    } else if (ext === "css" || ext === "scss" || ext === "sass" || ext === "less") {
      references = extractCssRefs(source);
    } else if (ext === "json" || ext === "jsonc" || ext === "json5") {
      references = extractJsonRefs(source);
    } else if (ext === "yaml" || ext === "yml") {
      references = extractYamlRefs(source);
    }
    return { symbols: [], references, errors: [] };
  }
}
export {
  StaticFileParser
};
