"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LightWeightWatcher = void 0;
const rxjs_1 = require("rxjs");
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const lib_3 = require("tnp-helpers/lib");
const constants_1 = require("../../constants");
// @ts-ignore TODO weird inheritance problem
class LightWeightWatcher extends lib_3.BaseFeatureForProject {
    //#region fields
    codeWatchers = new Map();
    rebuildWatchers = new Map();
    //#endregion
    //#region public methods
    //#region public methods / is taon light weight mode allowed
    get isTaonLightWatcherMode() {
        //#region @backendFunc
        if (lib_1._.isBoolean(global.oldBuildModeInArgs)) {
            return !global.oldBuildModeInArgs;
        }
        return true;
        //#endregion
    }
    //#endregion
    //#region public methods / trigger rebuild of
    triggerRebuildOf(buildType) {
        //#region @backendFunc
        if (!buildType ||
            !lib_1.CoreModels.BuildWatcherTypeArr.includes(lib_1.CoreModels.buildTypeToWatcherFn(buildType))) {
            lib_3.Helpers.warn(`[triggerRebuildOf] Wrong trigger "${buildType}"`);
            return;
        }
        lib_3.Helpers.logInfo(`TRIGGER REBUILD ${buildType}`);
        this.rebuildTriggerWatcher(buildType).next({});
        //#endregion
    }
    //#endregion
    //#region public methods / trigger rebuild of
    cancelAndSetAsReadyForRebuildTrigger(buildType) {
        //#region @backendFunc
        if (!buildType || !lib_1.CoreModels.BuildTypeArr.includes(buildType)) {
            lib_3.Helpers.warn(`[cancelAndSetAsReadyForRebuildTrigger] Wrong trigger "${buildType}"`);
            return;
        }
        // TODO @LAST
        //#endregion
    }
    //#endregion
    //#region public methods / get rebuild trigger watcher
    rebuildTriggerWatcher(buildType) {
        //#region @backendFunc
        if (!this.isTaonLightWatcherMode) {
            return;
        }
        if (!this.rebuildWatchers.has(buildType)) {
            const subject = new rxjs_1.BehaviorSubject({});
            this.rebuildWatchers.set(buildType, subject);
        }
        return this.rebuildWatchers.get(buildType);
        //#endregion
    }
    //#endregion
    //#region public methods / get ts code obserable for
    getTsCodeObservableFor(buildType) {
        if (buildType === 'backend-watcher') {
            return this.tmpSourceRebuildForBackendObs;
        }
        if (buildType === 'browser-watcher') {
            return this.tmpSourceRebuildForBrowserObs;
        }
        if (buildType === 'websql-watcher') {
            return this.tmpSourceRebuildForWebsqlObs;
        }
    }
    //#endregion
    //#endregion
    //#region private methods
    //#region private methods / get watcher for
    /**
     * Get source code watcher obserable watcher
     */
    getWatcherFor(folders, watcherType) {
        //#region @backendFunc
        if (this.codeWatchers.has(watcherType)) {
            return this.codeWatchers.get(watcherType).asObservable();
        }
        const watcher = new rxjs_1.Subject();
        this.codeWatchers.set(watcherType, watcher);
        // const isomorphic = this.project.nodeModules.getIsomorphicPackagesNames();
        // console.log({ isomorphic });
        const pathsToWatch = [
            ...folders,
            // ...isomorphic.map(p =>
            //   this.project.nodeModules.pathFor([p, packageJsonNpmLib]),
            // ),
        ];
        // console.log('Watching for changes in:', pathsToWatch);
        lib_1.chokidar
            .watch(pathsToWatch, {
            ignoreInitial: true,
        })
            .on('all', async (eventWatcher, pathToFile) => {
            const relative = (0, lib_2.crossPlatformPath)(pathToFile).replace(this.project.location + '/' + lib_2.path.basename(folders[0]) + '/', '');
            if (watcherType === 'backend-watcher') {
                if (!relative.startsWith(`${constants_1.assetsFromTempSrc}/`)) {
                    // console.log('WATCHER CHANGE', {
                    //   eventWatcher,
                    //   pathToFile,
                    //   watcherType,
                    // });
                    watcher.next({});
                }
            }
            else {
                if (!relative.startsWith(`${constants_1.assetsFromTempSrc}/`) &&
                    !relative.startsWith(`app/`) &&
                    !relative.startsWith(`app.`)) {
                    // console.log('WATCHER CHANGE', {
                    //   eventWatcher,
                    //   pathToFile,
                    //   watcherType,
                    // });
                    watcher.next({});
                }
            }
        });
        return watcher.asObservable();
        //#endregion
    }
    //#endregion
    //#region private methods / tmp srouce obserable
    get tmpSourceRebuildForBackendObs() {
        const key = 'tmpSourceRebuildForBackendObs';
        if (this.project.cache[key]) {
            return this.project.cache[key];
        }
        const watcher = this.getWatcherFor([this.project.pathFor(constants_1.tmpSourceDist)], 'backend-watcher');
        this.project.cache[key] = watcher;
        return this.project.cache[key];
    }
    get tmpSourceRebuildForBrowserObs() {
        const key = 'tmpSourceRebuildForBrowserObs';
        if (this.project.cache[key]) {
            return this.project.cache[key];
        }
        const watcher = this.getWatcherFor([this.project.pathFor(constants_1.tmpSrcDist)], 'browser-watcher');
        this.project.cache[key] = watcher;
        return this.project.cache[key];
    }
    get tmpSourceRebuildForWebsqlObs() {
        const key = 'tmpSourceRebuildForWebsqlObs';
        if (this.project.cache[key]) {
            return this.project.cache[key];
        }
        const watcher = this.getWatcherFor([this.project.pathFor(constants_1.tmpSrcDistWebsql)], 'websql-watcher');
        this.project.cache[key] = watcher;
        return this.project.cache[key];
    }
}
exports.LightWeightWatcher = LightWeightWatcher;
//# sourceMappingURL=lightweight-watcher.js.map