import eol from "eol";
import path from "path";
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 viteifyPath(pathname) {
  return `/@fs/${pathname.replace(/^\//, "")}`;
}
export {
  canonicalURL,
  codeFrame,
  parseNpmName,
  resolveDependency,
  viteifyPath
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvdXRpbC50cyJdLAogICJtYXBwaW5ncyI6ICJBQUVBO0FB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