"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDebounceCompilerForProject = void 0;
const lib_1 = require("tnp-core/lib");
const base_compiler_for_project_1 = require("./base-compiler-for-project");
class BaseDebounceCompilerForProject extends base_compiler_for_project_1.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 = lib_1._.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 = lib_1.Utils.uniqArray(this.exitedFilesAbsPathes);
        }
        await this.debounceAction();
    }
}
exports.BaseDebounceCompilerForProject = BaseDebounceCompilerForProject;
//# sourceMappingURL=base-debounce-compiler-for-project.js.map