//#region imports
import { config } from 'tnp-core/lib-prod';
import { fse, path } from 'tnp-core/lib-prod';
import { Helpers__NS__exists, Helpers__NS__mkdirp, Helpers__NS__readFile, Helpers__NS__warn, Helpers__NS__writeFile } from 'tnp-core/lib-prod';
import { UtilsQuickFixes__NS__replaceKnownFaultyCode } 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__NS__exists(absPath)) {
                Helpers__NS__warn(`File for quick fix not found: ${absPath}`);
                continue;
            }
            const fileContent = Helpers__NS__readFile(absPath) || '';
            if (fileContent) {
                if (!fileContent.startsWith(`// @ts-${'nocheck'}`)) {
                    const contentFixed = `// @ts-${'nocheck'}\n${fileContent}`;
                    if (fileContent !== contentFixed) {
                        Helpers__NS__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__NS__exists(filePath)) {
            return;
        }
        const content = Helpers__NS__readFile(filePath);
        const fixedContent = UtilsQuickFixes__NS__replaceKnownFaultyCode(content);
        Helpers__NS__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__NS__warn(`Package "${missingLibName}" will replaced with empty package mock. ${this.project.genericName}`);
            }
            // Helpers__NS__remove(pathInProjectNodeModules);
            if (!fse.existsSync(pathInProjectNodeModules)) {
                Helpers__NS__mkdirp(pathInProjectNodeModules);
            }
            Helpers__NS__writeFile(path.join(pathInProjectNodeModules, 'index.js'), `
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = {};
`);
            Helpers__NS__writeFile(path.join(pathInProjectNodeModules, 'index.d.ts'), `
declare const _default: {};
export default _default;
`);
            Helpers__NS__writeFile(path.join(pathInProjectNodeModules, config.file.package_json), {
                name: missingLibName,
                version: '0.0.0',
            });
        });
        //#endregion
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-quick-fixes.js.map