"use strict";
/* Socket Lib - Built with esbuild */
"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 normalize_exports = {};
__export(normalize_exports, {
  fromUnixPath: () => import_conversion.fromUnixPath,
  getUrl: () => import_internal2.getUrl,
  isAbsolute: () => import_predicates.isAbsolute,
  isNodeModules: () => import_predicates.isNodeModules,
  isPath: () => import_predicates.isPath,
  isPathSeparator: () => import_predicates.isPathSeparator,
  isRelative: () => import_predicates.isRelative,
  isUnixPath: () => import_predicates.isUnixPath,
  isWindowsDeviceRoot: () => import_predicates.isWindowsDeviceRoot,
  msysDriveToNative: () => msysDriveToNative,
  normalizePath: () => normalizePath,
  pathLikeToString: () => import_internal2.pathLikeToString,
  relative: () => import_resolve.relative,
  relativeResolve: () => import_resolve.relativeResolve,
  resolve: () => import_resolve.resolve,
  splitPath: () => import_conversion.splitPath,
  toUnixPath: () => import_conversion.toUnixPath,
  trimLeadingDotSlash: () => import_conversion.trimLeadingDotSlash
});
module.exports = __toCommonJS(normalize_exports);
var import_platform = require("../constants/platform");
var import_search = require("../strings/search");
var import_string = require("../primordials/string");
var import_internal = require("./_internal");
var import_internal2 = require("./_internal");
var import_conversion = require("./conversion");
var import_predicates = require("./predicates");
var import_resolve = require("./resolve");
function msysDriveToNative(normalized) {
  if (import_platform.WIN32) {
    return normalized.replace(
      import_internal.msysDriveRegExp,
      (_, letter, sep) => `${letter.toUpperCase()}:${sep || "/"}`
    );
  }
  return normalized;
}
// @__NO_SIDE_EFFECTS__
function normalizePath(pathLike) {
  const filepath = (0, import_internal.pathLikeToString)(pathLike);
  const { length } = filepath;
  if (length === 0) {
    return ".";
  }
  if (length < 2) {
    return length === 1 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 0) === 92 ? "/" : filepath;
  }
  let code = 0;
  let start = 0;
  let prefix = "";
  if (length > 4 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 3) === 92) {
    const code2 = (0, import_string.StringPrototypeCharCodeAt)(filepath, 2);
    if ((code2 === 63 || code2 === 46) && (0, import_string.StringPrototypeCharCodeAt)(filepath, 0) === 92 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 1) === 92) {
      start = 2;
      prefix = "//";
    }
  }
  if (start === 0) {
    if (length > 2 && ((0, import_string.StringPrototypeCharCodeAt)(filepath, 0) === 92 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 1) === 92 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 2) !== 92 || (0, import_string.StringPrototypeCharCodeAt)(filepath, 0) === 47 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 1) === 47 && (0, import_string.StringPrototypeCharCodeAt)(filepath, 2) !== 47)) {
      let firstSegmentEnd = -1;
      let hasSecondSegment = false;
      let i = 2;
      while (i < length && ((0, import_string.StringPrototypeCharCodeAt)(filepath, i) === 47 || (0, import_string.StringPrototypeCharCodeAt)(filepath, i) === 92)) {
        i++;
      }
      while (i < length) {
        const char = (0, import_string.StringPrototypeCharCodeAt)(filepath, i);
        if (char === 47 || char === 92) {
          firstSegmentEnd = i;
          break;
        }
        i++;
      }
      if (firstSegmentEnd > 2) {
        i = firstSegmentEnd;
        while (i < length && ((0, import_string.StringPrototypeCharCodeAt)(filepath, i) === 47 || (0, import_string.StringPrototypeCharCodeAt)(filepath, i) === 92)) {
          i++;
        }
        if (i < length) {
          hasSecondSegment = true;
        }
      }
      if (firstSegmentEnd > 2 && hasSecondSegment) {
        start = 2;
        prefix = "//";
      } else {
        code = (0, import_string.StringPrototypeCharCodeAt)(filepath, start);
        while (code === 47 || code === 92) {
          start += 1;
          code = (0, import_string.StringPrototypeCharCodeAt)(filepath, start);
        }
        if (start) {
          prefix = "/";
        }
      }
    } else {
      code = (0, import_string.StringPrototypeCharCodeAt)(filepath, start);
      while (code === 47 || code === 92) {
        start += 1;
        code = (0, import_string.StringPrototypeCharCodeAt)(filepath, start);
      }
      if (start) {
        prefix = "/";
      }
    }
  }
  let nextIndex = (0, import_search.search)(filepath, import_internal.slashRegExp, { fromIndex: start });
  if (nextIndex === -1) {
    const segment = filepath.slice(start);
    if (segment === "." || segment.length === 0) {
      return prefix || ".";
    }
    if (segment === "..") {
      return prefix ? (0, import_string.StringPrototypeSlice)(prefix, 0, -1) || "/" : "..";
    }
    return msysDriveToNative(prefix + segment);
  }
  let collapsed = "";
  let segmentCount = 0;
  let leadingDotDots = 0;
  while (nextIndex !== -1) {
    const segment = filepath.slice(start, nextIndex);
    if (segment.length > 0 && segment !== ".") {
      if (segment === "..") {
        if (segmentCount > 0) {
          const lastSeparatorIndex = collapsed.lastIndexOf("/");
          if (lastSeparatorIndex === -1) {
            collapsed = "";
            segmentCount = 0;
            if (leadingDotDots > 0 && !prefix) {
              collapsed = "..";
              leadingDotDots = 1;
            }
          } else {
            const lastSegmentStart = lastSeparatorIndex + 1;
            const lastSegmentValue = collapsed.slice(lastSegmentStart);
            if (lastSegmentValue === "..") {
              collapsed = `${collapsed}/${segment}`;
              leadingDotDots += 1;
            } else {
              collapsed = collapsed.slice(0, lastSeparatorIndex);
              segmentCount -= 1;
            }
          }
        } else if (!prefix) {
          collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + segment;
          leadingDotDots += 1;
        }
      } else {
        collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + segment;
        segmentCount += 1;
      }
    }
    start = nextIndex + 1;
    code = (0, import_string.StringPrototypeCharCodeAt)(filepath, start);
    while (code === 47 || code === 92) {
      start += 1;
      code = (0, import_string.StringPrototypeCharCodeAt)(filepath, start);
    }
    nextIndex = (0, import_search.search)(filepath, import_internal.slashRegExp, { fromIndex: start });
  }
  const lastSegment = filepath.slice(start);
  if (lastSegment.length > 0 && lastSegment !== ".") {
    if (lastSegment === "..") {
      if (segmentCount > 0) {
        const lastSeparatorIndex = collapsed.lastIndexOf("/");
        if (lastSeparatorIndex === -1) {
          collapsed = "";
          segmentCount = 0;
          if (leadingDotDots > 0 && !prefix) {
            collapsed = "..";
            leadingDotDots = 1;
          }
        } else {
          const lastSegmentStart = lastSeparatorIndex + 1;
          const lastSegmentValue = collapsed.slice(lastSegmentStart);
          if (lastSegmentValue === "..") {
            collapsed = `${collapsed}/${lastSegment}`;
            leadingDotDots += 1;
          } else {
            collapsed = collapsed.slice(0, lastSeparatorIndex);
            segmentCount -= 1;
          }
        }
      } else if (!prefix) {
        collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + lastSegment;
        leadingDotDots += 1;
      }
    } else {
      collapsed = collapsed + (collapsed.length === 0 ? "" : "/") + lastSegment;
      segmentCount += 1;
    }
  }
  if (collapsed.length === 0) {
    return prefix || ".";
  }
  return msysDriveToNative(prefix + collapsed);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  fromUnixPath,
  getUrl,
  isAbsolute,
  isNodeModules,
  isPath,
  isPathSeparator,
  isRelative,
  isUnixPath,
  isWindowsDeviceRoot,
  msysDriveToNative,
  normalizePath,
  pathLikeToString,
  relative,
  relativeResolve,
  resolve,
  splitPath,
  toUnixPath,
  trimLeadingDotSlash
});
