import {AstroCheck, DiagnosticSeverity} from "@astrojs/language-server";
import {bold, black, bgWhite, red, cyan, yellow} from "kleur/colors";
import glob from "fast-glob";
import * as path from "path";
import {pathToFileURL} from "url";
import * as fs from "fs";
async function openAllDocuments(workspaceUri, filePathsToIgnore, checker) {
  const files = await glob("**/*.astro", {
    cwd: workspaceUri.pathname,
    ignore: ["node_modules/**"].concat(filePathsToIgnore.map((ignore) => `${ignore}/**`))
  });
  const absFilePaths = files.map((f) => path.resolve(workspaceUri.pathname, f));
  for (const absFilePath of absFilePaths) {
    const text = fs.readFileSync(absFilePath, "utf-8");
    checker.upsertDocument({
      uri: pathToFileURL(absFilePath).toString(),
      text
    });
  }
}
function offsetAt({line, character}, text) {
  let i = 0;
  let l = 0;
  let c = 0;
  while (i < text.length) {
    if (l === line && c === character) {
      break;
    }
    let char = text[i];
    switch (char) {
      case "\n": {
        l++;
        c = 0;
        break;
      }
      default: {
        c++;
        break;
      }
    }
    i++;
  }
  return i;
}
function pad(str, len) {
  return Array.from({length: len}, () => str).join("");
}
async function run() {
}
async function check(astroConfig) {
  const root = astroConfig.projectRoot;
  let checker = new AstroCheck(root.toString());
  await openAllDocuments(root, [], checker);
  let diagnostics = await checker.getDiagnostics();
  let result = {
    errors: 0,
    warnings: 0
  };
  diagnostics.forEach((diag) => {
    diag.diagnostics.forEach((d) => {
      switch (d.severity) {
        case DiagnosticSeverity.Error: {
          console.error(`${bold(cyan(path.relative(root.pathname, diag.filePath)))}:${bold(yellow(d.range.start.line))}:${bold(yellow(d.range.start.character))} - ${d.message}`);
          let startOffset = offsetAt({line: d.range.start.line, character: 0}, diag.text);
          let endOffset = offsetAt({line: d.range.start.line + 1, character: 0}, diag.text);
          let str = diag.text.substring(startOffset, endOffset - 1);
          const lineNumStr = d.range.start.line.toString();
          const lineNumLen = lineNumStr.length;
          console.error(`${bgWhite(black(lineNumStr))}  ${str}`);
          let tildes = pad("~", d.range.end.character - d.range.start.character);
          let spaces = pad(" ", d.range.start.character + lineNumLen - 1);
          console.error(`   ${spaces}${bold(red(tildes))}
`);
          result.errors++;
          break;
        }
        case DiagnosticSeverity.Warning: {
          result.warnings++;
          break;
        }
      }
    });
  });
  if (result.errors) {
    console.error(`Found ${result.errors} errors.`);
  }
  const exitCode = result.errors ? 1 : 0;
  return exitCode;
}
export {
  check,
  run
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NsaS9jaGVjay50cyJdLAogICJtYXBwaW5ncyI6ICJBQUNBO0FBR0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUVBLGdDQUFnQyxjQUFtQixtQkFBNkIsU0FBcUI7QUFDbkcsUUFBTSxRQUFRLE1BQU0sS0FBSyxjQUFjO0FBQUEsSUFDckMsS0FBSyxhQUFhO0FBQUEsSUFDbEIsUUFBUSxDQUFDLG1CQUFtQixPQUFPLGtCQUFrQixJQUFJLENBQUMsV0FBVyxHQUFHO0FBQUE7QUFFMUUsUUFBTSxlQUFlLE1BQU0sSUFBSSxDQUFDLE1BQU0sS0FBSyxRQUFRLGFBQWEsVUFBVTtBQUUxRSxhQUFXLGVBQWUsY0FBYztBQUN0QyxVQUFNLE9BQU8sR0FBRyxhQUFhLGFBQWE7QUFDMUMsWUFBUSxlQUFlO0FBQUEsTUFDckIsS0FBSyxjQUFjLGFBQWE7QUFBQSxNQUNoQztBQUFBO0FBQUE7QUFBQTtBQVVOLGtCQUFrQixDQUFFLE1BQU0sWUFBa0QsTUFBYztBQUN4RixNQUFJLElBQUk7QUFDUixNQUFJLElBQUk7QUFDUixNQUFJLElBQUk7QUFDUixTQUFPLElBQUksS0FBSyxRQUFRO0FBQ3RCLFFBQUksTUFBTSxRQUFRLE1BQU0sV0FBVztBQUNqQztBQUFBO0FBR0YsUUFBSSxPQUFPLEtBQUs7QUFDaEIsWUFBUTtBQUFBLFdBQ0QsTUFBTTtBQUNUO0FBQ0EsWUFBSTtBQUNKO0FBQUE7Q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