"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const definitions_1 = require("./definitions");
const rxjs_1 = require("rxjs");
const socket_io_client_1 = __importDefault(require("socket.io-client"));
class SocketIOProjectLinker {
    constructor() {
        this._config = {};
        this._config$ = new rxjs_1.Subject();
        this._projectInfo = new rxjs_1.Subject();
        this.errors = new rxjs_1.Subject();
        this.events = new rxjs_1.Subject();
        this.connected = new rxjs_1.BehaviorSubject(false);
        this.socket = socket_io_client_1.default('http://localhost:8888');
        this.socket.on('connect', () => {
            console.log('connected!');
            this.connected.next(true);
        });
        this.socket.on('disconnect', () => {
            console.log('disconnect');
            this.connected.next(false);
        });
        this.socket.on('config', (data) => {
            console.info('New config', data);
            this._config = data;
            this._config$.next(this._config);
        });
        this.socket.on('projectInfo', (data) => {
            console.info('New project info', data);
            this._projectInfo.next(data);
        });
        this.socket.on('error', (data) => {
            console.warn(data);
        });
        this.socket.on(definitions_1.ERROR_CHANNEL, (data) => {
            console.warn(definitions_1.ERROR_CHANNEL, data);
            this.errors.next(data);
        });
        this.socket.on(definitions_1.EVENT_CHANNEL, (data) => {
            this.events.next(data);
        });
    }
    hasConfig() {
        return this._config != undefined;
    }
    get projectInfoChange() {
        return this._projectInfo;
    }
    sync() {
        this.socket.emit('getConfig', 'all');
        this.socket.emit('getProjectInfo', 'all');
    }
    get config() {
        return this._config;
    }
    get configChange() {
        return this._config$.asObservable();
    }
    setAppConfig(data) {
        this._config.appConfig = data;
    }
    applyConfig() {
        console.info('setConfig', this._config);
        this.socket.emit('setConfig', this._config);
    }
    generate() {
        this.socket.emit('schematic', 'generate');
    }
    serve(port = 4202) {
        this.socket.emit('serve', {
            port: port
        });
    }
}
exports.SocketIOProjectLinker = SocketIOProjectLinker;
//# sourceMappingURL=socketio-project-linker.js.map