"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const ListenerManager_1 = __importDefault(require("./ListenerManager"));
const webpack_serve_1 = __importDefault(require("webpack-serve"));
class HotClientServer {
    // tslint:disable-next-line
    constructor(configs) {
        this.configs = configs;
    }
    start() {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield webpack_serve_1.default({}, {
                compiler: this.configs.clientCompiler,
                // content: Utils.fromProjectRoot('./build/electron'),
                devMiddleware: {
                    publicPath: '/',
                    writeToDisk: true,
                },
                clipboard: false,
                logLevel: 'warn',
                // add: (app, _middleware, options) => {
                // 	// Be sure to pass the options argument from the arguments
                // 	app.use(webpackServeWaitpage(options, { theme: 'material' }));
                // 	// Make sure the usage of webpack-serve-waitpage will be before the following commands if exists
                // 	// middleware.webpack();
                // 	// middleware.content();
                // },
                on: {
                    'build-started': () => {
                        cli_core_1.Utils.logger.log({
                            label: '@bluebase/cli/client',
                            level: 'info',
                            message: 'Build Started',
                        });
                    },
                    'build-finished': () => {
                        cli_core_1.Utils.logger.log({
                            label: '@bluebase/cli/client',
                            level: 'info',
                            message: 'Running with latest changes.',
                            notify: true,
                        });
                    },
                    'compiler-error': () => {
                        cli_core_1.Utils.logger.log({
                            label: '@bluebase/cli/client',
                            level: 'error',
                            message: 'Build failed, please check the console for more information.',
                            notify: true,
                        });
                    },
                }
            });
            this.app = result.app;
            this.listenerManager = new ListenerManager_1.default(this.app, 'client');
            result.app.on('connection', () => {
                // tslint:disable-next-line:no-console
                console.log('connected');
            });
        });
    }
    dispose() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.app) {
                this.app.stop();
            }
            if (this.listenerManager) {
                yield this.listenerManager.dispose();
            }
        });
    }
}
exports.HotClientServer = HotClientServer;
//# sourceMappingURL=HotClientServer.js.map