"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 ServiceDiscovery_exports = {};
__export(ServiceDiscovery_exports, {
  discoverNames: () => discoverNames
});
module.exports = __toCommonJS(ServiceDiscovery_exports);
var import_Observable = require("#util/Observable.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function discoverNames(names, suffix, signal) {
  const normalizedSuffix = `.${suffix.toLowerCase().replace(/\.local$/, "")}.local`;
  const exactType = suffix.toLowerCase().replace(/\.local$/, "") + ".local";
  const filter = (record) => {
    const lower = record.name.toLowerCase();
    return lower === exactType || lower.endsWith(normalizedSuffix);
  };
  names.filters.add(filter, [exactType]);
  const observers = new import_Observable.ObserverGroup();
  const queue = [];
  let waiting;
  let done = false;
  const push = (name) => {
    if (done) return;
    const lower = name.qname.toLowerCase();
    if (lower.endsWith(normalizedSuffix)) {
      if (waiting) {
        const resolve = waiting;
        waiting = void 0;
        resolve({ value: name, done: false });
      } else {
        queue.push(name);
      }
    }
  };
  const cleanup = () => {
    if (done) return;
    done = true;
    observers.close();
    names.filters.delete(filter);
    signal.removeEventListener("abort", onAbort);
    if (waiting) {
      const resolve = waiting;
      waiting = void 0;
      resolve({ value: void 0, done: true });
    }
  };
  const onAbort = () => cleanup();
  signal.addEventListener("abort", onAbort, { once: true });
  observers.on(names.discovered, push);
  for (const name of names.discoveredNames) {
    push(name);
  }
  if (signal.aborted) {
    cleanup();
  }
  const iterator = {
    next() {
      if (done) {
        return Promise.resolve({ value: void 0, done: true });
      }
      if (queue.length > 0) {
        return Promise.resolve({ value: queue.shift(), done: false });
      }
      return new Promise((resolve) => {
        waiting = resolve;
      });
    },
    return() {
      cleanup();
      return Promise.resolve({ value: void 0, done: true });
    },
    [Symbol.asyncIterator]() {
      return iterator;
    }
  };
  return iterator;
}
//# sourceMappingURL=ServiceDiscovery.js.map
