"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseVscodeHelpers = void 0;
const lib_1 = require("tnp-core/lib");
const index_1 = require("../../index");
const base_feature_for_project_1 = require("./base-feature-for-project");
class BaseVscodeHelpers extends base_feature_for_project_1.BaseFeatureForProject {
    //#region init
    async init(options) {
        options = options || {};
        this.recreateExtensions();
        await this.recreateBaseSettings();
        this.recreateWindowTitle();
        if (!options.skipHiddingTempFiles) {
            this.toogleFilesVisibilityInVscode({
                action: 'hide-files',
                skipSaving: true,
            });
        }
        this.saveCurrentSettings();
    }
    //#endregion
    //#region fields
    /**
     * settings.json relative path
     */
    relativePathSettingsJsonVscode = '.vscode/settings.json';
    relativePathExtensionJsonVScode = '.vscode/extensions.json';
    currentSettingsValue;
    //#endregion
    //#region constructor
    constructor(project) {
        super(project);
        this.currentSettingsValue =
            lib_1.Helpers.readJsonC(project.pathFor(this.relativePathSettingsJsonVscode)) ||
                {};
    }
    //#endregion
    //#region modify vscode settings
    modifyVscode(modifyFN) {
        //#region @backendFunc
        try {
            // @ts-ignore
            this.currentSettingsValue = modifyFN(this.currentSettingsValue);
        }
        catch (error) {
            console.log(error);
            lib_1.Helpers.error(`Error during modifying vscode settings`, true, true);
        }
        //#endregion
    }
    //#endregion
    //#region save current settings
    saveCurrentSettings() {
        //#region @backendFunc
        this.project.writeJsonC(this.relativePathSettingsJsonVscode, this.currentSettingsValue);
        //#endregion
    }
    //#endregion
    //#region recreate extensions
    recreateExtensions() {
        //#region @backendFunc
        this.project.writeFile(this.relativePathExtensionJsonVScode, JSON.stringify({
            recommendations: index_1.UtilsVSCode.vscodeExtensions(),
        }, null, 2));
        //#endregion
    }
    //#endregion
    //#region recreate base settings
    getExecuteParamsForSettings() {
        return [this.project];
    }
    async recreateBaseSettings(options) {
        //#region @backendFunc
        options = options || {};
        options.save = !!options.save;
        const settings = await this.getBasicSettins();
        await this.resolveParamsVscode(settings, this.getExecuteParamsForSettings());
        lib_1._.merge(this.currentSettingsValue, settings);
        if (options.save) {
            this.saveCurrentSettings();
        }
        //#endregion
    }
    //#endregion
    //#region recraete window title
    recreateWindowTitle(options) {
        //#region @backendFunc
        options = options || {};
        options.save = !!options.save;
        lib_1._.set(this.currentSettingsValue, '["window.title"]', this.project.titleBarName);
        if (options.save) {
            this.saveCurrentSettings();
        }
        //#endregion
    }
    //#endregion
    //#region get vscode bottom color
    /**
     * by default left menu color and bottom status bar are the same
     */
    getVscodeBottomColor() {
        return void 0;
    }
    //#endregion
    //#region refresh colors in settings
    refreshColorsInSettings() {
        const overideBottomColor = this.getVscodeBottomColor();
        index_1.UtilsVSCode.regenerateVsCodeSettingsColors(this.project.location, overideBottomColor);
    }
    //#endregion
    //#region get basic settings
    async getBasicSettins() {
        const settings = {
            '[markdown]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[typescriptreact]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[json]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[jsonc]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[json5]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[scss]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[html]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[javascript]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[typescript]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            '[dockercompose]': {
                'editor.defaultFormatter': 'esbenp.prettier-vscode',
                'editor.formatOnSave': false,
            },
            'editor.rulers': [80, 120],
            'eslint.migration.2_x': 'off',
            'eslint.enable': true,
            'prettier.enable': true,
            'editor.suggest.snippetsPreventQuickSuggestions': false,
            'editor.inlineSuggest.enabled': true,
            'prettier.prettierPath': './node_modules/prettier',
            'prettier.endOfLine': 'auto', // fix for jumpling to end of file when file > 100kb
            // 'tslint.autoFixOnSave': false,
            // 'tslint.enable': false,
            // 'tslint.alwaysShowRuleFailuresAsWarnings': false,
            // 'github.copilot.nextEditSuggestions.enabled': true,
            // 'github.copilot.chat.languageContext.inline.typescript.enabled': true,
            // 'github.copilot.chat.languageContext.typescript.enabled': true,
            'typescript.suggest.autoImports': false,
            'javascript.suggest.autoImports': false,
        };
        settings['files.associations'] = {
            'tsconfig.json': 'json',
        };
        settings['search.followSymlinks'] = false;
        settings['search.useIgnoreFiles'] = false;
        settings['search.include'] = ['**/src/**'];
        settings['search.exclude'] = {
            bin: true,
            local_release: true,
            node_modules: true,
            '.build': true,
            '.vscode': true,
            browser: true,
            dist: true,
            ['dist-*']: true,
            'package-lock.json': true,
            'tmp-*': true,
            'src/lib/env/**/*.*': true,
            '*.rest': true,
        };
        Object.keys(settings['search.exclude']).forEach(k => {
            settings['search.exclude'][`**/${k}`] = true;
        });
        settings['search.exclude']['projects'] = true;
        return settings;
    }
    //#endregion
    //#region resolve params vscode
    async resolveParamsVscode(obj, prams) {
        //#region @backendFunc
        if (lib_1._.isObject(obj)) {
            const keys = Object.keys(obj);
            for (let index = 0; index < keys.length; index++) {
                const key = keys[index];
                if (lib_1._.isFunction(obj[key])) {
                    obj[key] = await lib_1.Helpers.runSyncOrAsync({
                        functionFn: obj[key],
                        arrayOfParams: prams,
                    });
                }
                else if (lib_1._.isArray(obj[key])) {
                    for (let index2 = 0; index2 < obj[key].length; index2++) {
                        const o = obj[key][index2];
                        await this.resolveParamsVscode(o, prams);
                    }
                }
                else if (lib_1._.isObject(obj[key])) {
                    await this.resolveParamsVscode(obj[key], prams);
                }
            }
        }
        //#endregion
    }
    //#endregion
    toogleFilesVisibilityInVscode(options) {
        options = options || {};
        const { action, skipSaving } = options;
        if (action === 'hide-files') {
            this.modifyVscode(settings => {
                settings['files.exclude'] =
                    this.project.ignoreHide.getVscodeFilesFoldersAndPatternsToHide() ||
                        {};
                return settings;
            });
            if (!skipSaving) {
                this.saveCurrentSettings();
            }
        }
        else if (action === 'show-files') {
            this.modifyVscode(settings => {
                settings['files.exclude'] = {};
                return settings;
            });
            if (!skipSaving) {
                this.saveCurrentSettings();
            }
        }
    }
    changeColorThemeVscode(white = true) {
        this.modifyVscode(settings => {
            settings['workbench.colorTheme'] = white
                ? 'Default Light+'
                : 'Kimbie Dark';
            return settings;
        });
        this.saveCurrentSettings();
    }
}
exports.BaseVscodeHelpers = BaseVscodeHelpers;
//# sourceMappingURL=base-vscode.js.map