/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ImplementationError, Logger } from "#general";
import { Ble } from "#protocol";
import { CommissioningBehavior } from "../commissioning/CommissioningBehavior.js";
import { NetworkBehavior } from "./NetworkBehavior.js";
const logger = Logger.get("NetworkingServer");
class NetworkServer extends NetworkBehavior {
  initialize() {
    if (this.state.ble === void 0) {
      this.state.ble = Ble.enabled;
    } else if (this.state.ble && !Ble.enabled) {
      logger.warn("Disabling Bluetooth commissioning because BLE support is not installed");
      this.state.ble = false;
    }
    const discoveryCaps = this.state.discoveryCapabilities;
    switch (discoveryCaps.ble) {
      case void 0:
        discoveryCaps.ble = this.state.ble;
        break;
      case true:
        if (!this.state.ble) {
          discoveryCaps.ble = false;
        }
        break;
    }
    if (discoveryCaps.onIpNetwork === void 0) {
      discoveryCaps.onIpNetwork = true;
    }
    this.reactTo(this.agent.get(CommissioningBehavior).events.commissioned, this.#endUncommissionedMode);
    return super.initialize();
  }
  /**
   * Advertise and continue advertising at regular intervals until timeout per Matter specification.  If already
   * advertising, the advertisement timeout resets.
   *
   * If the node is uncommissioned it announces as commissionable on all available transports. Commissioned devices
   * only advertise for operational discovery via DNS-SD.
   *
   * Advertisement begins at startup.
   */
  openAdvertisementWindow() {
    if (!this.internal.runtime) {
      throw new ImplementationError("Cannot advertise offline server");
    }
    this.internal.runtime.openAdvertisementWindow().catch((error) => logger.error("Failed to open advertisement window", error));
  }
  /**
   * Immediately broadcast presence to the network regardless of whether the advertisement window is open.
   */
  advertiseNow() {
    if (!this.internal.runtime) {
      throw new ImplementationError("Cannot advertise offline server");
    }
    this.env.runtime.add(this.internal.runtime.advertiseNow());
  }
  #endUncommissionedMode() {
    if (this.internal.runtime) {
      this.internal.runtime.endUncommissionedMode();
    }
  }
}
((NetworkServer2) => {
  class Internal extends NetworkBehavior.Internal {
  }
  NetworkServer2.Internal = Internal;
  class State extends NetworkBehavior.State {
    listeningAddressIpv4 = void 0;
    listeningAddressIpv6 = void 0;
    ipv4 = true;
    ble = void 0;
    discoveryCapabilities = {
      onIpNetwork: true
    };
    subscriptionOptions = void 0;
  }
  NetworkServer2.State = State;
})(NetworkServer || (NetworkServer = {}));
export {
  NetworkServer
};
//# sourceMappingURL=NetworkServer.js.map
