import { deleteBulk } from "./mod.ts";
import { Command } from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts";
import { toFileUrl, join } from "https://scrapbox.io/api/code/takker/deno_std%2Fpath/mod.ts";

const { args: [token, list] } = await new Command()
   .name("delete-gyazo")
   .description("複数のGyazoの画像を削除するツール")
   .version("v1.0.0")
   .arguments("<token:string> <list:string>")
   .parse(Deno.args);

const res = await fetch(toFileUrl(join(Deno.cwd(), list)));
const json = (await res.json()) as (string | null)[];
const imageList: string[] = json
  .flatMap((url) => url ? [url.slice("https://gyazo.com/".length)] : []);

const accessToken = Deno.args[0];

let i = 0;
for await (const result of deleteBulk(imageList, token)) {
  if (!result.success) {
    console.error(result.reason);
  } else {
    console.log(`[${i++}] Delete ${result.value}`);
  }
}