import { Observable } from '@nativescript/core';
export class Server {
    constructor(config) {
        this.server = NSCServer.alloc().init(config.logger ?? false, config.path, config.directory, config.index ?? null, config.hostName ?? '127.0.0.1', config.port ?? 8080, config.workers ?? 2, config.showFiles ?? false);
    }
    get status() {
        return this.server.status;
    }
    setStatusChangeListener(value) {
        this.server.setStatusChangeCallback((status) => {
            value(status);
        });
    }
    start() {
        return new Promise((resolve, reject) => {
            this.server.start((error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    stop(wait = false) {
        return new Promise((resolve, reject) => {
            this.server.stop(wait, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
}
export class Client {
    get id() {
        return this._native.id;
    }
}
export class WebSocketServer extends Observable {
    constructor(config) {
        super();
        this.clients = new Map();
        this.server = NSCWebSocketServer.alloc().init(config.logger ?? false, config.path, config.maxPayload ?? 1024 * 1024 * 100, config.hostName ?? '127.0.0.1', config.port ?? 8081, config.workers ?? 2, config.autoPong ?? true);
        this.server.addOnConnect((client) => {
            const ret = new Client();
            ret._native = client;
            this.clients.set(client.id, ret);
            this.notify({ eventName: 'connection', client: ret });
        });
        this.server.addOnMessage((client, message) => {
            let ret = null;
            if (message.type === 0 /* NSCMessageType.Text */) {
                ret = message.text;
            }
            else if (message.data instanceof NSData) {
                ret = interop.bufferFromData(message.data);
            }
            this.notify({ eventName: 'message', client: this.clients.get(client.id), message: ret });
        });
        this.server.addOnPing((client, message) => {
            let ret = null;
            if (message) {
                ret = interop.bufferFromData(message);
            }
            this.notify({ eventName: 'ping', client: this.clients.get(client.id), message: ret });
        });
        this.server.addOnPong((client, message) => {
            let ret = null;
            if (message) {
                ret = interop.bufferFromData(message);
            }
            this.notify({ eventName: 'pong', client: this.clients.get(client.id), message: ret });
        });
        this.server.addOnDisconnect((client, code, description) => {
            this.notify({ eventName: 'close', client: this.clients.get(client.id), code, description });
            this.clients.delete(client.id);
        });
    }
    get status() {
        return this.server.status;
    }
    broadcast(data) {
        if (typeof data === 'string') {
            this.server.broadcast(data);
        }
        else if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.broadcastWithBinary(NSData.dataWithData(data));
        }
    }
    send(id, data) {
        if (typeof data === 'string') {
            this.server.sendWithTextId(data, id);
        }
        else if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.sendWithBinaryId(NSData.dataWithData(data), id);
        }
    }
    ping(id, data = null) {
        if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.sendWithPingId(NSData.dataWithData(data), id);
        }
        else {
            this.server.sendEmptyPingWithId(id);
        }
    }
    pong(id, data = null) {
        if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.sendWithPongId(NSData.dataWithData(data), id);
        }
        else {
            this.server.sendEmptyPongWithId(id);
        }
    }
    setStatusChangeListener(value) {
        this.server.setStatusChangeCallback((status) => {
            value(status);
        });
    }
    start() {
        return new Promise((resolve, reject) => {
            this.server.start((error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    stop(wait = false) {
        return new Promise((resolve, reject) => {
            this.server.stop(wait, (error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
}
//# sourceMappingURL=index.ios.js.map