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 viteifyURL(filePath) {
  return slash(fileURLToPath(filePath));
}
export {
  canonicalURL,
  codeFrame,
  parseNpmName,
  resolveDependency,
  viteifyURL
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvdXRpbC50cyJdLAogICJtYXBwaW5ncyI6ICJBQUVBO0FB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