"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 AppAddress_exports = {};
__export(AppAddress_exports, {
  AppAddress: () => AppAddress,
  InvalidAppAddress: () => InvalidAppAddress
});
module.exports = __toCommonJS(AppAddress_exports);
var import_Diagnostic = require("#log/Diagnostic.js");
var import_Network = require("./Network.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class InvalidAppAddress extends import_Network.NetworkError {
}
class AppAddress extends URL {
  constructor(definition) {
    if (typeof definition === "object" && !(definition instanceof URL)) {
      let protocol = definition.appProtocol;
      let host;
      if (definition.tls) {
        protocol += "s";
      }
      if (definition.transport.kind === "unix") {
        protocol += "+unix";
        host = encodeURIComponent(definition.transport.path);
      } else {
        host = definition.transport.host;
        if (definition.transport.port) {
          host = `${host}:${definition.transport.port}`;
        }
      }
      const path = definition.path ?? "/";
      definition = `${protocol}://${host}${path}`;
    }
    try {
      super(definition);
    } catch (e) {
      if (e instanceof TypeError || e instanceof SyntaxError) {
        throw new InvalidAppAddress(`Invalid app address: ${e.message}`);
      }
      throw e;
    }
  }
  get [import_Diagnostic.Diagnostic.value]() {
    return this.toString();
  }
  get appProtocol() {
    return this.protocol.replace(/[:+].*$/, "");
  }
  get isTls() {
    switch (this.appProtocol) {
      case "https":
      case "mqtts":
      case "wss":
        return true;
      case "mqtt":
        return this.protocolModifiers.includes("wss");
    }
    return false;
  }
  get protocolModifiers() {
    const segments = this.protocol.replace(/:$/, "").split("+");
    segments.shift();
    return segments;
  }
  get transport() {
    if (this.protocolModifiers.includes("unix")) {
      return {
        kind: "unix",
        path: decodeURIComponent(this.hostname)
      };
    }
    return {
      kind: "ip",
      host: this.hostname,
      port: this.portNum
    };
  }
  get portNum() {
    if (this.port !== "") {
      return Number(this.port);
    }
    return 0;
  }
  get isWildcardHost() {
    const host = this.hostname.replace(/^\[|\]$/g, "");
    return host === "0.0.0.0" || host === "::";
  }
  get isWildcardPort() {
    return this.port === "0";
  }
  static for(definition) {
    if (definition instanceof AppAddress) {
      return definition;
    }
    return new AppAddress(definition);
  }
}
//# sourceMappingURL=AppAddress.js.map
