import { handleReplace, getLinks } from "./main.ts";
import { replace } from "../Projectを横断してリンクを置換するUserScript/mod.ts";
import type { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts";
declare const scrapbox: Scrapbox;

export const setup = (projects: Iterable<string>): void => {
  const p = [scrapbox.Project.name, ...projects];
  scrapbox.PopupMenu.addButton({
    title: (text) => {
      const linkCount = getLinks(text).length;
      return linkCount > 1 ? "update links" : linkCount === 1 ? "update a link" : "";
    },
    onClick: (text): undefined => {
      const linkCount = getLinks(text).length;
      if (linkCount > 1) {
        handleReplace(text, p);
      } else if (linkCount === 1) {
        replace(text, p);
      }
      return undefined;
    },
  });
};