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

import {
  assertArgs,
  lastPathSegment,
  stripSuffix,
} from "../_common/basename.ts";
import { CHAR_COLON } from "../_common/constants.ts";
import { stripTrailingSeparators } from "../_common/strip_trailing_separators.ts";
import { isPathSeparator, isWindowsDeviceRoot } from "./_util.ts";

/**
 * Return the last portion of a `path`.
 * Trailing directory separators are ignored, and optional suffix is removed.
 *
 * @example
 * ```ts
 * import { basename } from "@std/path/basename";
 *
 * basename("C:\\user\\Documents\\"); // "Documents"
 * basename("C:\\user\\Documents\\image.png"); // "image.png"
 * basename("C:\\user\\Documents\\image.png", ".png"); // "image"
 * ```
 *
 * @param path - path to extract the name from.
 * @param [suffix] - suffix to remove from extracted name.
 */
export function basename(path: string, suffix = ""): string {
  assertArgs(path, suffix);

  // Check for a drive letter prefix so as not to mistake the following
  // path separator as an extra separator at the end of the path that can be
  // disregarded
  let start = 0;
  if (path.length >= 2) {
    const drive = path.charCodeAt(0);
    if (isWindowsDeviceRoot(drive)) {
      if (path.charCodeAt(1) === CHAR_COLON) start = 2;
    }
  }

  const lastSegment = lastPathSegment(path, isPathSeparator, start);
  const strippedSegment = stripTrailingSeparators(lastSegment, isPathSeparator);
  return suffix ? stripSuffix(strippedSegment, suffix) : strippedSegment;
}

// denoCacheMetadata={"headers":{"accept-ranges":"bytes","date":"Tue, 28 Jul 2026 12:31:15 GMT","x-content-type-options":"nosniff","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';","set-cookie":"__cf_bm=AeAGMz8HZC1ipx8mo9R4dpQdC.VujP6tzWYz1DIQB8I-1785241875.2309635-1.0.1.1-CQaNgS_BgTSTgWrSyterNfATy0Kk4h_acfrfLzTEAGVs.DvOnZc5uBlK4O5jG2OFCmjImWTtNzv5Yt8ngHx8El_PzkWwC_n3jA1Zrnw5S86jXkt4hwE7Xv9wrx8PCIdB; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","vary":"Origin","x-jsr-backend":"modules","cache-control":"public, max-age=31536000, immutable","x-robots-tag":"noindex","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","etag":"\"9d47b482594921367f892c308e42c18d\"","alt-svc":"h3=\":443\"; ma=86400","age":"468293","content-type":"text/typescript","cf-ray":"a223e6582b9f7db6-LAX","server":"cloudflare","access-control-allow-origin":"*","cross-origin-resource-policy":"cross-origin","content-length":"1579","cf-cache-status":"HIT","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=6uUztHRCoxJfHfPKLXyo916OHhvg7O%2FVnnkzYC83T0e3T4L2L2BBOqoWSkh6dYPJTTrXZRSVWpL3zyuBFIBBz5JC9wqfhbHnwApERQVeKWdjoKvpGvK0H40%3D\"}]}"},"url":"https://jsr.io/@std/path/0.224.0/windows/basename.ts","time":1785241875}