"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseIgnoreHideHelpers = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const base_feature_for_project_1 = require("./base-feature-for-project");
//#endregion
/**
 * Git Ignore
 * Vscode Files Hide
 * Npm Ignore
 */
class BaseIgnoreHideHelpers extends base_feature_for_project_1.BaseFeatureForProject {
    /**
     * (@default: false)
     *
     * If true, the files/folders/patterns that aren't really code and can
     * be recreated from template project like (tsconfig.json, .babelrc, .prettierc etc.)
     * will are stored in the repo
     */
    storeInRepoConfigFiles() {
        return true;
    }
    //#region init
    async init() {
        this.writeNpmIgnore();
        this.writeGitIgnore();
    }
    //#endregion
    //#region
    /**
     * @param data list of patterns
     * @returns list of patterns for recrusive ignore
     */
    recursivePatternTransformFn(data) {
        return data
            .map(c => {
            if (this.alwaysUseRecursivePattern().includes(c)) {
                return [`${c}`, `**/${c}`];
            }
            return [`${c}`];
        })
            .reduce((a, b) => {
            return a.concat(b);
        }, []);
    }
    //#endregion
    //#region patterns ignored in repo but visible to user
    /**
     * Have to start with /
     *
     * Usefull if you are generating some files and folder in src/*.* folder
     * (Taon CLI does this for automatic host generation)
     * @returns patterns that are ignored in repo but visible to user
     */
    getPatternsIgnoredInRepoButVisibleToUser() {
        return [
            '/*.sqlite', // you want to see your temp databases in the repo
            '/*.rest', // you want to see your temp rest databases in the repo
        ];
    }
    //#endregion
    //#region always ignored and hidden patterns
    alwaysIgnoredHiddenPatterns() {
        return [
            'chrome-profiler-events*.json',
            'speed-measure-plugin*.json',
            'tmp*',
            'npm-debug.log*',
        ];
    }
    alwaysUseRecursivePattern() {
        return ['.DS_Store'];
    }
    alwaysIgnoredAndHiddenFilesAndFolders() {
        return [
            '_changelog',
            '.sass-cache',
            '.sourcemaps',
            '.DS_Store',
            'npm-debug.log',
            'yarn-error.log',
            'testem.log',
            'connect.lock',
            'yarn-error.log',
            'node_modules',
            'libpeerconnection.log',
            'dist',
            'coverage',
            'Thumbs.db',
        ];
    }
    //#endregion
    //#region hidden but not neccesary ignored
    hiddenButNeverIgnoredInRepo() {
        return ['.gitignore']; // just to avoid mess when not .gitignore and started project
    }
    hiddenButNotNecessaryIgnoredInRepoFilesAndFolders() {
        return [
            '.npmignore',
            '.babelrc',
            '.editorconfig',
            'eslint.config.js',
            'protractor.conf.js',
            'karma.conf.js',
            '.prettierc',
            'angular.json',
            '.prettierrc.json',
            '.eslintrc.js',
            '.eslintrc.json',
            '.npmrc',
            '.yarnrc',
            '.idea',
            ...this.project.linter.getLintFiles(),
        ];
    }
    hiddenButNotNecessaryIgnoredInRepoPatterns() {
        const linkeProjectPrefix = this.project.linkedProjects.getLinkedProjectsConfig().prefix;
        return [
            'tsconfig*',
            'webpack.*',
            '.eslintrc*',
            'tslint.*',
            linkeProjectPrefix ? `${linkeProjectPrefix}*` : void 0,
        ].filter(f => !!f);
    }
    //#endregion
    //#region ignore for npm
    npmIgnoreFilesFoldersPatterns() {
        return [
            '.vscode',
            'dist',
            'src',
            'app',
            'source',
            'docs',
            'preview',
            'tests',
            'tsconfig.json',
            'npm-debug.log*',
        ];
    }
    //#endregion
    //#region public methods / write ignore files§
    // TODO
    writeGitIgnore() {
        //#region @backendFunc
        let filesAndFoldersToIgnoreInGit = this.recursivePatternTransformFn([
            ...this.alwaysIgnoredAndHiddenFilesAndFolders(),
            ...this.alwaysIgnoredHiddenPatterns(),
            ...this.getPatternsIgnoredInRepoButVisibleToUser().map(c => c.startsWith('/') ? c.slice(1) : c),
        ]);
        if (!this.storeInRepoConfigFiles()) {
            filesAndFoldersToIgnoreInGit = [
                ...filesAndFoldersToIgnoreInGit,
                ...this.recursivePatternTransformFn([
                    ...this.hiddenButNotNecessaryIgnoredInRepoFilesAndFolders(),
                    ...this.hiddenButNotNecessaryIgnoredInRepoPatterns(),
                ]),
            ];
        }
        lib_1.Helpers.writeFile(lib_2.path.join(this.project.location, '.gitignore'), lib_2.Utils.uniqArray(filesAndFoldersToIgnoreInGit
            .sort((a, b) => a.localeCompare(b))
            .map(c => `/${c}`)).join('\n'));
        //#endregion
    }
    //#endregion
    //#region public methods / get vscode hide settings
    /**
     * Use can apply this to .vscode/settings.json [files.exclude] property
     */
    getVscodeFilesFoldersAndPatternsToHide() {
        //#region @backendFunc
        const hideInVSCode = [
            ...this.alwaysIgnoredAndHiddenFilesAndFolders(),
            ...this.alwaysIgnoredHiddenPatterns(),
            ...this.hiddenButNeverIgnoredInRepo(),
            ...this.hiddenButNotNecessaryIgnoredInRepoFilesAndFolders(),
            ...this.hiddenButNotNecessaryIgnoredInRepoPatterns(),
        ];
        return this.recursivePatternTransformFn(hideInVSCode).reduce((acc, curr) => {
            acc[curr] = true;
            return acc;
        }, {});
        //#endregion
    }
    //#endregion
    //#region public methods / npm ignore
    writeNpmIgnore() {
        //#region @backendFunc
        lib_1.Helpers.writeFile(lib_2.path.join(this.project.location, '.npmignore'), this.npmIgnoreFilesFoldersPatterns()
            .map(c => `/${c}`)
            .join('\n'));
        //#endregion
    }
}
exports.BaseIgnoreHideHelpers = BaseIgnoreHideHelpers;
//# sourceMappingURL=base-ignore-hide.js.map