import { Config } from "@herb-tools/config";
import { defaultFormatOptions } from "@herb-tools/formatter";
import { version } from "../package.json";
export class Settings {
    constructor(params, connection) {
        var _a;
        this.defaultSettings = {
            linter: {
                enabled: true,
                fixOnSave: true
            },
            formatter: {
                enabled: false,
                indentWidth: defaultFormatOptions.indentWidth,
                maxLineLength: defaultFormatOptions.maxLineLength
            }
        };
        this.globalSettings = this.defaultSettings;
        this.documentSettings = new Map();
        this.hasConfigurationCapability = false;
        this.hasWorkspaceFolderCapability = false;
        this.hasDiagnosticRelatedInformationCapability = false;
        this.hasShowDocumentCapability = false;
        this.params = params;
        this.capabilities = params.capabilities;
        this.connection = connection;
        this.hasConfigurationCapability = !!(this.capabilities.workspace && !!this.capabilities.workspace.configuration);
        this.hasShowDocumentCapability = !!((_a = this.capabilities.window) === null || _a === void 0 ? void 0 : _a.showDocument);
        this.hasWorkspaceFolderCapability = !!(this.capabilities.workspace && !!this.capabilities.workspace.workspaceFolders);
        this.hasDiagnosticRelatedInformationCapability = !!(this.capabilities.textDocument &&
            this.capabilities.textDocument.publishDiagnostics &&
            this.capabilities.textDocument.publishDiagnostics.relatedInformation);
    }
    async initializeProjectConfig(config) {
        if (config) {
            this.projectConfig = config;
            return;
        }
        try {
            this.projectConfig = await Config.loadForEditor(this.projectPath, version);
        }
        catch (error) {
            this.connection.console.warn(`Failed to load project config: ${error}`);
            this.projectConfig = undefined;
        }
    }
    async refreshProjectConfig(config) {
        await this.initializeProjectConfig(config);
        this.documentSettings.clear();
    }
    // TODO: ideally we can just use Config all the way through
    mergeSettings(userSettings, projectConfig) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
        const settings = userSettings || this.defaultSettings;
        const hasConfigFile = projectConfig ? Config.exists(projectConfig.projectPath) : false;
        if (!projectConfig || !hasConfigFile) {
            return {
                trace: settings.trace,
                linter: {
                    enabled: (_b = (_a = settings.linter) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : this.defaultSettings.linter.enabled,
                    fixOnSave: (_d = (_c = settings.linter) === null || _c === void 0 ? void 0 : _c.fixOnSave) !== null && _d !== void 0 ? _d : this.defaultSettings.linter.fixOnSave
                },
                formatter: {
                    enabled: (_f = (_e = settings.formatter) === null || _e === void 0 ? void 0 : _e.enabled) !== null && _f !== void 0 ? _f : this.defaultSettings.formatter.enabled,
                    indentWidth: (_h = (_g = settings.formatter) === null || _g === void 0 ? void 0 : _g.indentWidth) !== null && _h !== void 0 ? _h : this.defaultSettings.formatter.indentWidth,
                    maxLineLength: (_k = (_j = settings.formatter) === null || _j === void 0 ? void 0 : _j.maxLineLength) !== null && _k !== void 0 ? _k : this.defaultSettings.formatter.maxLineLength
                }
            };
        }
        return {
            trace: settings.trace,
            linter: {
                enabled: projectConfig.isLinterEnabled,
                fixOnSave: (_m = (_l = settings.linter) === null || _l === void 0 ? void 0 : _l.fixOnSave) !== null && _m !== void 0 ? _m : this.defaultSettings.linter.fixOnSave
            },
            formatter: {
                enabled: projectConfig.isFormatterEnabled,
                indentWidth: (_p = (_o = projectConfig.formatter) === null || _o === void 0 ? void 0 : _o.indentWidth) !== null && _p !== void 0 ? _p : this.defaultSettings.formatter.indentWidth,
                maxLineLength: (_r = (_q = projectConfig.formatter) === null || _q === void 0 ? void 0 : _q.maxLineLength) !== null && _r !== void 0 ? _r : this.defaultSettings.formatter.maxLineLength
            }
        };
    }
    get projectPath() {
        var _a, _b, _c, _d, _e;
        const uri = (_e = (_d = (_c = (_b = (_a = this.params.workspaceFolders) === null || _a === void 0 ? void 0 : _a.at(0)) === null || _b === void 0 ? void 0 : _b.uri) !== null && _c !== void 0 ? _c : this.params.rootUri) !== null && _d !== void 0 ? _d : this.params.rootPath) !== null && _e !== void 0 ? _e : "";
        return uri.replace(/^file:\/\//, "");
    }
    getDocumentSettings(resource) {
        if (!this.hasConfigurationCapability) {
            const merged = this.mergeSettings(this.globalSettings, this.projectConfig);
            return Promise.resolve(merged);
        }
        let result = this.documentSettings.get(resource);
        if (!result) {
            result = this.connection.workspace.getConfiguration({
                scopeUri: resource,
                section: "languageServerHerb",
            }).then((userSettings) => {
                return this.mergeSettings(userSettings, this.projectConfig);
            });
            this.documentSettings.set(resource, result);
        }
        return result;
    }
}
//# sourceMappingURL=settings.js.map