"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const server_1 = require("../../server");
class HotNodeServer {
    // tslint:disable-next-line
    constructor(configs) {
        this.configs = configs;
        this.clientCompiling = 'NOT-STARTED';
        this.start = () => {
            this.configs.clientCompiler.hooks.compile.tap('HotHodeServer', () => {
                this.clientCompiling = 'COMPILING';
            });
            this.configs.clientCompiler.hooks.done.tap('HotHodeServer', (stats) => {
                if (!stats.hasErrors()) {
                    this.clientCompiling = 'DONE';
                }
            });
            this.waitForClientThenStartServer();
        };
        this.dispose = () => __awaiter(this, void 0, void 0, function* () {
            if (this.server) {
                this.server.close();
            }
        });
        this.startServer = () => __awaiter(this, void 0, void 0, function* () {
            if (this.server) {
                this.server.close();
                this.server = null;
                cli_core_1.Utils.logger.log({
                    label: `@bluebase/cli/server`,
                    level: 'info',
                    message: 'Restarting server...',
                });
            }
            const newServer = server_1.server({
                assetsDirPath: this.configs.assetsDirPath,
                client: this.configs.clientConfigs,
                server: this.configs.serverConfigs,
            });
            newServer.on('listening', () => {
                cli_core_1.Utils.logger.log({
                    label: `@bluebase/cli/server`,
                    level: 'info',
                    message: 'Server running with latest changes.',
                    notify: true,
                });
            });
            newServer.on('error', (err) => {
                cli_core_1.Utils.logger.log({
                    err,
                    label: `@bluebase/cli/server`,
                    level: 'error',
                    message: 'Error in server execution, check the console for more info.',
                });
            });
            this.server = newServer;
        });
        // We want our node server bundles to only start after a successful client
        // build.  This avoids any issues with node server bundles depending on
        // client bundle assets.
        this.waitForClientThenStartServer = () => {
            if (this.clientCompiling !== 'DONE') {
                setTimeout(this.waitForClientThenStartServer, 50);
            }
            else {
                this.startServer();
            }
        };
    }
}
exports.HotNodeServer = HotNodeServer;
//# sourceMappingURL=HotNodeServer.js.map