// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { dirname } from "jsr:/@std/path@^0.224.0/dirname";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { toPathString } from "./_to_path_string.ts";

/**
 * Asynchronously ensures that the hard link exists. If the directory structure
 * does not exist, it is created.
 *
 * @param src The source file path as a string or URL. Directory hard links are
 * not allowed.
 * @param dest The destination link path as a string or URL.
 * @returns A void promise that resolves once the hard link exists.
 *
 * @example
 * ```ts
 * import { ensureLink } from "@std/fs/ensure-link";
 *
 * await ensureLink("./folder/targetFile.dat", "./folder/targetFile.link.dat");
 * ```
 */
export async function ensureLink(src: string | URL, dest: string | URL) {
  dest = toPathString(dest);
  await ensureDir(dirname(dest));

  await Deno.link(toPathString(src), dest);
}

/**
 * Synchronously ensures that the hard link exists. If the directory structure
 * does not exist, it is created.
 *
 * @param src The source file path as a string or URL. Directory hard links are
 * not allowed.
 * @param dest The destination link path as a string or URL.
 * @returns A void value that returns once the hard link exists.
 *
 * @example
 * ```ts
 * import { ensureLinkSync } from "@std/fs/ensure-link";
 *
 * ensureLinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat");
 * ```
 */
export function ensureLinkSync(src: string | URL, dest: string | URL) {
  dest = toPathString(dest);
  ensureDirSync(dirname(dest));

  Deno.linkSync(toPathString(src), dest);
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","alt-svc":"h3=\":443\"; ma=86400","x-robots-tag":"noindex","content-type":"text/typescript","cache-control":"public, max-age=31536000, immutable","accept-ranges":"bytes","cf-cache-status":"HIT","access-control-expose-headers":"*","cross-origin-resource-policy":"cross-origin","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=RxFcMnzzt4cKx%2F7v1GM6aX3uOf8hsz2yizvQIyHkD6zv%2Bi3w%2F1C4txz9NtYHvYIpYJTUnYxqEQtJpoV0fgWsukst63cEF5%2FctfGpHewvy2Qy39aNqPwNTw8%3D\"}]}","age":"804698","vary":"Origin","cf-ray":"a223e656afd97db6-LAX","server":"cloudflare","x-content-type-options":"nosniff","x-jsr-backend":"modules","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';","content-length":"1643","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"1073fc87744d08eb174a2f66a2caa8f1\"","set-cookie":"__cf_bm=a.txQ3mholqudq54zfHtObN7JKwmxMFhXlMBW1HGoq4-1785241874.9983783-1.0.1.1-nUoQMt_lDO6L4zC_awk1lBkVjiBC2NbzZY0cW4mk3VoADsLjc9NRXCMe1geLvvgbjpwIa1mMQO0cyhKNljoBMZReT7yrVAGgvk.6X6WZub5aLbtk0yN9f687OgeP8WjW; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT"},"url":"https://jsr.io/@std/fs/0.224.0/ensure_link.ts","time":1785241875}