/**
 * BulletList Node Extension
 * 无序列表节点
 */

import { NodeExtension } from '../../core/Extension';
import { wrappingInputRule } from 'prosemirror-inputrules';
import { wrapInList, liftListItem, sinkListItem } from 'prosemirror-schema-list';
import type { NodeSpec, Schema, InputRule, Command } from '../../core/types';

export class BulletList extends NodeExtension {
  get name() {
    return 'bulletList';
  }

  get schema(): NodeSpec {
    return {
      content: 'listItem+',
      group: 'block',
      parseDOM: [{ tag: 'ul' }],
      toDOM() {
        return ['ul', { class: 'editor-bullet-list' }, 0];
      },
    };
  }

  inputRules(schema: Schema): InputRule[] {
    const type = schema.nodes[this.name];
    if (!type) return [];

    return [
      // - 或 * 开头转换为无序列表
      wrappingInputRule(/^\s*([-*])\s$/, type),
    ];
  }

  keys(schema: Schema): Record<string, Command> {
    const listItemType = schema.nodes.listItem;
    if (!listItemType) return {};

    return {
      'Tab': sinkListItem(listItemType),
      'Shift-Tab': liftListItem(listItemType),
    };
  }
}

export default BulletList;
