"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 NetworkServer_exports = {};
__export(NetworkServer_exports, {
  NetworkServer: () => NetworkServer
});
module.exports = __toCommonJS(NetworkServer_exports);
var import_general = require("#general");
var import_protocol = require("#protocol");
var import_CommissioningBehavior = require("../commissioning/CommissioningBehavior.js");
var import_NetworkBehavior = require("./NetworkBehavior.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("NetworkingServer");
class NetworkServer extends import_NetworkBehavior.NetworkBehavior {
  initialize() {
    if (this.state.ble === void 0) {
      this.state.ble = import_protocol.Ble.enabled;
    } else if (this.state.ble && !import_protocol.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(import_CommissioningBehavior.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 import_general.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 import_general.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 import_NetworkBehavior.NetworkBehavior.Internal {
  }
  NetworkServer2.Internal = Internal;
  class State extends import_NetworkBehavior.NetworkBehavior.State {
    listeningAddressIpv4 = void 0;
    listeningAddressIpv6 = void 0;
    ipv4 = true;
    ble = void 0;
    discoveryCapabilities = {
      onIpNetwork: true
    };
    subscriptionOptions = void 0;
  }
  NetworkServer2.State = State;
})(NetworkServer || (NetworkServer = {}));
//# sourceMappingURL=NetworkServer.js.map
