"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 ServerAddress_exports = {};
__export(ServerAddress_exports, {
  ServerAddress: () => ServerAddress
});
module.exports = __toCommonJS(ServerAddress_exports);
var import_Diagnostic = require("#log/Diagnostic.js");
var import_Duration = require("#time/Duration.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function ServerAddress(definition) {
  return {
    ttl: void 0,
    discoveredAt: void 0,
    healthyAt: void 0,
    unhealthyAt: void 0,
    priority: void 0,
    weight: void 0,
    ...definition
  };
}
((ServerAddress2) => {
  function urlFor(address) {
    switch (address.type) {
      case "udp":
      case "tcp":
        const ip = address.ip;
        return `${address.type}://${ip.includes(":") ? `[${ip}]` : ip}:${address.port}`;
      case "ble":
        return `ble://${address.peripheralAddress}`;
      default:
        return `${address.type}://`;
    }
  }
  ServerAddress2.urlFor = urlFor;
  function diagnosticFor(address) {
    const diagnostic = Array();
    switch (address.type) {
      case "udp":
      case "tcp":
        diagnostic.push(`${address.type}://`, import_Diagnostic.Diagnostic.strong(address.ip), ":", address.port);
        break;
      case "ble":
        diagnostic.push("ble://", import_Diagnostic.Diagnostic.strong(address.peripheralAddress));
        break;
      default:
        diagnostic.push(`${address.type}://`);
        break;
    }
    if ("ttl" in address && typeof address.ttl === "number") {
      diagnostic.push(" ttl ", import_Duration.Duration.format(address.ttl));
    }
    return import_Diagnostic.Diagnostic.squash(...diagnostic);
  }
  ServerAddress2.diagnosticFor = diagnosticFor;
  function isEqual(a, b) {
    if (a.type !== b.type) {
      return false;
    }
    if (a.type === "udp" && b.type === "udp") {
      return a.ip === b.ip && a.port === b.port;
    }
    if (a.type === "ble" && b.type === "ble") {
      return a.peripheralAddress === b.peripheralAddress;
    }
    return false;
  }
  ServerAddress2.isEqual = isEqual;
  function healthOf(health) {
    if (health.unhealthyAt === void 0) {
      return health;
    }
    if (health.healthyAt !== void 0 && health.healthyAt > health.unhealthyAt) {
      return {
        healthyAt: health.healthyAt
      };
    }
    return health;
  }
  ServerAddress2.healthOf = healthOf;
  let SelectionPreference;
  ((SelectionPreference2) => {
    SelectionPreference2[SelectionPreference2["IPV6_ULA"] = 0] = "IPV6_ULA";
    SelectionPreference2[SelectionPreference2["IPV6_LINK_LOCAL"] = 1] = "IPV6_LINK_LOCAL";
    SelectionPreference2[SelectionPreference2["IPV6"] = 2] = "IPV6";
    SelectionPreference2[SelectionPreference2["IPV4"] = 3] = "IPV4";
    SelectionPreference2[SelectionPreference2["NOT_IP"] = 3] = "NOT_IP";
  })(SelectionPreference = ServerAddress2.SelectionPreference || (ServerAddress2.SelectionPreference = {}));
  function selectionPreferenceOf(address) {
    if (!("ip" in address)) {
      return 3 /* NOT_IP */;
    }
    if (address.ip.startsWith("fd")) {
      return 0 /* IPV6_ULA */;
    }
    if (address.ip.startsWith("fe00")) {
      return 1 /* IPV6_LINK_LOCAL */;
    }
    if (address.ip.includes(":")) {
      return 2 /* IPV6 */;
    }
    return 3 /* IPV4 */;
  }
  ServerAddress2.selectionPreferenceOf = selectionPreferenceOf;
})(ServerAddress || (ServerAddress = {}));
//# sourceMappingURL=ServerAddress.js.map
