import { path, ___NS__first, ___NS__keys, Helpers__NS__taskDone, Helpers__NS__taskStarted, Helpers__NS__writeFile, UtilsMigrations__NS__getFormattedTimestampFromClassName, UtilsMigrations__NS__getTimestampFromClassName } from 'tnp-core/lib-prod';
import { BaseDebounceCompilerForProject, UtilsTypescript__NS__extractClassNamesFromFile, UtilsTypescript__NS__formatFile } from 'tnp-helpers/lib-prod';
import { indexTsFromMigrationsFromSrc, libFromSrc, MIGRATION_CONST_PREFIX, migration_index_autogenerated_ts, migrationIndexAutogeneratedTsFileRelativeToSrcPath, migrationsFromLib, srcMainProject, THIS_IS_GENERATED_INFO_COMMENT, } from '../../../../../constants';
//#endregion
export class MigrationHelper extends BaseDebounceCompilerForProject {
    //#region constructor
    constructor(project) {
        super(project, {
            folderPath: project.pathFor([
                srcMainProject,
                libFromSrc,
                migrationsFromLib,
            ]),
            taskName: 'MigrationHelper',
            notifyOnFileUnlink: true,
        });
    }
    //#endregion
    //#region migration index autogenerated ts file
    get migrationIndexAutogeneratedTsFileAbsPath() {
        return this.project.pathFor([
            srcMainProject,
            migrationIndexAutogeneratedTsFileRelativeToSrcPath,
        ]);
    }
    //#endregion
    //#region rebuild
    rebuild(relativePathsWithClasses) {
        //#region @backendFunc
        Helpers__NS__taskStarted(`Rebuilding migration index file...`);
        const allClassesByTimestamp = relativePathsWithClasses
            .reduce((acc, c) => acc.concat(c.classes), [])
            .sort((classA, classB) => {
            const timestampA = UtilsMigrations__NS__getTimestampFromClassName(classA);
            const timestampB = UtilsMigrations__NS__getTimestampFromClassName(classB);
            return timestampA - timestampB; // Ascending order
        });
        const allContexts = allClassesByTimestamp.reduce((alreadyAddedClassesObj, className) => {
            const contextName = ___NS__first(className.split('_')) || '';
            if (!alreadyAddedClassesObj[contextName]) {
                alreadyAddedClassesObj[contextName] = [];
            }
            alreadyAddedClassesObj[contextName].push(className);
            return alreadyAddedClassesObj;
        }, {});
        const allImports = relativePathsWithClasses
            .filter(c => c.classes.length >= 1)
            .map(c => {
            return (`import { ${c.classes.join(', ')} } from ` +
                `'./${c.relativePath.replace('.ts', '')}';`);
        });
        // console.log('MigrationHelper rebuild for ', allImports);
        // export const MIGRATIONS_CLASSES = [${allClasses.join(',\n')}];
        Helpers__NS__writeFile(this.migrationIndexAutogeneratedTsFileAbsPath, `
${THIS_IS_GENERATED_INFO_COMMENT}
${allImports.join('\n')}
${THIS_IS_GENERATED_INFO_COMMENT}
${___NS__keys(allContexts)
            .map(contextName => {
            const classes = allContexts[contextName];
            return (`export const ${MIGRATION_CONST_PREFIX}${contextName}` +
                ` = {${classes
                    .map(c => {
                    return `/* ${UtilsMigrations__NS__getFormattedTimestampFromClassName(c)} */ ${c}`;
                })
                    .join(',\n ')}};`);
        })
            .join('\n')}
${THIS_IS_GENERATED_INFO_COMMENT}
      `);
        UtilsTypescript__NS__formatFile(this.migrationIndexAutogeneratedTsFileAbsPath);
        Helpers__NS__taskDone(`Rebuilding migration index Done`);
        //#endregion
    }
    //#endregion
    //#region action
    action({ changeOfFiles, asyncEvent, }) {
        //#region @backendFunc
        changeOfFiles = changeOfFiles.filter(c => {
            return (c.fileAbsolutePath.endsWith('.ts') &&
                path.basename(c.fileAbsolutePath) !== migration_index_autogenerated_ts);
        });
        if (changeOfFiles.length === 0) {
            // console.log('MigrationHelper action skipped');
            return;
        }
        if (!this.project.hasFile([
            srcMainProject,
            libFromSrc,
            migrationsFromLib,
            indexTsFromMigrationsFromSrc,
        ])) {
            Helpers__NS__writeFile(this.project.pathFor([
                srcMainProject,
                libFromSrc,
                migrationsFromLib,
                indexTsFromMigrationsFromSrc,
            ]), `//@ts-nocheck
export * from './${migration_index_autogenerated_ts.replace('.ts', '')}';
        `.trim() + '\n');
        }
        // console.log('MigrationHelper action for ', changeOfFiles);
        this.rebuild(this.exitedFilesAbsPathes
            .filter(f => f.endsWith('.ts') && !f.endsWith('._auto-generated_.ts'))
            .map(c => {
            return {
                absPath: c,
                relativePath: this.project
                    .relative(c)
                    .split('/')
                    .slice(3)
                    .join('/'),
                classes: UtilsTypescript__NS__extractClassNamesFromFile(c),
            };
        }));
        //#endregion
    }
}
//# sourceMappingURL=migrations-helper.js.map