'use strict';

const React = require('react');
const utils = require('../../../utils.cjs');

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);

const useImageActions = ({
  editor,
  node,
  src,
  onViewClick
}) => {
  const isLink = utils.isUrl(src);
  const onView = React__namespace.useCallback(() => {
    onViewClick(true);
  }, [onViewClick]);
  const onDownload = React__namespace.useCallback(() => {
    editor.commands.downloadImage({ src: node.attrs.src, alt: node.attrs.alt });
  }, [editor.commands, node.attrs.alt, node.attrs.src]);
  const onCopy = React__namespace.useCallback(() => {
    editor.commands.copyImage({ src: node.attrs.src });
  }, [editor.commands, node.attrs.src]);
  const onCopyLink = React__namespace.useCallback(() => {
    editor.commands.copyLink({ src: node.attrs.src });
  }, [editor.commands, node.attrs.src]);
  const onRemoveImg = React__namespace.useCallback(() => {
    editor.commands.command(({ tr, dispatch }) => {
      const { selection } = tr;
      const nodeAtSelection = tr.doc.nodeAt(selection.from);
      if (nodeAtSelection && nodeAtSelection.type.name === "image") {
        if (dispatch) {
          tr.deleteSelection();
          return true;
        }
      }
      return false;
    });
  }, [editor.commands]);
  return { isLink, onView, onDownload, onCopy, onCopyLink, onRemoveImg };
};

exports.useImageActions = useImageActions;
