"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.EditableTable = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _patternsFields = require("@wix/patterns-fields");
var _PatternsFieldsProvider = require("../../providers/PatternsFieldsProvider");
var _TableVirtual = require("../Table/TableVirtual");
var _EditableCell = require("./EditableCell");
var _EditableTableContextMenu = require("./EditableTableContextMenu");
var _useEditableTableContextMenu = require("./useEditableTableContextMenu");
var _ActionModal = require("../ActionModal");
var _useEditableTableCollectionSyncProps = require("./useEditableTableCollectionSyncProps");
var _useKeyboardNavigation = require("./useKeyboardNavigation");
var _useClipboardHandler = require("./useClipboardHandler");
var _useClearFocusOnBlur = require("./useClearFocusOnBlur");
var _useMouseSweepSelection = require("./useMouseSweepSelection");
var _AddItemRow = require("./AddItemRow");
var _useAddRow = require("./useAddRow");
var _useHeaderActions = require("./useHeaderActions");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/EditableTable/EditableTable.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); }
function _EditableTable(props) {
  const {
    state,
    columns: editableColumns,
    hideHeaderActions,
    dataHook,
    children,
    ...tableBaseProps
  } = props;
  (0, _useEditableTableCollectionSyncProps.useEditableTableCollectionSyncProps)(props);
  const containerRef = (0, _react.useRef)(null);
  const keyboard = (0, _useKeyboardNavigation.useKeyboardNavigation)(state);
  (0, _useClipboardHandler.useClipboardHandler)(state, containerRef);
  const {
    onFocus,
    onBlur
  } = (0, _useClearFocusOnBlur.useClearFocusOnBlur)(state.cellInteraction);
  (0, _useMouseSweepSelection.useMouseSweepSelection)(state, containerRef);
  const builtInActions = (0, _useHeaderActions.useHeaderActions)(state, editableColumns, hideHeaderActions);
  const {
    showAddItemRow,
    handleAddItem
  } = (0, _useAddRow.useAddRow)(state);
  const tableColumns = (0, _react.useMemo)(() => editableColumns.map(col => ({
    ...col,
    name: col.name ?? (typeof col.title === 'string' ? col.title : col.id),
    titleSuffix: builtInActions[col.id],
    render: (_item, rowNum) => {
      var _state$keyedItems$row;
      const rowKey = (_state$keyedItems$row = state.keyedItems[rowNum]) == null ? void 0 : _state$keyedItems$row.key;
      return rowKey ? /*#__PURE__*/_react.default.createElement(_EditableCell.EditableCell, {
        state: state,
        rowKey: rowKey,
        columnId: col.id,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 89,
          columnNumber: 13
        }
      }) : null;
    }
  })), [editableColumns, state, builtInActions]);
  const contextMenu = (0, _useEditableTableContextMenu.useEditableTableContextMenu)(state.cellInteraction);
  return /*#__PURE__*/_react.default.createElement(_PatternsFieldsProvider.PatternsFieldsProvider, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 99,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    ref: containerRef,
    "data-hook": dataHook,
    tabIndex: 0,
    onKeyDown: keyboard.onKeyDown,
    onFocus: onFocus,
    onBlur: onBlur,
    onContextMenu: contextMenu.handleContextMenu,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 100,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_TableVirtual.TableVirtual, (0, _extends2.default)({}, tableBaseProps, {
    state: state.tableState,
    columns: tableColumns,
    forceResize: true,
    horizontalScroll: true,
    showSelection: true,
    showRowNumbers: true,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 109,
      columnNumber: 9
    }
  }), showAddItemRow && /*#__PURE__*/_react.default.createElement(_AddItemRow.AddItemRow, {
    onAdd: handleAddItem,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 118,
      columnNumber: 30
    }
  }))), /*#__PURE__*/_react.default.createElement(_EditableTableContextMenu.EditableTableContextMenu, {
    state: state,
    position: contextMenu.position,
    cell: contextMenu.cell,
    menuRef: contextMenu.menuRef,
    onClose: contextMenu.handleClose,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 121,
      columnNumber: 7
    }
  }), state.deleteConfirmModal.value && /*#__PURE__*/_react.default.createElement(_ActionModal.MessageActionModal, (0, _extends2.default)({
    state: state.deleteConfirmModal
  }, state.deleteConfirmModal.value, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 129,
      columnNumber: 9
    }
  })), state.fieldActions.confirmModal.value && /*#__PURE__*/_react.default.createElement(_ActionModal.MessageActionModal, (0, _extends2.default)({
    state: state.fieldActions.confirmModal
  }, state.fieldActions.confirmModal.value, {
    primaryButtonProps: {
      ...state.fieldActions.confirmModal.value.primaryButtonProps,
      disabled: state.fieldActions.deleteTask.status.isLoading
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 135,
      columnNumber: 9
    }
  })), children, /*#__PURE__*/_react.default.createElement(_patternsFields.GlobalAudioPlayer, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 145,
      columnNumber: 7
    }
  }));
}
const EditableTable = exports.EditableTable = (0, _mobxReactLite.observer)(_EditableTable);
//# sourceMappingURL=EditableTable.js.map