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

import { isWindows } from "./_os.ts";
import { basename as posixBasename } from "./posix/basename.ts";
import { basename as windowsBasename } from "./windows/basename.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("/home/user/Documents/"); // "Documents"
 * basename("C:\\user\\Documents\\image.png"); // "image.png"
 * basename("/home/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 {
  return isWindows
    ? windowsBasename(path, suffix)
    : posixBasename(path, suffix);
}

// denoCacheMetadata={"headers":{"server":"cloudflare","content-length":"942","x-content-type-options":"nosniff","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","accept-ranges":"bytes","cache-control":"public, max-age=31536000, immutable","set-cookie":"__cf_bm=ymtLoHNLNVCQLi2hzg_NItxxFk.MfXPdPlYrqhb5zx8-1785241875.1386793-1.0.1.1-gyigFhZZ3T6F16Q6Ebc11kBl.bZKE8iSiliDb6ntBvug5feLFMXnBKbOk0EVZ_4xe3bjl6Y5sV6Un.ON5.q6YOyJ_yLyQSgalsGa8774.tuesYmo0H2DMrAieKVWo5fb; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","content-type":"text/typescript","date":"Tue, 28 Jul 2026 12:31:15 GMT","cf-ray":"a223e6579a0a7db6-LAX","vary":"Origin","alt-svc":"h3=\":443\"; ma=86400","x-robots-tag":"noindex","access-control-allow-origin":"*","etag":"\"08dee624e544e4898af6e26adcb0edb1\"","cross-origin-resource-policy":"cross-origin","cf-cache-status":"HIT","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';","x-jsr-backend":"modules","age":"44352","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=LrflP4J5UOtmdb%2BklsFJWKM9RpwuLPKoj8%2BQFlATm3vczf4U%2BijVEgGdUaFPIIp%2FGp6J2QtwptCvgID64dyeCTZ2HvnY32JCt6D8Z9HJug3bblJAqeEABv8%3D\"}]}"},"url":"https://jsr.io/@std/path/0.224.0/basename.ts","time":1785241875}