"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 isIp(address) {
    return address.ip !== void 0;
  }
  ServerAddress2.isIp = isIp;
  function isBle(address) {
    return address.peripheralAddress !== void 0;
  }
  ServerAddress2.isBle = isBle;
  function protocolOf(address) {
    if (isBle(address)) {
      return "ble";
    }
    if ("type" in address && typeof address.type === "string") {
      return address.type;
    }
    return "ip";
  }
  ServerAddress2.protocolOf = protocolOf;
  function urlFor(address) {
    if (isIp(address)) {
      const proto = protocolOf(address);
      const host = address.ip.includes(":") ? `[${address.ip}]` : address.ip;
      return `${proto}://${host}:${address.port}`;
    }
    if (isBle(address)) {
      return `ble://${address.peripheralAddress}`;
    }
    return `unknown://`;
  }
  ServerAddress2.urlFor = urlFor;
  function diagnosticFor(address) {
    const diagnostic = Array();
    if (isIp(address)) {
      diagnostic.push(`${protocolOf(address)}://`, import_Diagnostic.Diagnostic.strong(address.ip), ":", address.port);
    } else if (isBle(address)) {
      diagnostic.push("ble://", import_Diagnostic.Diagnostic.strong(address.peripheralAddress));
    } else {
      diagnostic.push("unknown://");
    }
    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 (isIp(a) && isIp(b)) {
      return a.ip === b.ip && a.port === b.port;
    }
    if (isBle(a) && isBle(b)) {
      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_LINK_LOCAL"] = 0] = "IPV6_LINK_LOCAL";
    SelectionPreference2[SelectionPreference2["IPV6_ULA"] = 1] = "IPV6_ULA";
    SelectionPreference2[SelectionPreference2["IPV6"] = 2] = "IPV6";
    SelectionPreference2[SelectionPreference2["IPV4"] = 3] = "IPV4";
    SelectionPreference2[SelectionPreference2["NOT_IP"] = 3] = "NOT_IP";
  })(SelectionPreference = ServerAddress2.SelectionPreference || (ServerAddress2.SelectionPreference = {}));
  const IPV6_LINK_LOCAL_PATTERN = /^fe[89ab]/i;
  function isIpv6LinkLocal(ip) {
    return IPV6_LINK_LOCAL_PATTERN.test(ip);
  }
  ServerAddress2.isIpv6LinkLocal = isIpv6LinkLocal;
  function selectionPreferenceOf(address) {
    if (!isIp(address)) {
      return 3 /* NOT_IP */;
    }
    return selectionPreferenceOfIp(address.ip);
  }
  ServerAddress2.selectionPreferenceOf = selectionPreferenceOf;
  const IPV6_ULA_PATTERN = /^f[cd]/i;
  function selectionPreferenceOfIp(ip) {
    if (IPV6_ULA_PATTERN.test(ip)) {
      return 1 /* IPV6_ULA */;
    }
    if (isIpv6LinkLocal(ip)) {
      return 0 /* IPV6_LINK_LOCAL */;
    }
    if (ip.includes(":")) {
      return 2 /* IPV6 */;
    }
    return 3 /* IPV4 */;
  }
  ServerAddress2.selectionPreferenceOfIp = selectionPreferenceOfIp;
})(ServerAddress || (ServerAddress = {}));
//# sourceMappingURL=ServerAddress.js.map
