import { ensureFile } from "$std/fs/ensure_file.ts";

/**
 * 
 * @param src url of file
 * @param dest destination file. If `useFileName` is true, this should be the destination directory
 * @param [useFileName] whether to use the inferred file name from `src`
 */

export async function downloadFile(src: string, dest: string, useFileName?:boolean) {
  if (!(src.startsWith("http://") || src.startsWith("https://"))) {
    throw new TypeError("URL must start with be http:// or https://");
  }

  const fileName = src.split('/').at(-1);
  
  const resp = await fetch(src);
  if (!resp.ok) {
    throw new Deno.errors.BadResource(
      `Request failed with status ${resp.status}`,
    );
  } else if (!resp.body) {
    throw new Deno.errors.UnexpectedEof(
      `The download url ${src} doesn't contain a file to download`,
    );
  } else if (resp.status === 404) {
    throw new Deno.errors.NotFound(
      `The requested url "${src}" could not be found`,
    );
  }

  await ensureFile(useFileName ? dest + fileName : dest);
  const file = await Deno.open(dest, { truncate: true, write: true });
  resp.body.pipeTo(file.writable);
}