"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var url_exports = {};
__export(url_exports, {
  getAddressUrls: () => getAddressUrls,
  normalizeUrl: () => normalizeUrl,
  withPublicPath: () => withPublicPath
});
module.exports = __toCommonJS(url_exports);
var import_os = __toESM(require("os"));
var import_url = require("url");
var import_path = require("path");
var import_constants = require("./constants");
const normalizeUrl = (url) => url.replace(/([^:]\/)\/+/g, "$1");
const urlJoin = (base, path) => {
  const fullUrl = new import_url.URL(base);
  fullUrl.pathname = import_path.posix.join(fullUrl.pathname, path);
  return fullUrl.toString();
};
const withPublicPath = (str, base) => {
  if (str.startsWith("//")) {
    return str;
  }
  try {
    return new import_url.URL(str).toString();
  } catch {
  }
  if (base.startsWith("http")) {
    return urlJoin(base, str);
  }
  if (base.startsWith("//")) {
    base = `https:${base}`;
    return urlJoin(base, str).replace("https:", "");
  }
  return import_path.posix.join(base, str);
};
const getIpv4Interfaces = () => {
  const interfaces = import_os.default.networkInterfaces();
  const ipv4Interfaces = /* @__PURE__ */ new Map();
  Object.keys(interfaces).forEach((key) => {
    interfaces[key].forEach((detail) => {
      const familyV4Value = typeof detail.family === "string" ? "IPv4" : 4;
      if (detail.family === familyV4Value && !ipv4Interfaces.has(detail.address)) {
        ipv4Interfaces.set(detail.address, detail);
      }
    });
  });
  return Array.from(ipv4Interfaces.values());
};
const getAddressUrls = (protocol = "http", port, host) => {
  const LOCAL_LABEL = "Local:  ";
  const NETWORK_LABEL = "Network:  ";
  const isLocalhost = (url) => url?.includes("localhost");
  if (host && host !== import_constants.DEFAULT_DEV_HOST) {
    return [
      {
        label: isLocalhost(host) ? LOCAL_LABEL : NETWORK_LABEL,
        url: `${protocol}://${host}:${port}`
      }
    ];
  }
  const ipv4Interfaces = getIpv4Interfaces();
  return ipv4Interfaces.reduce((memo, detail) => {
    if (isLocalhost(detail.address) || detail.internal) {
      memo.push({
        label: LOCAL_LABEL,
        url: `${protocol}://localhost:${port}`
      });
    } else {
      memo.push({
        label: NETWORK_LABEL,
        url: `${protocol}://${detail.address}:${port}`
      });
    }
    return memo;
  }, []);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getAddressUrls,
  normalizeUrl,
  withPublicPath
});
