"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module IModelHost
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalhostIpcHost = void 0;
const ws = require("ws");
const core_common_1 = require("@itwin/core-common");
const IpcHost_1 = require("./IpcHost");
class LocalTransport extends core_common_1.IpcWebSocketTransport {
    _server;
    _connections = new Map();
    constructor(opts) {
        super();
        if (!opts.noServer) {
            this._server = new ws.Server({ port: opts.socketPort ?? 3002, host: opts.host ?? "127.0.0.1" });
            this._server.on("connection", (connection) => LocalhostIpcHost.connect(connection));
        }
    }
    send(message) {
        this._connections.forEach((last, connection) => {
            message.sequence = last + 1;
            this._connections.set(connection, message.sequence);
            const parts = this.serialize(message);
            parts.forEach((part) => connection.send(part));
        });
    }
    connect(connection) {
        this._connections.set(connection, -1);
        connection.on("message", async (data) => {
            const message = await this.notifyIncoming(data, connection);
            if (core_common_1.IpcWebSocketMessage.skip(message)) {
                return;
            }
            for (const listener of core_common_1.IpcWebSocket.receivers) {
                listener({}, message);
            }
        });
        connection.on("close", () => {
            this._connections.delete(connection);
            this.notifyClose(connection);
        });
    }
}
class RpcHandler extends IpcHost_1.IpcHandler {
    channelName = core_common_1.rpcOverIpcStrings.channelName;
    async request(info) {
        const invocation = core_common_1.RpcSessionInvocation.create(info);
        const fulfillment = await invocation.fulfillment;
        return invocation.rejected ? Promise.reject(fulfillment.rawResult) : fulfillment.rawResult; // eslint-disable-line @typescript-eslint/prefer-promise-reject-errors
    }
}
/** @internal */
class LocalhostIpcHost {
    static _initialized = false;
    static socket;
    static connect(connection) {
        core_common_1.IpcWebSocket.transport.connect(connection);
    }
    static async startup(opts) {
        let registerHandler = false;
        if (!this._initialized) {
            registerHandler = true;
            core_common_1.IpcWebSocket.transport = new LocalTransport(opts?.localhostIpcHost ?? {});
            this.socket = new core_common_1.IpcWebSocketBackend();
            this._initialized = true;
        }
        await IpcHost_1.IpcHost.startup({ ipcHost: { socket: this.socket }, iModelHost: opts?.iModelHost });
        if (registerHandler)
            RpcHandler.register();
    }
}
exports.LocalhostIpcHost = LocalhostIpcHost;
//# sourceMappingURL=LocalhostIpcHost.js.map