'use strict';

const index$1 = require('../../../../../../../node_modules/.pnpm/prosemirror-state@1.4.4/node_modules/prosemirror-state/dist/index.cjs');
const utils = require('../../utils.cjs');
const index = require('../../../../../../../node_modules/.pnpm/@tiptap_core@3.20.0_@tiptap_pm@3.15.3/node_modules/@tiptap/core/dist/index.cjs');

const FileHandlePlugin = (options) => {
  const {
    key,
    editor,
    onPaste,
    onDrop,
    onValidationError,
    allowedMimeTypes,
    maxFileSize
  } = options;
  return new index$1.Plugin({
    key: key || new index$1.PluginKey("fileHandler"),
    props: {
      handleDrop(view, event) {
        const { dataTransfer } = event;
        if (!dataTransfer?.files.length) {
          return false;
        }
        event.preventDefault();
        event.stopPropagation();
        const pos = view.posAtCoords({
          left: event.clientX,
          top: event.clientY
        });
        const [validFiles, errors] = utils.filterFiles(
          Array.from(dataTransfer.files),
          {
            allowedMimeTypes,
            maxFileSize,
            allowBase64: options.allowBase64
          }
        );
        if (errors.length > 0 && onValidationError) {
          onValidationError(errors);
        }
        if (validFiles.length > 0 && onDrop) {
          onDrop(editor, validFiles, pos?.pos ?? 0);
        }
        return true;
      },
      handlePaste(_, event) {
        const { clipboardData } = event;
        if (!clipboardData?.files.length) {
          return false;
        }
        event.preventDefault();
        event.stopPropagation();
        const [validFiles, errors] = utils.filterFiles(
          Array.from(clipboardData.files),
          {
            allowedMimeTypes,
            maxFileSize,
            allowBase64: options.allowBase64
          }
        );
        const html = clipboardData.getData("text/html");
        if (errors.length > 0 && onValidationError) {
          onValidationError(errors);
        }
        if (validFiles.length > 0 && onPaste) {
          onPaste(editor, validFiles, html);
        }
        return true;
      }
    }
  });
};
const FileHandler = index.Extension.create({
  name: "fileHandler",
  addOptions() {
    return {
      allowBase64: false,
      allowedMimeTypes: [],
      maxFileSize: 0
    };
  },
  addProseMirrorPlugins() {
    return [
      FileHandlePlugin({
        key: new index$1.PluginKey(this.name),
        editor: this.editor,
        ...this.options
      })
    ];
  }
});

exports.FileHandler = FileHandler;
