import { addButton } from "./mod.ts";
import { porterCopy, porterCut, porterPaste, toggleCaret } from "./commands.ts";
import {
  downBlocks,
  downLines,
  indentBlocks,
  indentLines,
  outdentBlocks,
  outdentLines,
  upBlocks,
  upLines,
  redo,
  undo,
} from "../scrapbox-userscript-std/dom.ts";

addButton({
  display: { type: "caret-left" },
  onClick: ({ cursor, selection }) => {
    cursor.focus();
    selection.getSelectedText() === "" ? outdentBlocks() : outdentLines();
  },
});
addButton({
  display: { type: "caret-right" },
  onClick: ({ cursor, selection }) => {
    cursor.focus();
    selection.getSelectedText() === "" ? indentBlocks() : indentLines();
  },
});
addButton({
  display: { type: "caret-up" },
  onClick: ({ cursor, selection }) => {
    cursor.focus();
    selection.getSelectedText() === "" ? upBlocks() : upLines();
  },
});
addButton({
  display: { type: "caret-down" },
  onClick: ({ cursor, selection }) => {
    cursor.focus();
    selection.getSelectedText() === "" ? downBlocks() : downLines()
  },
});
addButton({
  display: { type: "copy" },
  onClick: async ({ cursor, selection }) => await porterCopy(cursor, selection),
});
addButton({
  display: { type: "cut" },
  onClick: async ({ cursor, selection }) => await porterCut(cursor, selection),
});
addButton({
  display: { type: "clipboard" },
  onClick: async ({ cursor }) => await porterPaste(cursor),
});
addButton({
  display: { type: "undo" },
  onClick: () => undo(),
});
addButton({
  display: { type: "redo" },
  onClick: () => redo(),
});
addButton({
  display: ({ cursor }) => cursor.getVisible() && cursor.hasFocus()
    // @ts-ignore private扱いだけど、これしかアクセス方法がないため使わせていただく
    && cursor.visiblePopupMenu ?
    { type: ["i-cursor", "slash"] } :
    { type: "i-cursor" },
  onClick: ({ cursor }) => toggleCaret(cursor),
});