"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 ActiveDiscoveries_exports = {};
__export(ActiveDiscoveries_exports, {
  ActiveDiscoveries: () => ActiveDiscoveries
});
module.exports = __toCommonJS(ActiveDiscoveries_exports);
var import_general = require("#general");
var import_protocol = require("#protocol");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ActiveDiscoveries extends Set {
  #env;
  #mdnsTargetCriteria;
  constructor(env) {
    super();
    this.#env = env;
    this.#activateCommissionableScanning();
    env.get(import_protocol.ScannerSet).added.on(this.#activateCommissionableScanningFor.bind(this));
  }
  static [import_general.Environmental.create](env) {
    const instance = new ActiveDiscoveries(env);
    env.set(ActiveDiscoveries, instance);
    return instance;
  }
  async close() {
    this.#env.delete(ActiveDiscoveries, this);
    this.#deactivateCommissionableScanning();
  }
  #activateCommissionableScanning() {
    const scanners = this.#env.get(import_protocol.ScannerSet);
    for (const scanner of scanners) {
      this.#activateCommissionableScanningFor(scanner);
    }
  }
  #activateCommissionableScanningFor(scanner) {
    if (!(scanner instanceof import_protocol.MdnsClient)) {
      return;
    }
    if (this.#mdnsTargetCriteria === void 0) {
      this.#mdnsTargetCriteria = { commissionable: true, operationalTargets: [] };
    }
    scanner.targetCriteriaProviders.add(this.#mdnsTargetCriteria);
  }
  #deactivateCommissionableScanning() {
    if (!this.#mdnsTargetCriteria) {
      return;
    }
    const scanners = this.#env.get(import_protocol.ScannerSet);
    for (const scanner of scanners) {
      if (scanner instanceof import_protocol.MdnsClient) {
        scanner.targetCriteriaProviders.delete(this.#mdnsTargetCriteria);
      }
    }
  }
}
//# sourceMappingURL=ActiveDiscoveries.js.map
