"use strict";

exports.__esModule = true;
exports.EditableTableContextMenu = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/EditableTable/EditableTableContextMenu.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const MENU_WIDTH = 230;
const ITEM_HEIGHT = 36;
const DIVIDER_HEIGHT = 9;
const PADDING = 16;
const OFFSET_X = 4;
const OFFSET_Y = 4;
function _EditableTableContextMenu({
  state,
  position,
  cell,
  menuRef,
  onClose
}) {
  const {
    cellInteraction,
    clipboard,
    addItemDisabled,
    updateItemDisabled,
    removeItemDisabled
  } = state;
  const handleCopy = (0, _react.useCallback)(async () => {
    const tsv = clipboard.copy();
    try {
      await navigator.clipboard.writeText(tsv);
    } catch {}
    onClose();
  }, [clipboard, onClose]);
  const handleCut = (0, _react.useCallback)(async () => {
    const tsv = clipboard.cut();
    try {
      await navigator.clipboard.writeText(tsv);
    } catch {}
    onClose();
  }, [clipboard, onClose]);
  const handlePaste = (0, _react.useCallback)(async () => {
    if (!cellInteraction.focusedCell) {
      return;
    }
    try {
      const rawData = await navigator.clipboard.readText();
      if (!rawData) {
        onClose();
        return;
      }
      clipboard.paste(rawData);
    } catch {}
    onClose();
  }, [cellInteraction, clipboard, onClose]);
  const handleClear = (0, _react.useCallback)(() => {
    state.clearCells(cellInteraction.selectedCellsList);
    onClose();
  }, [state, cellInteraction, onClose]);
  if (!position || !cell) {
    return null;
  }
  const selectedCells = cellInteraction.selectedCellsList;
  const selectedCount = selectedCells.length;
  const selectedRowKeys = [...new Set(selectedCells.map(c => c.rowKey))];
  const hasMultipleRows = selectedRowKeys.length > 1;
  const options = [];
  options.push((0, _designSystem.listItemActionBuilder)({
    id: 'copy',
    title: `Copy`,
    disabled: selectedCount === 0
  }), (0, _designSystem.listItemActionBuilder)({
    id: 'cut',
    title: `Cut`,
    disabled: selectedCount === 0 || !!updateItemDisabled
  }), (0, _designSystem.listItemActionBuilder)({
    id: 'paste',
    title: `Paste`,
    disabled: !!updateItemDisabled
  }), (0, _designSystem.listItemActionBuilder)({
    id: 'clear',
    title: selectedCount > 1 ? `Clear ${selectedCount} Cells` : 'Clear Cell',
    disabled: selectedCount === 0 || !!updateItemDisabled
  }));
  const hasRowOps = !addItemDisabled || !removeItemDisabled;
  if (hasRowOps) {
    options.push({
      id: 'divider-1',
      value: '-'
    });
  }
  if (!addItemDisabled) {
    options.push((0, _designSystem.listItemActionBuilder)({
      id: 'add-above',
      title: 'Add Row Above',
      prefixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.AddSmall, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 123,
          columnNumber: 21
        }
      })
    }), (0, _designSystem.listItemActionBuilder)({
      id: 'add-below',
      title: 'Add Row Below',
      prefixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.AddSmall, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 128,
          columnNumber: 21
        }
      })
    }), (0, _designSystem.listItemActionBuilder)({
      id: 'duplicate',
      title: 'Duplicate Row',
      prefixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.DuplicateSmall, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 133,
          columnNumber: 21
        }
      })
    }));
  }
  if (!removeItemDisabled) {
    if (!addItemDisabled) {
      options.push({
        id: 'divider-2',
        value: '-'
      });
    }
    options.push((0, _designSystem.listItemActionBuilder)({
      id: 'delete',
      title: hasMultipleRows ? `Delete ${selectedRowKeys.length} Rows` : 'Delete Row',
      prefixIcon: /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.DeleteSmall, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 148,
          columnNumber: 21
        }
      }),
      skin: 'destructive'
    }));
  }
  const handleSelect = option => {
    switch (option.id) {
      case 'copy':
        handleCopy();
        break;
      case 'cut':
        handleCut();
        break;
      case 'paste':
        handlePaste();
        break;
      case 'clear':
        handleClear();
        break;
      case 'add-above':
        {
          const rowIdx = state.keyedItems.findIndex(ki => ki.key === cell.rowKey);
          const prevKey = rowIdx > 0 ? state.keyedItems[rowIdx - 1].key : undefined;
          state.addItem(prevKey);
          onClose();
          break;
        }
      case 'add-below':
        state.addItem(cell.rowKey);
        onClose();
        break;
      case 'duplicate':
        state.duplicateItem(cell.rowKey);
        onClose();
        break;
      case 'delete':
        state.confirmDeleteItems(hasMultipleRows ? selectedRowKeys : [cell.rowKey]);
        onClose();
        break;
    }
  };
  const dividerCount = options.filter(o => 'value' in o && o.value === '-').length;
  const itemCount = options.length - dividerCount;
  const estimatedHeight = itemCount * ITEM_HEIGHT + dividerCount * DIVIDER_HEIGHT + PADDING;
  const x = position.x + OFFSET_X + MENU_WIDTH > window.innerWidth ? position.x - MENU_WIDTH : position.x + OFFSET_X;
  const y = position.y + OFFSET_Y + estimatedHeight > window.innerHeight ? Math.max(4, position.y - estimatedHeight) : position.y + OFFSET_Y;
  return /*#__PURE__*/(0, _reactDom.createPortal)(/*#__PURE__*/_react.default.createElement("div", {
    ref: menuRef,
    style: {
      position: 'fixed',
      left: x,
      top: y,
      zIndex: 1000,
      width: MENU_WIDTH
    },
    onMouseDown: e => e.preventDefault(),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 212,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.DropdownLayout, {
    dataHook: "editable-table-context-menu",
    visible: true,
    inContainer: true,
    options: options,
    onSelect: handleSelect,
    onClose: onClose,
    maxHeightPixels: 400,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 223,
      columnNumber: 7
    }
  })), document.body);
}
const EditableTableContextMenu = exports.EditableTableContextMenu = (0, _mobxReactLite.observer)(_EditableTableContextMenu);
//# sourceMappingURL=EditableTableContextMenu.js.map