"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const path = require("./path.js");
const platform = require("./platform.js");
const strings = require("./strings.js");
function isPathSeparator(code) {
  return code === 47 || code === 92;
}
function toSlashes(osPath) {
  return osPath.replace(/[\\/]/g, path.posix.sep);
}
function toPosixPath(osPath) {
  if (osPath.indexOf("/") === -1) {
    osPath = toSlashes(osPath);
  }
  if (/^[a-zA-Z]:(\/|$)/.test(osPath)) {
    osPath = "/" + osPath;
  }
  return osPath;
}
function getRoot(path$1, sep = path.posix.sep) {
  if (!path$1) {
    return "";
  }
  const len = path$1.length;
  const firstLetter = path$1.charCodeAt(0);
  if (isPathSeparator(firstLetter)) {
    if (isPathSeparator(path$1.charCodeAt(1))) {
      if (!isPathSeparator(path$1.charCodeAt(2))) {
        let pos2 = 3;
        const start = pos2;
        for (; pos2 < len; pos2++) {
          if (isPathSeparator(path$1.charCodeAt(pos2))) {
            break;
          }
        }
        if (start !== pos2 && !isPathSeparator(path$1.charCodeAt(pos2 + 1))) {
          pos2 += 1;
          for (; pos2 < len; pos2++) {
            if (isPathSeparator(path$1.charCodeAt(pos2))) {
              return path$1.slice(0, pos2 + 1).replace(/[\\/]/g, sep);
            }
          }
        }
      }
    }
    return sep;
  } else if (isWindowsDriveLetter(firstLetter)) {
    if (path$1.charCodeAt(1) === 58) {
      if (isPathSeparator(path$1.charCodeAt(2))) {
        return path$1.slice(0, 2) + sep;
      } else {
        return path$1.slice(0, 2);
      }
    }
  }
  let pos = path$1.indexOf("://");
  if (pos !== -1) {
    pos += 3;
    for (; pos < len; pos++) {
      if (isPathSeparator(path$1.charCodeAt(pos))) {
        return path$1.slice(0, pos + 1);
      }
    }
  }
  return "";
}
function isEqualOrParent(base, parentCandidate, ignoreCase, separator = path.sep) {
  if (base === parentCandidate) {
    return true;
  }
  if (!base || !parentCandidate) {
    return false;
  }
  if (parentCandidate.length > base.length) {
    return false;
  }
  if (ignoreCase) {
    const beginsWith = strings.startsWithIgnoreCase(base, parentCandidate);
    if (!beginsWith) {
      return false;
    }
    if (parentCandidate.length === base.length) {
      return true;
    }
    let sepOffset = parentCandidate.length;
    if (parentCandidate.charAt(parentCandidate.length - 1) === separator) {
      sepOffset--;
    }
    return base.charAt(sepOffset) === separator;
  }
  if (parentCandidate.charAt(parentCandidate.length - 1) !== separator) {
    parentCandidate += separator;
  }
  return base.indexOf(parentCandidate) === 0;
}
function isWindowsDriveLetter(char0) {
  return char0 >= 65 && char0 <= 90 || char0 >= 97 && char0 <= 122;
}
function isRootOrDriveLetter(path$1) {
  const pathNormalized = path.normalize(path$1);
  if (platform.isWindows) {
    if (path$1.length > 3) {
      return false;
    }
    return hasDriveLetter(pathNormalized) && (path$1.length === 2 || pathNormalized.charCodeAt(2) === 92);
  }
  return pathNormalized === path.posix.sep;
}
function hasDriveLetter(path2, continueAsWindows) {
  const isWindowsPath = continueAsWindows !== void 0 ? continueAsWindows : platform.isWindows;
  if (isWindowsPath) {
    return isWindowsDriveLetter(path2.charCodeAt(0)) && path2.charCodeAt(1) === 58;
  }
  return false;
}
exports.getRoot = getRoot;
exports.hasDriveLetter = hasDriveLetter;
exports.isEqualOrParent = isEqualOrParent;
exports.isPathSeparator = isPathSeparator;
exports.isRootOrDriveLetter = isRootOrDriveLetter;
exports.isWindowsDriveLetter = isWindowsDriveLetter;
exports.toPosixPath = toPosixPath;
exports.toSlashes = toSlashes;
