import { DomEditor, type IButtonMenu, type IDomEditor } from '@wangeditor/core';
import { SlateTransforms } from '@wangeditor/editor';
import deleteRaw from '../icon/delete.svg?raw';

class DeleteVideoButtonMenu implements IButtonMenu {
  readonly title = '删除视频';
  readonly iconSvg = deleteRaw;
  readonly tag = 'button';

  getValue(editor: IDomEditor): string | boolean {
    // 无需获取 val
    return '';
  }

  isActive(editor: IDomEditor): boolean {
    // 无需 active
    return false;
  }

  isDisabled(editor: IDomEditor): boolean {
    if (editor.selection == null) return true;

    const videoNode = DomEditor.getSelectedNodeByType(editor, 'video');
    return videoNode == null;
  }

  exec(editor: IDomEditor, value: string | boolean) {
    if (this.isDisabled(editor)) return;

    // 删除视频
    SlateTransforms.removeNodes(editor, {
      match: (n) => DomEditor.checkNodeType(n, 'video'),
    });
  }
}

export default {
  key: 'deleteVideo',
  factory(): DeleteVideoButtonMenu {
    return new DeleteVideoButtonMenu();
  },
};
