"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useContextMenu = void 0;
require("antd/es/menu/style/css");
var _menu = _interopRequireDefault(require("antd/es/menu"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _lodash = require("lodash");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
var useContextMenu = exports.useContextMenu = function useContextMenu(apiRef, getCustomContextMenuItems, onCellContextMenu, suppressContextMenu) {
  var _useState = (0, _react.useState)([]),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    contextMenuParams = _useState2[0],
    setContextMenuParams = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    contextMenuVisible = _useState4[0],
    setContextMenuVisible = _useState4[1];
  //rowClicked
  var getCellContextMenu = (0, _react.useCallback)(function (params) {
    if (params.event.which === 3 && !suppressContextMenu) {
      setContextMenuParams(params);
      setContextMenuVisible(true);
      params.event.preventDefault(); // 阻止默认上下文菜单
    }
    onCellContextMenu && onCellContextMenu(params);
  }, [suppressContextMenu]);
  var onVisibleChange = (0, _react.useCallback)(function (visible) {
    setContextMenuVisible(visible);
  }, []);
  //转数据兼容新老写法
  var transData = function transData(data) {
    return (0, _lodash.map)(data, function (item) {
      var label = (item === null || item === void 0 ? void 0 : item.label) || (item === null || item === void 0 ? void 0 : item.name);
      return item == 'separator' ? {
        type: 'divider'
      } : Object.assign(Object.assign({}, item), {
        key: (item === null || item === void 0 ? void 0 : item.key) || (item === null || item === void 0 ? void 0 : item.name),
        label: (item === null || item === void 0 ? void 0 : item.menuItem) ? /*#__PURE__*/(0, _react.createElement)(item === null || item === void 0 ? void 0 : item.menuItem, item === null || item === void 0 ? void 0 : item.menuItemParams) : /*#__PURE__*/_react.default.createElement("div", {
          style: {
            display: 'flex',
            justifyContent: 'space-between',
            height: '100%',
            lineHeight: '30px'
          }
        }, /*#__PURE__*/_react.default.createElement("span", null, label), /*#__PURE__*/_react.default.createElement("span", null, item === null || item === void 0 ? void 0 : item.shortcut)),
        children: (item === null || item === void 0 ? void 0 : item.subMenu) ? transData(item === null || item === void 0 ? void 0 : item.subMenu) : null
      });
    });
  };
  var itemsList = (0, _react.useMemo)(function () {
    if (contextMenuParams.length === 0) return [];
    var gridOptions = (0, _lodash.get)(contextMenuParams, 'api.gridOptionsWrapper.gridOptions');
    var contextMenuItems = getCustomContextMenuItems ? getCustomContextMenuItems(contextMenuParams) : [];
    var data = transData(contextMenuItems).length == 0 ? [] : [].concat((0, _toConsumableArray2.default)(transData(contextMenuItems)), [{
      type: 'divider'
    }]);
    var list = [{
      key: 'export',
      label: '全部导出',
      action: function action() {
        apiRef.current.exportDataAsExcel();
      }
    }, {
      key: 'selectexport',
      label: '选中导出',
      action: function action() {
        apiRef.current.exportDataAsExcel({
          onlySelected: true
        });
      }
    }];
    var expandBtnList = (gridOptions === null || gridOptions === void 0 ? void 0 : gridOptions.treeData) ? [{
      type: 'divider'
    }, {
      key: 'expand',
      label: '展开所有',
      action: function action() {
        apiRef.current.expandAll();
      }
    }, {
      key: 'noexpand',
      label: '收起所有',
      action: function action() {
        apiRef.current.forEachNode(function (node) {
          return node.setExpanded(false);
        });
      }
    }] : [];
    return [].concat((0, _toConsumableArray2.default)(data), list, expandBtnList);
  }, [apiRef, getCustomContextMenuItems, contextMenuParams]);
  var menuOnClick = (0, _react.useCallback)(function (action) {
    setContextMenuVisible(false);
    action && action();
  }, []);
  var getContextMenuListDom = (0, _react.useCallback)(function () {
    return itemsList.length > 0 ? /*#__PURE__*/_react.default.createElement(_menu.default, {
      className: 'menuClassName'
    }, itemsList.map(function (item, index) {
      return (item === null || item === void 0 ? void 0 : item.type) === 'divider' ? /*#__PURE__*/_react.default.createElement(_menu.default.Divider, {
        key: index
      }) : /*#__PURE__*/_react.default.createElement(_menu.default.Item, {
        key: index,
        disabled: item === null || item === void 0 ? void 0 : item.disabled,
        onClick: function onClick() {
          return menuOnClick(item === null || item === void 0 ? void 0 : item.action);
        }
      }, item === null || item === void 0 ? void 0 : item.label);
    })) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
  }, [itemsList]);
  (0, _react.useEffect)(function () {
    // 右击其他地方不显示上下文菜单
    var timeoutId = setTimeout(function () {
      document.addEventListener('contextmenu', function (event) {
        var dom = event.target;
        if (!dom.classList.contains("ag-cell")) {
          setContextMenuParams([]);
          setContextMenuVisible(false);
        }
      });
    }, 300);
    return function () {
      clearTimeout(timeoutId);
    };
  }, []);
  return {
    getCellContextMenu: getCellContextMenu,
    contextMenuVisible: contextMenuVisible,
    onVisibleChange: onVisibleChange,
    contextMenuListDom: getContextMenuListDom()
  };
};