import { EventEmitter } from '@kano/common/index.js';
export class CreatorDevTools {
    constructor() {
        this._onDidChangeFile = new EventEmitter();
        this._onDidUpdateFiles = new EventEmitter();
        this._onDidConnectionStatusChange = new EventEmitter();
    }
    get onDidChangeFile() { return this._onDidChangeFile.event; }
    get onDidUpdateFiles() { return this._onDidUpdateFiles.event; }
    get onDidConnectionStatusChange() { return this._onDidConnectionStatusChange.event; }
    connect() {
        return this.loadScript()
            .then(() => {
            this.client = window.io.connect('http://localhost:4113');
            this.client.on('files', (paths) => {
                this._onDidUpdateFiles.fire(paths);
            });
            this.client.on('change', (p) => {
                const data = JSON.parse(p);
                this._onDidChangeFile.fire(data);
            });
            this.client.emit('get-files');
            this.client.on('connect', () => this.onConnect());
            this.client.on('reconnect', () => this.onConnect());
            this.client.on('disconnect', () => this.onDisconnect());
            this.client.on('error', () => this.onDisconnect());
        });
    }
    onConnect() {
        this._onDidConnectionStatusChange.fire(true);
    }
    onDisconnect() {
        this._onDidConnectionStatusChange.fire(false);
    }
    loadScript() {
        return new Promise((res, rej) => {
            const script = document.createElement('script');
            script.src = 'http://localhost:4113/socket.io/socket.io.js';
            script.onload = res;
            script.onerror = rej;
            document.head.appendChild(script);
        });
    }
    openFile(path) {
        if (!this.client) {
            return;
        }
        this.client.emit('open-file', path);
    }
}
//# sourceMappingURL=dev.js.map