import eol from "eol";
import path from "path";
import slash from "slash";
import { fileURLToPath, pathToFileURL } from "url";
import resolve from "resolve";
function canonicalURL(url, base) {
  let pathname = url.replace(/\/index.html$/, "");
  pathname = pathname.replace(/\/1\/?$/, "");
  if (!path.extname(pathname))
    pathname = pathname.replace(/(\/+)?$/, "/");
  pathname = pathname.replace(/\/+/g, "/");
  return new URL(pathname, base);
}
function parseNpmName(spec) {
  if (!spec || spec[0] === "." || spec[0] === "/")
    return void 0;
  let scope;
  let name = "";
  let parts = spec.split("/");
  if (parts[0][0] === "@") {
    scope = parts[0];
    name = parts.shift() + "/";
  }
  name += parts.shift();
  let subpath = parts.length ? `./${parts.join("/")}` : void 0;
  return {
    scope,
    name,
    subpath
  };
}
function codeFrame(src, loc) {
  if (!loc)
    return "";
  const lines = eol.lf(src).split("\n");
  const visibleLines = [];
  for (let n = -2; n <= 2; n++) {
    if (lines[loc.line + n])
      visibleLines.push(loc.line + n);
  }
  let gutterWidth = 0;
  for (const lineNo of visibleLines) {
    let w = `> ${lineNo}`;
    if (w.length > gutterWidth)
      gutterWidth = w.length;
  }
  let output = "";
  for (const lineNo of visibleLines) {
    const isFocusedLine = lineNo === loc.line - 1;
    output += isFocusedLine ? "> " : "  ";
    output += `${lineNo + 1} | ${lines[lineNo]}
`;
    if (isFocusedLine)
      output += `${[...new Array(gutterWidth)].join(" ")}  | ${[...new Array(loc.column)].join(" ")}^
`;
  }
  return output;
}
function resolveDependency(dep, astroConfig) {
  const resolved = resolve.sync(dep, {
    basedir: fileURLToPath(astroConfig.projectRoot)
  });
  return pathToFileURL(resolved).toString();
}
function viteID(filePath) {
  return slash(fileURLToPath(filePath));
}
export {
  canonicalURL,
  codeFrame,
  parseNpmName,
  resolveDependency,
  viteID
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvdXRpbC50cyJdLAogICJtYXBwaW5ncyI6ICJBQUVBO0FBQ0E7Q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