// src/actions/dead/sourceEdits.ts
import fs from "node:fs";
import ts from "typescript";
function loadSourceFile(filePath, sourceText = fs.readFileSync(filePath, "utf8")) {
  const isJsLike = /\.(?:js|mjs|cjs)$/i.test(filePath);
  return ts.createSourceFile(
    filePath,
    sourceText,
    ts.ScriptTarget.Latest,
    true,
    isJsLike ? ts.ScriptKind.JS : void 0
  );
}
function isEmptySourceFile(sourceText, filePath) {
  if (sourceText.trim() === "") return true;
  return loadSourceFile(filePath, sourceText).statements.length === 0;
}
function removeEndAfterStatement(sourceText, statementEnd) {
  let end = statementEnd;
  while (end < sourceText.length && (sourceText[end] === " " || sourceText[end] === "	")) {
    end += 1;
  }
  if (sourceText[end] === "\r") end += 1;
  if (sourceText[end] === "\n") end += 1;
  return end;
}
function removeStatementFromSource(sourceText, sourceFile, statement) {
  const rangeStart = statement.getFullStart();
  const rangeEnd = removeEndAfterStatement(sourceText, statement.end);
  return `${sourceText.slice(0, rangeStart)}${sourceText.slice(rangeEnd)}`;
}
function removeStatementsFromSource(sourceText, sourceFile, statements) {
  const sorted = [...statements].toSorted((a, b) => b.getFullStart() - a.getFullStart());
  let result = sourceText;
  for (const statement of sorted) {
    result = removeStatementFromSource(result, sourceFile, statement);
  }
  return result;
}
export {
  isEmptySourceFile,
  loadSourceFile,
  removeStatementFromSource,
  removeStatementsFromSource
};
//# sourceMappingURL=sourceEdits.mjs.map
