/*! Copyright 2026 Fonticons, Inc. - https://webawesome.com/license */
import { expect } from "@open-wc/testing";
async function expectEvent(el, events, action, options = {}) {
  const eventNames = Array.isArray(events) ? events : [events];
  const { timeout = 1e3, count = 1 } = options;
  const collected = /* @__PURE__ */ new Map();
  const listeners = [];
  for (const name of eventNames) {
    collected.set(name, []);
    const handler = (event) => {
      collected.get(name).push(event);
    };
    listeners.push([name, handler]);
    el.addEventListener(name, handler);
  }
  try {
    await action();
    const deadline = Date.now() + timeout;
    while (Date.now() < deadline) {
      const allReceived = eventNames.every((name) => collected.get(name).length >= count);
      if (allReceived) break;
      await new Promise((resolve) => setTimeout(resolve, 10));
    }
    for (const name of eventNames) {
      const received = collected.get(name);
      expect(received.length).to.equal(
        count,
        `Expected event "${name}" to fire ${count} time(s), but it fired ${received.length} time(s)`
      );
    }
    const allEvents = [];
    for (const name of eventNames) {
      allEvents.push(...collected.get(name));
    }
    return allEvents;
  } finally {
    for (const [name, handler] of listeners) {
      el.removeEventListener(name, handler);
    }
  }
}
export {
  expectEvent
};
