//#region imports
import { config } from 'tnp-core/lib';
import { fse, path } from 'tnp-core/lib';
import { Helpers } from 'tnp-core/lib';
import { UtilsQuickFixes } from '../../index';
import { BaseFeatureForProject } from './base-feature-for-project';
//#endregion
export class BaseQuickFixes extends 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 (!Helpers.exists(absPath)) {
                Helpers.warn(`File for quick fix not found: ${absPath}`);
                continue;
            }
            const fileContent = Helpers.readFile(absPath) || '';
            if (fileContent) {
                if (!fileContent.startsWith(`// @ts-${'nocheck'}`)) {
                    const contentFixed = `// @ts-${'nocheck'}\n${fileContent}`;
                    if (fileContent !== contentFixed) {
                        Helpers.writeFile(absPath, contentFixed);
                    }
                }
            }
        }
        //#endregion
    }
    //#region fix sqlite pacakge in node_modules
    fixSQLLiteModuleInNodeModules() {
        //#region @backendFunc
        const filePath = this.project.pathFor(`${config.folder.node_modules}/sql.js/dist/sql-wasm.js`);
        if (!Helpers.exists(filePath)) {
            return;
        }
        const content = Helpers.readFile(filePath);
        const fixedContent = UtilsQuickFixes.replaceKnownFaultyCode(content);
        Helpers.writeFile(filePath, fixedContent);
        //#endregion
    }
    //#endregion
    //#region add missing empty libs
    createDummyEmptyLibsReplacements(missingLibsNames = []) {
        //#region @backendFunc
        missingLibsNames.forEach(missingLibName => {
            const pathInProjectNodeModules = path.join(this.project.location, config.folder.node_modules, missingLibName);
            if (fse.existsSync(pathInProjectNodeModules)) {
                Helpers.warn(`Package "${missingLibName}" will replaced with empty package mock. ${this.project.genericName}`);
            }
            // Helpers.remove(pathInProjectNodeModules);
            if (!fse.existsSync(pathInProjectNodeModules)) {
                Helpers.mkdirp(pathInProjectNodeModules);
            }
            Helpers.writeFile(path.join(pathInProjectNodeModules, 'index.js'), `
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = {};
`);
            Helpers.writeFile(path.join(pathInProjectNodeModules, 'index.d.ts'), `
declare const _default: {};
export default _default;
`);
            Helpers.writeFile(path.join(pathInProjectNodeModules, config.file.package_json), {
                name: missingLibName,
                version: '0.0.0',
            });
        });
        //#endregion
    }
}
//# sourceMappingURL=base-quick-fixes.js.map