// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import {
  CHAR_BACKWARD_SLASH,
  CHAR_COLON,
  CHAR_DOT,
  CHAR_QUESTION_MARK,
} from "../_common/constants.ts";
import { isWindowsDeviceRoot } from "./_util.ts";
import { resolve } from "./resolve.ts";

/**
 * Resolves path to a namespace path
 * @param path to resolve to namespace
 */
export function toNamespacedPath(path: string): string {
  // Note: this will *probably* throw somewhere.
  if (typeof path !== "string") return path;
  if (path.length === 0) return "";

  const resolvedPath = resolve(path);

  if (resolvedPath.length >= 3) {
    if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
      // Possible UNC root

      if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
        const code = resolvedPath.charCodeAt(2);
        if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
          // Matched non-long UNC root, convert the path to a long UNC path
          return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
        }
      }
    } else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
      // Possible device root

      if (
        resolvedPath.charCodeAt(1) === CHAR_COLON &&
        resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH
      ) {
        // Matched device root, convert the path to a long UNC path
        return `\\\\?\\${resolvedPath}`;
      }
    }
  }

  return path;
}

// denoCacheMetadata={"headers":{"accept-ranges":"bytes","vary":"Origin","set-cookie":"__cf_bm=7oISfLJqvZyzeCxkdvlG.WOhm1PONPbG2XB7W0MIpu8-1785241875.2412803-1.0.1.1-AsxzSjmqXb1jwY7Xl77LnPd32Y9bbIItygovRTsgxb09PARBxGK0nqCjvWbDvzv6bCm2zipdMRNXh6QSZjH4pQwmx5k8vH.lqT.3Ht9yafbyHtyxsbIv3w4NGNofQN5_; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","alt-svc":"h3=\":443\"; ma=86400","date":"Tue, 28 Jul 2026 12:31:15 GMT","age":"44352","cross-origin-resource-policy":"cross-origin","x-jsr-backend":"modules","x-content-type-options":"nosniff","cf-cache-status":"HIT","access-control-allow-origin":"*","cf-ray":"a223e6583bcb7db6-LAX","content-length":"1457","server":"cloudflare","content-type":"text/typescript","etag":"\"1b92ef86fde588971a9173638d353e14\"","access-control-expose-headers":"*","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=Zrk%2F7cinq5w6KLlI9mTW0DrQ0hMQsehkZCpBWPLZGM8GE2ovkI6c9pDGaKjPc1pedtDHHRfeGtafMlp7rw3GX5mYrKcYc2jJjT%2BEyYRGqsHQgrB9gzL38RQ%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","x-robots-tag":"noindex","cache-control":"public, max-age=31536000, immutable"},"url":"https://jsr.io/@std/path/0.224.0/windows/to_namespaced_path.ts","time":1785241875}