"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 ServerAddressSet_exports = {};
__export(ServerAddressSet_exports, {
  ServerAddressSet: () => ServerAddressSet
});
module.exports = __toCommonJS(ServerAddressSet_exports);
var import_ServerAddress = require("./ServerAddress.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function ServerAddressSet(initial, comparator = ServerAddressSet.compareDesirability) {
  let version = 0;
  let addresses;
  const set = {
    add(address) {
      version++;
      const key = import_ServerAddress.ServerAddress.urlFor(address);
      const existing = addresses?.get(key);
      if (existing) {
        return existing;
      }
      if (!addresses) {
        addresses = /* @__PURE__ */ new Map();
      }
      addresses.set(key, address);
      return address;
    },
    delete(address) {
      version++;
      return addresses?.delete(import_ServerAddress.ServerAddress.urlFor(address)) ?? false;
    },
    has(address) {
      return addresses?.has(import_ServerAddress.ServerAddress.urlFor(address)) ?? false;
    },
    replace(newAddresses) {
      version++;
      const oldAddresses = addresses;
      addresses = /* @__PURE__ */ new Map();
      for (const address of newAddresses) {
        const key = import_ServerAddress.ServerAddress.urlFor(address);
        addresses.set(key, oldAddresses?.get(key) ?? address);
      }
    },
    get size() {
      return addresses?.size ?? 0;
    },
    *[Symbol.iterator]() {
      const tried = /* @__PURE__ */ new Set();
      all: while (true) {
        const currentVersion = version;
        const ordered = addresses ? [...addresses.entries()].map(([, address]) => address).sort(comparator) : [];
        for (const address of ordered) {
          const key = import_ServerAddress.ServerAddress.urlFor(address);
          if (tried.has(key)) {
            continue;
          }
          yield address;
          if (currentVersion !== version) {
            continue all;
          }
        }
        break;
      }
    }
  };
  if (initial) {
    set.replace(initial);
  }
  return set;
}
((ServerAddressSet2) => {
  function copyHealth(targetAddresses, sourceAddresses) {
    const output = [...targetAddresses];
    nextTarget: for (let i = 0; i < output.length; i++) {
      const target = output[i];
      for (const source of sourceAddresses) {
        if (import_ServerAddress.ServerAddress.isEqual(target, source)) {
          if (source.healthyAt !== void 0 || source.unhealthyAt !== void 0) {
            output[i] = { ...target, healthyAt: source.healthyAt, unhealthyAt: source.unhealthyAt };
          }
          continue nextTarget;
        }
      }
    }
    return output;
  }
  ServerAddressSet2.copyHealth = copyHealth;
  function compareDesirability(a, b) {
    const value = import_ServerAddress.ServerAddress.selectionPreferenceOf(a) - import_ServerAddress.ServerAddress.selectionPreferenceOf(b);
    if (value) {
      return value;
    }
    const relativeHealth = compareHealth(a, b);
    if (relativeHealth) {
      return relativeHealth;
    }
    if (a.priority !== void 0 && b.priority !== void 0) {
      return b.priority - a.priority;
    }
    return 0;
  }
  ServerAddressSet2.compareDesirability = compareDesirability;
  function compareHealth(a, b) {
    const ha = import_ServerAddress.ServerAddress.healthOf(a);
    const hb = import_ServerAddress.ServerAddress.healthOf(b);
    if (ha.unhealthyAt) {
      if (hb.unhealthyAt) {
        return ha.unhealthyAt - hb.unhealthyAt;
      }
      return 1;
    }
    if (hb.unhealthyAt) {
      return -1;
    }
    if (ha.healthyAt) {
      if (hb.healthyAt) {
        return hb.healthyAt - ha.healthyAt;
      }
      return -1;
    }
    return 0;
  }
  ServerAddressSet2.compareHealth = compareHealth;
})(ServerAddressSet || (ServerAddressSet = {}));
//# sourceMappingURL=ServerAddressSet.js.map
