'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const React = require('react');
const index$1 = require('../../../../../../node_modules/.pnpm/@tiptap_starter-kit@3.15.3/node_modules/@tiptap/starter-kit/dist/index.cjs');
const index = require('../../../../../../node_modules/.pnpm/@tiptap_react@3.15.3_@floating-ui_dom@1.7.4_@tiptap_core@3.20.0_@tiptap_pm@3.15.3__@tip_b42b5b2d59902d8a01cd4cc3e2d5dc64/node_modules/@tiptap/react/dist/index.cjs');
const index$5 = require('../../../../../../node_modules/.pnpm/@tiptap_extension-typography@3.15.3_@tiptap_core@3.20.0_@tiptap_pm@3.15.3_/node_modules/@tiptap/extension-typography/dist/index.cjs');
const index$3 = require('../../../../../../node_modules/.pnpm/@tiptap_extension-text-style@3.15.3_@tiptap_core@3.20.0_@tiptap_pm@3.15.3_/node_modules/@tiptap/extension-text-style/dist/index.cjs');
const index$4 = require('../../../../../../node_modules/.pnpm/@tiptap_extensions@3.15.3_@tiptap_core@3.20.0_@tiptap_pm@3.15.3__@tiptap_pm@3.15.3/node_modules/@tiptap/extensions/dist/index.cjs');
const index$6 = require('../../../../../../node_modules/.pnpm/@tiptap_markdown@3.15.3_@tiptap_core@3.20.0_@tiptap_pm@3.15.3__@tiptap_pm@3.15.3/node_modules/@tiptap/markdown/dist/index.cjs');
const index$7 = require('../../../../../../node_modules/.pnpm/@tiptap_extension-list@3.15.3_@tiptap_core@3.20.0_@tiptap_pm@3.15.3__@tiptap_pm@3.15.3/node_modules/@tiptap/extension-list/dist/index.cjs');
const index$8 = require('../../../../../../node_modules/.pnpm/@tiptap_extension-table@3.15.3_@tiptap_core@3.20.0_@tiptap_pm@3.15.3__@tiptap_pm@3.15.3/node_modules/@tiptap/extension-table/dist/index.cjs');
const codeBlockLowlight = require('../extensions/code-block-lowlight/code-block-lowlight.cjs');
const color = require('../extensions/color/color.cjs');
const horizontalRule = require('../extensions/horizontal-rule/horizontal-rule.cjs');
const image = require('../extensions/image/image.cjs');
const unsetAllMarks = require('../extensions/unset-all-marks/unset-all-marks.cjs');
const resetMarksOnEnter = require('../extensions/reset-marks-on-enter/reset-marks-on-enter.cjs');
const index$2 = require('../extensions/file-handler/index.cjs');
const markdownPaste = require('../extensions/markdown-paste/markdown-paste.cjs');
const utils$1 = require('../../../lib/utils.cjs');
const utils = require('../utils.cjs');
const useThrottle = require('./use-throttle.cjs');
const sonner = require('sonner');

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  const n = Object.create(null);
  if (e) {
    for (const k in e) {
      n[k] = e[k];
    }
  }
  n.default = e;
  return n;
}

const React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

