<script setup lang="ts">
import type { Editor } from "@tiptap/vue-3";

const props = defineProps<{
  editor: Editor | null;
}>();

const buttons = [
  {
    label: "B",
    title: "Bold",
    command: () => props.editor?.chain().focus().toggleBold().run(),
    isActive: () => props.editor?.isActive("bold") ?? false,
  },
  {
    label: "I",
    title: "Italic",
    command: () => props.editor?.chain().focus().toggleItalic().run(),
    isActive: () => props.editor?.isActive("italic") ?? false,
  },
  {
    label: "H1",
    title: "Heading 1",
    command: () =>
      props.editor?.chain().focus().toggleHeading({ level: 1 }).run(),
    isActive: () => props.editor?.isActive("heading", { level: 1 }) ?? false,
  },
  {
    label: "H2",
    title: "Heading 2",
    command: () =>
      props.editor?.chain().focus().toggleHeading({ level: 2 }).run(),
    isActive: () => props.editor?.isActive("heading", { level: 2 }) ?? false,
  },
  {
    label: "Link",
    title: "Link",
    command: () => {
      const ed = props.editor;
      if (!ed) return;
      if (ed.isActive("link")) {
        ed.chain().focus().unsetLink().run();
      } else {
        const url = prompt("URL:");
        if (url) {
          ed.chain().focus().setLink({ href: url }).run();
        }
      }
    },
    isActive: () => props.editor?.isActive("link") ?? false,
  },
  {
    label: "UL",
    title: "Bullet List",
    command: () => props.editor?.chain().focus().toggleBulletList().run(),
    isActive: () => props.editor?.isActive("bulletList") ?? false,
  },
  {
    label: "OL",
    title: "Numbered List",
    command: () => props.editor?.chain().focus().toggleOrderedList().run(),
    isActive: () => props.editor?.isActive("orderedList") ?? false,
  },
  {
    label: "`",
    title: "Inline Code",
    command: () => props.editor?.chain().focus().toggleCode().run(),
    isActive: () => props.editor?.isActive("code") ?? false,
  },
  {
    label: "```",
    title: "Code Block",
    command: () => props.editor?.chain().focus().toggleCodeBlock().run(),
    isActive: () => props.editor?.isActive("codeBlock") ?? false,
  },
  {
    label: "[[",
    title: "Insert wikilink (type note title to search)",
    command: () => props.editor?.chain().focus().insertContent("[[").run(),
    isActive: () => false,
  },
];
</script>

<template>
  <div class="md-toolbar">
    <button
      v-for="btn in buttons"
      :key="btn.label"
      :class="['md-btn', { active: btn.isActive() }]"
      :title="btn.title"
      @click="btn.command()"
    >
      {{ btn.label }}
    </button>
  </div>
</template>

<style scoped>
.md-toolbar {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
}
.md-btn {
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--color-input-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg-card);
  color: var(--color-text);
  cursor: pointer;
  font-size: 0.8rem;
  font-family: inherit;
  line-height: 1;
}
.md-btn:hover {
  background: var(--color-bg-secondary);
}
.md-btn.active {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}
</style>
