export class DocumentSaveService {
    constructor(connection, settings, autofixService, formattingService) {
        this.connection = connection;
        this.settings = settings;
        this.autofixService = autofixService;
        this.formattingService = formattingService;
    }
    /**
     * Apply only autofix edits on save.
     * Called by willSaveWaitUntil - formatting is handled separately by editor.formatOnSave
     */
    async applyFixes(document) {
        var _a;
        const settings = await this.settings.getDocumentSettings(document.uri);
        const fixOnSave = ((_a = settings === null || settings === void 0 ? void 0 : settings.linter) === null || _a === void 0 ? void 0 : _a.fixOnSave) !== false;
        this.connection.console.log(`[DocumentSave] applyFixes fixOnSave=${fixOnSave}`);
        if (!fixOnSave)
            return [];
        return this.autofixService.autofix(document);
    }
    /**
     * Apply autofix and formatting.
     * Called by onDocumentFormatting (manual format or editor.formatOnSave)
     */
    async applyFixesAndFormatting(document, reason) {
        var _a, _b, _c;
        const settings = await this.settings.getDocumentSettings(document.uri);
        const fixOnSave = ((_a = settings === null || settings === void 0 ? void 0 : settings.linter) === null || _a === void 0 ? void 0 : _a.fixOnSave) !== false;
        const formatterEnabled = (_c = (_b = settings === null || settings === void 0 ? void 0 : settings.formatter) === null || _b === void 0 ? void 0 : _b.enabled) !== null && _c !== void 0 ? _c : false;
        this.connection.console.log(`[DocumentSave] applyFixesAndFormatting fixOnSave=${fixOnSave}, formatterEnabled=${formatterEnabled}`);
        let autofixEdits = [];
        if (fixOnSave) {
            autofixEdits = await this.autofixService.autofix(document);
        }
        if (!formatterEnabled)
            return autofixEdits;
        if (autofixEdits.length === 0) {
            return this.formattingService.formatOnSave(document, reason);
        }
        const autofixedDocument = {
            ...document,
            uri: document.uri,
            getText: () => autofixEdits[0].newText,
        };
        return this.formattingService.formatOnSave(autofixedDocument, reason);
    }
}
//# sourceMappingURL=document_save_service.js.map