"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var MockTcpListener_exports = {};
__export(MockTcpListener_exports, {
  MockTcpListener: () => MockTcpListener
});
module.exports = __toCommonJS(MockTcpListener_exports);
var import_MockTcpConnection = require("./MockTcpConnection.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class MockTcpListener {
  port;
  #host;
  #connectionListeners = /* @__PURE__ */ new Set();
  constructor(host, options) {
    this.port = options.listeningPort ?? 1024 + Math.floor(Math.random() * 64511);
    this.#host = host;
    host.registerTcpListener(this);
  }
  /**
   * Called by {@link MockNetwork.connectTcp} when a client connects.
   * Creates a socket pair, notifies connection listeners with the server-side
   * socket, and returns the client-side socket.
   */
  accept(clientAddress, clientPort) {
    const [clientSocket, serverSocket] = import_MockTcpConnection.MockTcpConnection.createPair(
      clientAddress,
      clientPort,
      this.#host.defaultRoute,
      this.port
    );
    for (const listener of this.#connectionListeners) {
      listener(serverSocket);
    }
    return clientSocket;
  }
  onConnection(listener) {
    this.#connectionListeners.add(listener);
    return {
      close: async () => {
        this.#connectionListeners.delete(listener);
      }
    };
  }
  async close() {
    this.#host.unregisterTcpListener(this.port);
  }
}
//# sourceMappingURL=MockTcpListener.js.map
