import { _, Utils } from 'tnp-core/lib';
import { BaseCompilerForProject } from './base-compiler-for-project';
export class BaseDebounceCompilerForProject extends BaseCompilerForProject {
    initialParams;
    /**
     * default debounce time is 1s
     */
    debounceTime = 1000;
    /**
     * current files that are in project
     */
    exitedFilesAbsPathes = [];
    /**
     * @deprecated use action() instead
     */
    async syncAction(absFilesPathes, initialParams) {
        this.initialParams = initialParams || {};
        this.exitedFilesAbsPathes = absFilesPathes;
        return await this.action({
            changeOfFiles: absFilesPathes.map(fileAbsolutePath => ({
                fileAbsolutePath,
                eventName: 'change',
                datetime: new Date(),
            })),
            asyncEvent: false,
        });
    }
    lastAsyncFilesChanges = [];
    debounceAction = _.debounce(() => {
        const changeOfFiles = this.lastAsyncFilesChanges;
        this.lastAsyncFilesChanges = [];
        this.action({
            changeOfFiles,
            asyncEvent: true,
        });
    }, this.debounceTime);
    /**
     * @deprecated use action() instead
     */
    async asyncAction(asyncEvents, initialParams) {
        this.lastAsyncFilesChanges.push(asyncEvents);
        if (asyncEvents.eventName === 'unlink') {
            this.exitedFilesAbsPathes = this.exitedFilesAbsPathes.filter(f => f !== asyncEvents.fileAbsolutePath);
        }
        else if (asyncEvents.eventName === 'unlinkDir') {
            this.exitedFilesAbsPathes = this.exitedFilesAbsPathes.filter(f => !f.startsWith(asyncEvents.fileAbsolutePath.replace(/\/$/, '') + '/'));
        }
        else {
            this.exitedFilesAbsPathes.push(asyncEvents.fileAbsolutePath);
            this.exitedFilesAbsPathes = Utils.uniqArray(this.exitedFilesAbsPathes);
        }
        await this.debounceAction();
    }
}
//# sourceMappingURL=base-debounce-compiler-for-project.js.map