// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright the Browserify authors. MIT License.

import { basename } from "jsr:/@std/path@^0.224.0/basename";
import { normalize } from "jsr:/@std/path@^0.224.0/normalize";
import { toPathString } from "./_to_path_string.ts";

/**
 * Walk entry for {@linkcode walk}, {@linkcode walkSync},
 * {@linkcode expandGlob} and {@linkcode expandGlobSync}.
 */
export interface WalkEntry extends Deno.DirEntry {
  /** Full path of the entry. */
  path: string;
}

/** Create {@linkcode WalkEntry} for the `path` synchronously. */
export function createWalkEntrySync(path: string | URL): WalkEntry {
  path = toPathString(path);
  path = normalize(path);
  const name = basename(path);
  const info = Deno.statSync(path);
  return {
    path,
    name,
    isFile: info.isFile,
    isDirectory: info.isDirectory,
    isSymlink: info.isSymlink,
  };
}

/** Create {@linkcode WalkEntry} for the `path` asynchronously. */
export async function createWalkEntry(path: string | URL): Promise<WalkEntry> {
  path = toPathString(path);
  path = normalize(path);
  const name = basename(path);
  const info = await Deno.stat(path);
  return {
    path,
    name,
    isFile: info.isFile,
    isDirectory: info.isDirectory,
    isSymlink: info.isSymlink,
  };
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","content-type":"text/typescript","vary":"Origin","x-jsr-backend":"modules","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=AR265JUcCWsUSWOkEJv%2F7fpML6mTZzyaTpEGz%2FW0QbhMO89SDfSvsJTtfjfMedUmvSIjRr46%2FQJOlhAXySum9gtG5ekfsVw9IPXYganhmeO1U4%2BttjpcqOk%3D\"}]}","date":"Tue, 28 Jul 2026 12:31:15 GMT","age":"804698","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=Rq6MP0tq4Pj8beTmP24xWX8hi.1Y69DKfHT.4Iz9oLI-1785241875.211582-1.0.1.1-92YGRl6lA5lr2Cp5Yi0ak.UhZN2b1O_6t71KI_UlIPz97p0ehM4q4luxSkngtykixTDTSsVig7f08_2JjERfsvrzeps5WvK.5cCUsGn5XLTU0EHlWp6Y6L.IJMcZJakv; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","cf-cache-status":"HIT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","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';","cf-ray":"a223e657fb437db6-LAX","access-control-allow-origin":"*","server":"cloudflare","accept-ranges":"bytes","cache-control":"public, max-age=31536000, immutable","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","x-content-type-options":"nosniff","content-length":"1318","etag":"\"fb120aac254f18af47035cc1f3512ef9\""},"url":"https://jsr.io/@std/fs/0.224.0/_create_walk_entry.ts","time":1785241875}