import { Herb } from "@herb-tools/node-wasm";
import { Linter } from "@herb-tools/linter";
import { getFullDocumentRange } from "./utils";
export class AutofixService {
    constructor(connection, config) {
        this.connection = connection;
        this.linter = this.buildLinter(config);
    }
    setConfig(config) {
        this.linter = this.buildLinter(config);
    }
    buildLinter(config) {
        return Linter.from(Herb, config);
    }
    async autofix(document) {
        try {
            const text = document.getText();
            const lintResult = this.linter.lint(text, { fileName: document.uri });
            const offensesToFix = lintResult.offenses;
            if (offensesToFix.length === 0)
                return [];
            const autofixResult = this.linter.autofix(text, { fileName: document.uri }, offensesToFix);
            if (autofixResult.source === text)
                return [];
            return [{ range: getFullDocumentRange(document), newText: autofixResult.source }];
        }
        catch (error) {
            this.connection.console.error(`[Autofix] Failed: ${error}`);
            return [];
        }
    }
}
//# sourceMappingURL=autofix_service.js.map