async function fakeuploader(file) {
  await new Promise((resolve) => setTimeout(resolve, 3e3));
  const src = await utils.fileToBase64(file);
  return src;
}
const createExtensions = ({
  placeholder,
  uploader,
  output = "html"
}) => [
  index$1.StarterKit.configure({
    blockquote: { HTMLAttributes: { class: "block-node" } },
    // bold
    bulletList: { HTMLAttributes: { class: "list-node" } },
    code: { HTMLAttributes: { class: "inline", spellcheck: "false" } },
    codeBlock: false,
    // document
    dropcursor: { width: 2, class: "ProseMirror-dropcursor border" },
    // gapcursor
    // hardBreak
    heading: { HTMLAttributes: { class: "heading-node" } },
    // undoRedo
    horizontalRule: false,
    // italic
    // listItem
    // listKeymap
    link: {
      enableClickSelection: true,
      openOnClick: false,
      HTMLAttributes: {
        class: "link"
      }
    },
    orderedList: { HTMLAttributes: { class: "list-node" } },
    paragraph: { HTMLAttributes: { class: "text-node" } }
    // strike
    // text
    // underline
    // trailingNode
  }),
  image.Image.configure({
    allowedMimeTypes: ["image/*"],
    maxFileSize: 5 * 1024 * 1024,
    allowBase64: true,
    uploadFn: async (file) => {
      return uploader ? await uploader(file) : await fakeuploader(file);
    },
    onToggle(editor, files, pos) {
      editor.commands.insertContentAt(
        pos,
        files.map((image) => {
          const blobUrl = URL.createObjectURL(image);
          const id = utils.randomId();
          return {
            type: "image",
            attrs: {
              id,
              src: blobUrl,
              alt: image.name,
              title: image.name,
              fileName: image.name
            }
          };
        })
      );
    },
    onImageRemoved({ id, src }) {
      console.log("Image removed", { id, src });
    },
    onValidationError(errors) {
      errors.forEach((error) => {
        sonner.toast.error("Image validation error", {
          position: "bottom-right",
          description: error.reason
        });
      });
    },
    onActionSuccess({ action }) {
      const mapping = {
        copyImage: "Copy Image",
        copyLink: "Copy Link",
        download: "Download"
      };
      sonner.toast.success(mapping[action], {
        position: "bottom-right",
        description: "Image action success"
      });
    },
    onActionError(error, { action }) {
      const mapping = {
        copyImage: "Copy Image",
        copyLink: "Copy Link",
        download: "Download"
      };
      sonner.toast.error(`Failed to ${mapping[action]}`, {
        position: "bottom-right",
        description: error.message
      });
    }
  }),
  index$2.FileHandler.configure({
    allowBase64: true,
    allowedMimeTypes: ["image/*"],
    maxFileSize: 5 * 1024 * 1024,
    onDrop: (editor, files, pos) => {
      files.forEach(async (file) => {
        const src = await utils.fileToBase64(file);
        editor.commands.insertContentAt(pos, {
          type: "image",
          attrs: { src }
        });
      });
    },
    onPaste: (editor, files) => {
      files.forEach(async (file) => {
        const src = await utils.fileToBase64(file);
        editor.commands.insertContent({
          type: "image",
          attrs: { src }
        });
      });
    },
    onValidationError: (errors) => {
      errors.forEach((error) => {
        sonner.toast.error("Image validation error", {
          position: "bottom-right",
          description: error.reason
        });
      });
    }
  }),
  color.Color,
  index$3.TextStyle,
  index$4.Selection,
  index$5.Typography,
  unsetAllMarks.UnsetAllMarks,
  horizontalRule.HorizontalRule,
  resetMarksOnEnter.ResetMarksOnEnter,
  codeBlockLowlight.CodeBlockLowlight,
  index$4.Placeholder.configure({ placeholder: () => placeholder }),
  // Add MarkdownPaste extension when output is markdown
  ...output === "markdown" ? [
    // Markdown with GFM support for tables, task lists, etc.
    index$6.Markdown.configure({
      markedOptions: {
        gfm: true
      }
    }),
    // Task lists (checkboxes)
    index$7.TaskList.configure({
      HTMLAttributes: { class: "task-list-node" }
    }),
    index$7.TaskItem.configure({
      nested: true
    }),
    // Tables
    index$8.TableKit.configure({
      table: {
        resizable: true,
        HTMLAttributes: { class: "table-node" }
      }
    }),
    markdownPaste.MarkdownPaste
  ] : []
];
const useMinimalTiptapEditor = ({
  value,
  output = "html",
  placeholder = "",
  editorClassName,
  throttleDelay = 0,
  onUpdate,
  onBlur,
  uploader,
  ...props
}) => {
  const throttledSetValue = useThrottle.useThrottle(
    (value2) => onUpdate?.(value2),
    throttleDelay
  );
  const handleUpdate = React__namespace.useCallback(
    (editor2) => throttledSetValue(utils.getOutput(editor2, output)),
    [output, throttledSetValue]
  );
  const handleCreate = React__namespace.useCallback(
    (editor2) => {
      if (value && editor2.isEmpty) {
        editor2.commands.setContent(value, {
          contentType: output === "markdown" ? "markdown" : void 0
        });
      }
    },
    [value, output]
  );
  const handleBlur = React__namespace.useCallback(
    (editor2) => onBlur?.(utils.getOutput(editor2, output)),
    [output, onBlur]
  );
  const editor = index.useEditor({
    immediatelyRender: false,
    extensions: createExtensions({ placeholder, uploader, output }),
    editorProps: {
      attributes: {
        autocomplete: "off",
        autocorrect: "off",
        autocapitalize: "off",
        class: utils$1.cn("focus:outline-hidden", editorClassName)
      }
    },
    onUpdate: ({ editor: editor2 }) => handleUpdate(editor2),
    onCreate: ({ editor: editor2 }) => handleCreate(editor2),
    onBlur: ({ editor: editor2 }) => handleBlur(editor2),
    ...props
  });
  return editor;
};

exports.default = useMinimalTiptapEditor;
exports.useMinimalTiptapEditor = useMinimalTiptapEditor;
