var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __using = (stack, value, async) => {
  if (value != null) {
    if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
    var dispose, inner;
    if (async) dispose = value[__knownSymbol("asyncDispose")];
    if (dispose === void 0) {
      dispose = value[__knownSymbol("dispose")];
      if (async) inner = dispose;
    }
    if (typeof dispose !== "function") __typeError("Object not disposable");
    if (inner) dispose = function() {
      try {
        inner.call(this);
      } catch (e) {
        return Promise.reject(e);
      }
    };
    stack.push([async, dispose, value]);
  } else if (async) {
    stack.push([async]);
  }
  return value;
};
var __callDispose = (stack, error, hasError) => {
  var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
    return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
  };
  var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
  var next = (it) => {
    while (it = stack.pop()) {
      try {
        var result = it[1] && it[1].call(it[2]);
        if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
      } catch (e) {
        fail(e);
      }
    }
    if (hasError) throw error;
  };
  return next();
};
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Lifetime } from "@matter/general";
import { Advertisement } from "./Advertisement.js";
class Advertiser {
  #lifetime;
  #advertisements = /* @__PURE__ */ new Set();
  #isClosed = false;
  constructor(lifetime = Lifetime.process) {
    this.#lifetime = lifetime.join("advertiser");
  }
  /**
   * Begin advertising on configured schedule.
   *
   * Note that so long as the advertisement is installed the device will continue to respond to MDNS queries even if
   * the advertisement schedule is elapsed.
   *
   * Returns undefined if the advertiser does not support this type of advertisement.
   */
  advertise(description, event) {
    if (this.#isClosed) {
      return;
    }
    const ad = this.getAdvertisement(description);
    if (ad) {
      ad.start(event);
    }
    return ad;
  }
  /**
   * Destroy the instance.
   */
  async close() {
    var _stack = [];
    try {
      const _closing = __using(_stack, this.#lifetime.closing());
      this.#isClosed = true;
      await Advertisement.closeAll(this.#advertisements);
    } catch (_) {
      var _error = _, _hasError = true;
    } finally {
      __callDispose(_stack, _error, _hasError);
    }
  }
  /**
   * The set of advertisements active for this advertiser.
   */
  get advertisements() {
    return this.#advertisements;
  }
  /**
   * Obtain the active advertisement for {@link fabric}, if any.
   */
  advertisementForFabric(fabric) {
    for (const ad of this.#advertisements) {
      if (ad.isOperational() && ad.description.fabric === fabric) {
        return ad;
      }
    }
  }
  /**
   * Select advertisements using a predicate.
   */
  filter(predicate) {
    return [...this.#advertisements].filter(predicate);
  }
  join(...name) {
    return this.#lifetime.join(...name);
  }
}
export {
  Advertiser
};
//# sourceMappingURL=Advertiser.js.map
