"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseQuickFixes = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const lib_3 = require("tnp-core/lib");
const index_1 = require("../../index");
const base_feature_for_project_1 = require("./base-feature-for-project");
//#endregion
class BaseQuickFixes extends base_feature_for_project_1.BaseFeatureForProject {
    project;
    constructor(project) {
        super(project);
        this.project = project;
        this.project = project;
    }
    /**
     * filesRelativePaths example:
     * ['node_modules/@types/glob/index.d.ts', '/home/user/project/src/app.ts']
     *
     * @param filesRelativeAbsPaths\ Quick fix for typescript check
     */
    excludeNodeModulesDtsFromTypescriptCheck(filesRelativeAbsPaths) {
        //#region @backendFunc
        for (const absPath of filesRelativeAbsPaths) {
            if (!lib_3.Helpers.exists(absPath)) {
                lib_3.Helpers.warn(`File for quick fix not found: ${absPath}`);
                continue;
            }
            const fileContent = lib_3.Helpers.readFile(absPath) || '';
            if (fileContent) {
                if (!fileContent.startsWith(`// @ts-${'nocheck'}`)) {
                    const contentFixed = `// @ts-${'nocheck'}\n${fileContent}`;
                    if (fileContent !== contentFixed) {
                        lib_3.Helpers.writeFile(absPath, contentFixed);
                    }
                }
            }
        }
        //#endregion
    }
    //#region fix sqlite pacakge in node_modules
    fixSQLLiteModuleInNodeModules() {
        //#region @backendFunc
        const filePath = this.project.pathFor(`${lib_1.config.folder.node_modules}/sql.js/dist/sql-wasm.js`);
        if (!lib_3.Helpers.exists(filePath)) {
            return;
        }
        const content = lib_3.Helpers.readFile(filePath);
        const fixedContent = index_1.UtilsQuickFixes.replaceKnownFaultyCode(content);
        lib_3.Helpers.writeFile(filePath, fixedContent);
        //#endregion
    }
    //#endregion
    //#region add missing empty libs
    createDummyEmptyLibsReplacements(missingLibsNames = []) {
        //#region @backendFunc
        missingLibsNames.forEach(missingLibName => {
            const pathInProjectNodeModules = lib_2.path.join(this.project.location, lib_1.config.folder.node_modules, missingLibName);
            if (lib_2.fse.existsSync(pathInProjectNodeModules)) {
                lib_3.Helpers.warn(`Package "${missingLibName}" will replaced with empty package mock. ${this.project.genericName}`);
            }
            // Helpers.remove(pathInProjectNodeModules);
            if (!lib_2.fse.existsSync(pathInProjectNodeModules)) {
                lib_3.Helpers.mkdirp(pathInProjectNodeModules);
            }
            lib_3.Helpers.writeFile(lib_2.path.join(pathInProjectNodeModules, 'index.js'), `
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = {};
`);
            lib_3.Helpers.writeFile(lib_2.path.join(pathInProjectNodeModules, 'index.d.ts'), `
declare const _default: {};
export default _default;
`);
            lib_3.Helpers.writeFile(lib_2.path.join(pathInProjectNodeModules, lib_1.config.file.package_json), {
                name: missingLibName,
                version: '0.0.0',
            });
        });
        //#endregion
    }
}
exports.BaseQuickFixes = BaseQuickFixes;
//# sourceMappingURL=base-quick-fixes.js.map