import { _, Helpers } from 'tnp-core/lib';
import { BaseFeatureForProject } from './base-feature-for-project';
// TODO
export class BaseLinter extends BaseFeatureForProject {
    async init() {
        this.writeLintFiles();
    }
    getLintFiles() {
        return ['.prettierignore', '.prettierrc', '.editorconfig'];
    }
    //#region get editorconfig settings
    getConfigEditorconfig() {
        return `
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
quote_type = single

[*.md]
max_line_length = off
trim_trailing_whitespace = false
`;
    }
    //#endregion
    //#region get prettier settings
    getConfigPrettierrc() {
        //#region @backendFunc
        return {
            tabWidth: 2,
            useTabs: false,
            singleQuote: true,
            semi: true,
            bracketSpacing: true,
            arrowParens: 'avoid',
            trailingComma: 'all',
            bracketSameLine: true,
            printWidth: 80,
            singleAttributePerLine: true,
            endOfLine: 'auto',
        };
        //#endregion
    }
    //#endregion
    //#region get prettier ignore settings
    getConfigPrettierignore() {
        //#region @backendFunc
        return `
# This file is generated by taon.dev
/build
/coverage
/e2e
/docs
/node_modules
*.md
**/*.md
tmp-*
**/tmp-*
tsconfig*
.vscode/launch*
taon-config-standalone.schema.json
taon-config-container.schema.json
**/src/assets/**/*.*
/.build
/projects
*.js

/dist*
/bundle*
/browser
/browser*
/websql
/websql*
/module*
/backup
/module
/www

      `;
        //#endregion
    }
    //#endregion
    //#region write lint files
    writeLintFiles() {
        //#region @backendFunc
        for (const fileName of this.getLintFiles()) {
            const nameOfMethod = `getConfig${_.upperFirst(_.camelCase(fileName))}`;
            if (this[nameOfMethod]) {
                const content = this[nameOfMethod]();
                if (_.isObject(content)) {
                    this.project.writeJson(fileName, content);
                }
                else {
                    this.project.writeFile(fileName, content);
                }
            }
            else {
                Helpers.warn(`You have to implement method ${nameOfMethod}() in class (Base)Linter`);
            }
        }
        //#endregion
    }
    //#endregion
    async start(throwInstedExit = false) {
        //#region @backend
        const files = this.project.git.stagedFiles.filter(f => Helpers.exists(f));
        const allRelatives = files
            .filter(f => !f.endsWith('.pot')) // TODO QUICK_FIX
            .map(f => f.replace(this.project.location + '/', ''))
            .join(' ');
        const relative = files
            .map(f => f.replace(this.project.location + '/', ''))
            .filter(element => element.endsWith('.html') || element.endsWith('.ts'))
            .join(' ');
        Helpers.info(`Running eslint for all staged files`);
        try {
            this.project
                .run(`npm-run eslint --fix ${relative}`, {
                output: true,
                biggerBuffer: false,
            })
                .sync();
        }
        catch (error) {
            if (throwInstedExit) {
                throw 'Eslint failed';
            }
            else {
                process.exit(0);
            }
        }
        // const relative = files.map(f => f.replace(this.project.location + '/', ''));
        // for (let index = 0; index < relative.length; index++) {
        //   const element = relative[index];
        //   if(element.endsWith('.html') || element.endsWith('.ts')) {
        //     Helpers.info(`Running prettier for ${element}`);
        //     this.project.tryRunSync(`npm-run eslint --fix "${element}"`, { biggerBuffer: false });
        //   }
        // }
        Helpers.info(`Running prettier for all staged files`);
        try {
            this.project
                .run(`prettier --write ${allRelatives}  --end-of-line auto`, {
                output: true,
                biggerBuffer: false,
            })
                .sync();
        }
        catch (error) {
            if (throwInstedExit) {
                throw 'Prettier failed';
            }
            else {
                process.exit(0);
            }
        }
        // for (let index = 0; index < files.length; index++) {
        //   const element = files[index];
        //   Helpers.info(`Running prettier for ${element.replace(this.project.location + '/', '')}`);
        //   this.project.tryRunSync(`prettier --write "${element}" --end-of-line auto`, { biggerBuffer: false });
        // }
        // const children = this.project.children.filter(f => {
        //   return ['shared', 'ikp'].includes(f.basename);
        // })
        // for (let index = 0; index < children.length; index++) {
        //   const child = children[index];
        //   this.project.tryRunSync(`npm-run ng lint ${child.basename}`, {})
        //   Helpers.info(`prettier start for ${child.basename}`)
        //   child.run('prettier --write "**/*.{js,json,css,scss,less,md,ts,html,component.html}"  --end-of-line auto',{
        //     biggerBuffer: true
        //   }).sync();
        //   Helpers.info(`prettier done for ${child.basename}`)
        // }
        //#endregion
    }
}
//# sourceMappingURL=base-linter.js.map