"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const useLayerActions = require('../../../../lib/ui-builder/hooks/use-layer-actions.cjs');
const editorStore = require('../../../../lib/ui-builder/store/editor-store.cjs');
const useKeyboardShortcuts = require('../../../../hooks/use-keyboard-shortcuts.cjs');
const shortcutRegistry = require('../../../../lib/ui-builder/shortcuts/shortcut-registry.cjs');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }

const React__default = /*#__PURE__*/_interopDefaultCompat(React);

const LayerContextMenu = ({
  layerId,
  isSelected,
  onSelect,
  children,
  className,
  style
}) => {
  const openContextMenu = editorStore.useEditorStore((state) => state.openContextMenu);
  const {
    handleCopy,
    handleCut,
    handlePaste,
    handleDelete,
    handleDuplicate,
    getCanPaste
  } = useLayerActions.useGlobalLayerActions(layerId);
  const handleContextMenu = React.useCallback(
    (e) => {
      e.preventDefault();
      e.stopPropagation();
      if (!isSelected) {
        onSelect(layerId);
      }
      openContextMenu(e.clientX, e.clientY, layerId);
    },
    [isSelected, layerId, onSelect, openContextMenu]
  );
  const isInputFocused = React.useCallback(() => {
    const activeElement = document.activeElement;
    if (!activeElement) return false;
    const tagName = activeElement.tagName.toLowerCase();
    const isContentEditable = activeElement.isContentEditable;
    return tagName === "input" || tagName === "textarea" || isContentEditable;
  }, []);
  const keyCombinations = React.useMemo(() => {
    if (!isSelected) return [];
    return [
      {
        keys: shortcutRegistry.SHORTCUTS.copy.keys,
        key: shortcutRegistry.SHORTCUTS.copy.key,
        preventDefault: false,
        handler: (e) => {
          if (isInputFocused()) return;
          e.preventDefault();
          handleCopy();
        }
      },
      {
        keys: shortcutRegistry.SHORTCUTS.cut.keys,
        key: shortcutRegistry.SHORTCUTS.cut.key,
        preventDefault: false,
        handler: (e) => {
          if (isInputFocused()) return;
          e.preventDefault();
          handleCut();
        }
      },
      {
        keys: shortcutRegistry.SHORTCUTS.paste.keys,
        key: shortcutRegistry.SHORTCUTS.paste.key,
        preventDefault: false,
        handler: (e) => {
          if (isInputFocused()) return;
          if (!getCanPaste()) return;
          e.preventDefault();
          handlePaste();
        }
      },
      {
        keys: shortcutRegistry.SHORTCUTS.duplicate.keys,
        key: shortcutRegistry.SHORTCUTS.duplicate.key,
        preventDefault: false,
        handler: (e) => {
          if (isInputFocused()) return;
          e.preventDefault();
          handleDuplicate();
        }
      },
      {
        keys: shortcutRegistry.SHORTCUTS.delete.keys,
        key: shortcutRegistry.SHORTCUTS.delete.key,
        preventDefault: false,
        handler: (e) => {
          if (isInputFocused()) return;
          e.preventDefault();
          handleDelete();
        }
      }
    ];
  }, [isSelected, handleCopy, handleCut, handlePaste, handleDuplicate, handleDelete, getCanPaste, isInputFocused]);
  useKeyboardShortcuts.useKeyboardShortcuts(keyCombinations);
  if (React__default.isValidElement(children)) {
    return React__default.cloneElement(children, {
      onContextMenu: handleContextMenu,
      className: className ? `${children.props.className || ""} ${className}`.trim() : children.props.className,
      style: style ? { ...children.props.style, ...style } : children.props.style
    });
  }
  return /* @__PURE__ */ jsxRuntime.jsx(
    "span",
    {
      onContextMenu: handleContextMenu,
      className,
      style,
      children
    }
  );
};

exports.LayerContextMenu = LayerContextMenu;
