import { getExtensionField } from "./helpers/get-extension-field.js";
import { callOrReturn } from "./utilities/call-or-return.js";
import { mergeDeep } from "./utilities/merge-deep.js";
class Mark {
  constructor(config = {}) {
    this.type = "mark";
    this.name = "mark";
    this.parent = null;
    this.child = null;
    this.config = {
      name: this.name,
      defaultOptions: {}
    };
    this.config = {
      ...this.config,
      ...config
    };
    this.name = this.config.name;
    if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
      console.warn(
        `[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`
      );
    }
    this.options = this.config.defaultOptions;
    if (this.config.addOptions) {
      this.options = callOrReturn(
        getExtensionField(this, "addOptions", {
          name: this.name
        })
      );
    }
    this.storage = callOrReturn(
      getExtensionField(this, "addStorage", {
        name: this.name,
        options: this.options
      })
    ) || {};
  }
  static create(config = {}) {
    return new Mark(config);
  }
  configure(options = {}) {
    const extension = this.extend({
      ...this.config,
      addOptions: () => {
        return mergeDeep(this.options, options);
      }
    });
    extension.name = this.name;
    extension.parent = this.parent;
    return extension;
  }
  extend(extendedConfig = {}) {
    const extension = new Mark(extendedConfig);
    extension.parent = this;
    this.child = extension;
    extension.name = extendedConfig.name ? extendedConfig.name : extension.parent.name;
    if (extendedConfig.defaultOptions && Object.keys(extendedConfig.defaultOptions).length > 0) {
      console.warn(
        `[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`
      );
    }
    extension.options = callOrReturn(
      getExtensionField(extension, "addOptions", {
        name: extension.name
      })
    );
    extension.storage = callOrReturn(
      getExtensionField(extension, "addStorage", {
        name: extension.name,
        options: extension.options
      })
    );
    return extension;
  }
  static handleExit({ editor, mark }) {
    const { tr } = editor.state;
    const currentPos = editor.state.selection.$from;
    const isAtEnd = currentPos.pos === currentPos.end();
    if (isAtEnd) {
      const currentMarks = currentPos.marks();
      const isInMark = !!currentMarks.find((m) => m?.type.name === mark.name);
      if (!isInMark) {
        return false;
      }
      const removeMark = currentMarks.find((m) => m?.type.name === mark.name);
      if (removeMark) {
        tr.removeStoredMark(removeMark);
      }
      tr.insertText(" ", currentPos.pos);
      editor.view.dispatch(tr);
      return true;
    }
    return false;
  }
}
export {
  Mark
};
//# sourceMappingURL=mark.js.map
