"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 UdpTransport_exports = {};
__export(UdpTransport_exports, {
  UdpChannel: () => UdpChannel,
  UdpTransport: () => UdpTransport
});
module.exports = __toCommonJS(UdpTransport_exports);
var import_Channel = require("#net/Channel.js");
var import_Network = require("#net/Network.js");
var import_util = require("#util/index.js");
var import_ServerAddress = require("../ServerAddress.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class UdpTransport {
  #server;
  static async create(network, type, port, host, netInterface) {
    return new UdpTransport(
      await network.createUdpSocket({ listeningPort: port, type, netInterface, listeningAddress: host })
    );
  }
  constructor(server) {
    this.#server = server;
  }
  get server() {
    return this.#server;
  }
  supports(type, address) {
    return this.#server.supports(type, address);
  }
  async openChannel(address) {
    if (!import_ServerAddress.ServerAddress.isIp(address)) {
      throw new import_Network.NetworkError(`Unsupported address type for UDP interface`);
    }
    const { ip, port } = address;
    return Promise.resolve(new UdpChannel(this.#server, ip, port));
  }
  onData(listener) {
    return this.#server.onData((_netInterface, peerHost, peerPort, data) => {
      listener(new UdpChannel(this.#server, peerHost, peerPort), data);
    });
  }
  get port() {
    return this.#server.port;
  }
  close() {
    return this.#server.close();
  }
  addMembership(address) {
    return this.#server.addMembership(address);
  }
  dropMembership(address) {
    return this.#server.dropMembership(address);
  }
}
class UdpChannel {
  isReliable = false;
  supportsLargeMessages = false;
  type = import_Channel.ChannelType.UDP;
  #server;
  #peerAddress;
  #peerPort;
  networkAddressChanged = (0, import_util.Observable)();
  constructor(server, peerAddress, peerPort) {
    this.#server = server;
    this.#peerAddress = peerAddress;
    this.#peerPort = peerPort;
  }
  get maxPayloadSize() {
    return this.#server.maxPayloadSize;
  }
  send(data, addressOverride) {
    if (addressOverride) {
      return this.#server.send(addressOverride.ip, addressOverride.port, data);
    }
    return this.#server.send(this.#peerAddress, this.#peerPort, data);
  }
  get name() {
    return `${this.type}://${this.#peerAddress.includes(":") ? `[${this.#peerAddress}]` : this.#peerAddress}:${this.#peerPort}`;
  }
  get networkAddress() {
    return { type: "udp", ip: this.#peerAddress, port: this.#peerPort };
  }
  set networkAddress(address) {
    if (address.type !== "udp" || address.ip === this.#peerAddress && address.port === this.#peerPort) {
      return;
    }
    this.#peerAddress = address.ip;
    this.#peerPort = address.port;
    this.networkAddressChanged.emit(this.networkAddress);
  }
  async close() {
  }
}
//# sourceMappingURL=UdpTransport.js.map
