// src/actions/dead/parseCodeWorkspace.ts
import fs from "node:fs";
import path from "node:path";
function consumeString(input, ctx, quote) {
  ctx.out += input[ctx.i];
  ctx.i += 1;
  while (ctx.i < input.length) {
    const ch = input[ctx.i];
    ctx.out += ch;
    if (ch === "\\" && ctx.i + 1 < input.length) {
      ctx.out += input[ctx.i + 1];
      ctx.i += 2;
      continue;
    }
    if (ch === quote) {
      ctx.i += 1;
      return;
    }
    ctx.i += 1;
  }
}
function consumeLineComment(input, ctx) {
  while (ctx.i < input.length && input[ctx.i] !== "\n") ctx.i += 1;
}
function consumeBlockComment(input, ctx) {
  ctx.i += 2;
  while (ctx.i < input.length && !(input[ctx.i] === "*" && input[ctx.i + 1] === "/")) ctx.i += 1;
  ctx.i += 2;
}
function stripJsonc(input) {
  const ctx = { i: 0, out: "" };
  while (ctx.i < input.length) {
    const ch = input[ctx.i];
    const next = ctx.i + 1 < input.length ? input[ctx.i + 1] : "";
    if (ch === '"' || ch === "'") {
      consumeString(input, ctx, ch);
      continue;
    }
    if (ch === "/" && next === "/") {
      consumeLineComment(input, ctx);
      continue;
    }
    if (ch === "/" && next === "*") {
      consumeBlockComment(input, ctx);
      continue;
    }
    ctx.out += ch;
    ctx.i += 1;
  }
  return ctx.out.replaceAll(/,(\s*[}\]])/g, "$1");
}
function parseWorkspaceJson(workspaceFilePath, stripped) {
  try {
    return JSON.parse(stripped);
  } catch (ex) {
    throw new Error(
      `Failed to parse code-workspace file ${workspaceFilePath}: ${ex instanceof Error ? ex.message : String(ex)}`,
      { cause: ex }
    );
  }
}
function parseCodeWorkspace(workspaceFilePath) {
  const raw = fs.readFileSync(workspaceFilePath, "utf8");
  const stripped = stripJsonc(raw);
  const parsed = parseWorkspaceJson(workspaceFilePath, stripped);
  if (!parsed.folders || !Array.isArray(parsed.folders)) return [];
  const baseDir = path.dirname(workspaceFilePath);
  const results = [];
  for (const entry of parsed.folders) {
    if (!entry || typeof entry.path !== "string") continue;
    const rawPath = entry.path;
    const absolutePath = path.isAbsolute(rawPath) ? rawPath : path.resolve(baseDir, rawPath);
    results.push({
      absolutePath,
      name: typeof entry.name === "string" ? entry.name : void 0,
      rawPath
    });
  }
  return results;
}
export {
  parseCodeWorkspace
};
//# sourceMappingURL=parseCodeWorkspace.mjs.map
