import History from '@tiptap/extension-history';
import {
  Document,
  Dropcursor,
  Figcaption,
  Focus,
  FontStyle,
  Heading,
  Highlight,
  HorizontalRule,
  Link,
  Placeholder,
  Selection,
  StarterKit,
  Subscript,
  Superscript,
  Table,
  TableCell,
  TableHeader,
  TableRow,
  TextAlign,
  TextStyle,
  TrailingNode,
  Typography,
  Underline,
  Columns,
  Column,
  ImageBlock,
  FileBlock,
  PasteHandler,
  LinkButton,
  MediaUpload,
  isAllowedBlockEditorLinkUri,
  CustomMark,
  CustomConvertBlock,
} from '.';

export interface ExtensionKitOptions {
  /** 画像の新規挿入時に拡大表示（Lightbox）をデフォルト有効にするかを返す getter */
  getImageDefaultLightbox?: () => boolean;
}

export const ExtensionKit = ({ getImageDefaultLightbox }: ExtensionKitOptions = {}) => {
  return [
    StarterKit.configure({
      document: false,
      dropcursor: false,
      heading: false,
      horizontalRule: false,
      history: false,
    }),
    History,
    Document,
    Columns,
    Column,
    Selection,
    Heading.configure({
      levels: [1, 2, 3, 4, 5, 6],
    }),
    HorizontalRule,
    TextStyle,
    FontStyle,
    TrailingNode,
    Link.configure({
      openOnClick: false,
      isAllowedUri: (url, { protocols }) => !!isAllowedBlockEditorLinkUri(url, protocols),
      HTMLAttributes: {
        // Change rel to different value
        // Allow search engines to follow links(remove nofollow)
        rel: 'noopener noreferrer',
        // Remove target entirely so links open in current tab
        target: null,
      },
    }),
    Highlight.configure({ multicolor: true }),
    Underline,
    MediaUpload,
    CustomMark,
    CustomConvertBlock,
    ImageBlock.configure({
      getImageDefaultLightbox: getImageDefaultLightbox ?? (() => true),
    }),
    FileBlock,
    PasteHandler,
    LinkButton,
    TextAlign.extend({
      addKeyboardShortcuts() {
        return {};
      },
    }).configure({
      types: ['heading', 'paragraph'],
    }),
    Subscript,
    Superscript,
    Table,
    TableCell,
    TableHeader,
    TableRow,
    Typography,
    Placeholder.configure({
      includeChildren: true,
      showOnlyCurrent: false,
      placeholder: () => '',
    }),
    Focus,
    Figcaption,
    Dropcursor.configure({
      width: 4,
      color: '#a3c0ef',
      class: 'acms-admin-dropcursor',
    }),
  ];
};

export default ExtensionKit;
