import { InputRule, getNodeType, isNodeActive, mergeAttributes } from "./../../tiptap/core/index.js";
import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { CHECKBOX_LIST_STYLE_ATTRIBUTE_VALUE } from "./../../_shared/list-checkbox/checkbox-list-style.js";
import { isCheckboxListElement } from "./../../_shared/list-checkbox/is-checkbox-list-element.js";
import {
  createRicosNodeExtension,
  withBreakoutContainer
} from "./../../plugin-commons/index.js";
import { createListInputRuleHandler } from "../list-input-rule-handler.js";
import styles from "../statics/styles-scss.js";
const checkboxInputRegex = /^\s*(\[\]|\[ \])\s$/;
const checkboxList = createRicosNodeExtension({
  groups: ["no-trailing-node"],
  type: "html-node",
  name: Node_Type.CHECKBOX_LIST,
  createExtensionConfig() {
    return {
      name: this.name,
      // Must be higher than bulletedList (default 100) so `<ul data-list-style="checkbox">`
      // is parsed as a checkbox list before a regular bullet list can claim it.
      // Must stay BELOW paragraph's priority (1000) so paragraph remains the
      // `defaultType` for the `block+` content expression. If checkbox-list ties
      // with paragraph, it can become the default block type, which causes
      // `normalizeNodes` (used by `toggleLists`) to try retyping textblocks into
      // CHECKBOX_LIST and throw "Invalid content for node type CHECKBOX_LIST".
      priority: 200,
      addOptions() {
        return {
          HTMLAttributes: { class: styles.checkboxList },
          itemTypeName: Node_Type.LIST_ITEM
        };
      },
      group: "block list listItemSecondChildContent smartBlockCellContent layoutCellContent collapsibleListItemTitleContent collapsibleListItemBodyContent tableCellContent",
      content() {
        return "LIST_ITEM+";
      },
      parseHTML() {
        return [
          {
            tag: "ul",
            getAttrs: (element) => isCheckboxListElement(element) ? {} : false
          }
        ];
      },
      addAttributes() {
        return {
          indentation: { default: 0 },
          offset: { default: 0 }
        };
      },
      renderHTML({ HTMLAttributes }) {
        return withBreakoutContainer([
          "ul",
          mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
            "data-list-style": CHECKBOX_LIST_STYLE_ATTRIBUTE_VALUE
          }),
          0
        ]);
      },
      addCommands() {
        return {
          toggleCheckboxList: () => ({ commands }) => commands.toggleLists(this.name, this.options.itemTypeName),
          toggleOffCheckboxList: () => ({ state, commands }) => {
            const isActive = isNodeActive(state, getNodeType(this.name, state.schema));
            return isActive ? commands.toggleCheckboxList() : true;
          }
        };
      },
      addInputRules() {
        return [
          new InputRule({
            find: checkboxInputRegex,
            handler: createListInputRuleHandler({
              type: this.type,
              getItemAttributes: { checked: false }
            })
          })
        ];
      }
    };
  }
});
export {
  checkboxList
};
//# sourceMappingURL=extension.js.map
