import { Observable } from '@nativescript/core';
export class Server {
    constructor(config) {
        this.server = new io.nstudio.plugins.webserver.Server(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.getStatus();
    }
    setStatusChangeListener(value) {
        this.server.setStatusCallback(new io.nstudio.plugins.webserver.Server.StatusCallback({
            onChange(status) {
                value(status);
            },
        }));
    }
    start() {
        return new Promise((resolve, reject) => {
            this.server.start(new io.nstudio.plugins.webserver.Server.Callback({
                onSuccess() {
                    resolve();
                },
                onError(error) {
                    reject(error);
                },
            }));
        });
    }
    stop(wait = false) {
        return new Promise((resolve, reject) => {
            this.server.stop(wait, new io.nstudio.plugins.webserver.Server.Callback({
                onSuccess() {
                    resolve();
                },
                onError(error) {
                    reject(error);
                },
            }));
        });
    }
}
export class Client {
    get id() {
        return this._id;
    }
}
export class WebSocketServer extends Observable {
    constructor(config) {
        super();
        this.clients = new Map();
        this.server = new io.nstudio.plugins.webserver.websocket.Server(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);
        const ref = new WeakRef(this);
        this.server.addOnConnect(new io.nstudio.plugins.webserver.websocket.Server.ConnectCallback({
            onConnect(id) {
                const owner = ref.get?.();
                if (owner) {
                    const ret = new Client();
                    ret._id = id;
                    owner.clients.set(id, ret);
                    owner.notify({ eventName: 'connection', client: ret });
                }
            },
        }));
        const onMessage = new io.nstudio.plugins.webserver.websocket.Server.MessageCallback({
            onMessage(id, message) {
                const owner = ref.get?.();
                if (owner) {
                    let ret = null;
                    if (message.type === 0 /* NSCMessageType.Text */) {
                        ret = message.text;
                    }
                    else if (message.data instanceof NSData) {
                        ret = interop.bufferFromData(message.data);
                    }
                    owner.notify({ eventName: 'message', client: owner.clients.get(id), message: ret });
                }
            },
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            onPing(param0, param1) { },
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            onPong(param0, param1) { },
        });
        this.server.addOnMessage(onMessage);
        const onPing = new io.nstudio.plugins.webserver.websocket.Server.MessageCallback({
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            onMessage(id, message) { },
            onPing(id, message) {
                const owner = ref.get?.();
                if (owner) {
                    let ret = null;
                    if (message) {
                        ret = ArrayBuffer.from(message);
                    }
                    owner.notify({ eventName: 'ping', client: owner.clients.get(id), message: ret });
                }
            },
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            onPong(param0, param1) { },
        });
        this.server.addOnPing(onPing);
        const onPong = new io.nstudio.plugins.webserver.websocket.Server.MessageCallback({
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            onMessage(id, message) { },
            // eslint-disable-next-line @typescript-eslint/no-empty-function
            onPing(param0, param1) { },
            onPong(id, message) {
                const owner = ref.get?.();
                if (owner) {
                    let ret = null;
                    if (message) {
                        ret = ArrayBuffer.from(message);
                    }
                    owner.notify({ eventName: 'pong', client: this.clients.get(id), message: ret });
                }
            },
        });
        this.server.addOnPong(onPong);
        const onDisconnect = new io.nstudio.plugins.webserver.websocket.Server.DisconnectCallback({
            onDisconnect(id, code, description) {
                const owner = ref.get?.();
                if (owner) {
                    owner.notify({ eventName: 'close', client: owner.clients.get(id), code, description });
                    owner.clients.delete(id);
                }
            },
        });
        this.server.addOnDisconnect(onDisconnect);
    }
    get status() {
        return this.server.getStatus();
    }
    broadcast(data) {
        if (typeof data === 'string') {
            this.server.broadcast(data);
        }
        else if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.broadcast(data);
        }
    }
    send(id, data) {
        if (typeof data === 'string') {
            this.server.send(data, id);
        }
        else if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.send(data, id);
        }
    }
    ping(id, data = null) {
        if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.ping(data, id);
        }
        else {
            this.server.ping(null, id);
        }
    }
    pong(id, data = null) {
        if (data instanceof ArrayBuffer || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
            this.server.pong(data, id);
        }
        else {
            this.server.pong(null, id);
        }
    }
    setStatusChangeListener(value) {
        this.server.setStatusCallback(new io.nstudio.plugins.webserver.websocket.Server.StatusCallback({
            onChange(status) {
                value(status);
            },
        }));
    }
    start() {
        return new Promise((resolve, reject) => {
            this.server.start(new io.nstudio.plugins.webserver.websocket.Server.Callback({
                onSuccess() {
                    resolve();
                },
                onError(error) {
                    reject(error);
                },
            }));
        });
    }
    stop(wait = false) {
        return new Promise((resolve, reject) => {
            this.server.stop(wait, new io.nstudio.plugins.webserver.websocket.Server.Callback({
                onSuccess() {
                    resolve();
                },
                onError(error) {
                    reject(error);
                },
            }));
        });
    }
}
//# sourceMappingURL=index.android.js.map