"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _utils = require("@teamix/utils");

var _field = require("../../../field");

var _actions = require("../../../actions");

var _form = require("../../../form");

var _ToolBar = _interopRequireDefault(require("../ToolBar"));

var _QuickAction = _interopRequireDefault(require("../QuickAction"));

require("./index.scss");

var _excluded = ["header", "mainAction", "extra", "actionRef", "dataFilter", "dataFilterFormRef", "afterDataFilter", "rowSelection", "toolBar", "dataTeamixSpm"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var cls = (0, _utils.usePrefixCls)('teamix-pro-table-layout');

var Layout = function Layout(props) {
  var header = props.header,
      mainAction = props.mainAction,
      extra = props.extra,
      actionRef = props.actionRef,
      dataFilter = props.dataFilter,
      dataFilterFormRef = props.dataFilterFormRef,
      afterDataFilter = props.afterDataFilter,
      rowSelection = props.rowSelection,
      toolBar = props.toolBar,
      dataTeamixSpm = props.dataTeamixSpm,
      otherProps = _objectWithoutProperties(props, _excluded); // 渲染主操作区


  var renderMainAction = function renderMainAction() {
    var _mainAction$actions;

    if (_typeof(mainAction) === 'object' && (mainAction === null || mainAction === void 0 ? void 0 : (_mainAction$actions = mainAction.actions) === null || _mainAction$actions === void 0 ? void 0 : _mainAction$actions.length) > 0) {
      // 判断 actions 是否 visible 都为 false，是的话也不渲染
      var isEmptyActions = mainAction === null || mainAction === void 0 ? void 0 : mainAction.actions.every(function (action) {
        return (action === null || action === void 0 ? void 0 : action.visible) === false;
      });

      if (isEmptyActions) {
        return;
      } // 默认 context


      var defaultContext = {
        action: actionRef.current,
        rowSelection: rowSelection
      };
      return /*#__PURE__*/_react.default.createElement("div", {
        className: cls('main-action')
      }, /*#__PURE__*/_react.default.createElement(_actions.ProActionGroup, _objectSpread(_objectSpread({}, mainAction), {}, {
        context: _objectSpread(_objectSpread({}, defaultContext), mainAction.context),
        "data-teamix-spm": dataTeamixSpm ? "".concat(dataTeamixSpm, "-mainAction") : undefined
      })));
    } else return mainAction;
  }; // 渲染 addonAfter


  var renderAddonAfter = function renderAddonAfter() {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: cls('addon-after-wrapper')
    }, extra && !header && /*#__PURE__*/_react.default.createElement(_QuickAction.default, {
      actionRef: actionRef,
      quickAction: extra,
      rowSelection: rowSelection
    }), /*#__PURE__*/_react.default.createElement(_ToolBar.default, _objectSpread({
      actionRef: actionRef,
      toolBar: toolBar
    }, otherProps)));
  }; // 渲染新版 QueryFilter


  var renderQueryFilter = function renderQueryFilter() {
    var _dataFilter$schema;

    if (dataFilter === null || dataFilter === void 0 ? void 0 : (_dataFilter$schema = dataFilter.schema) === null || _dataFilter$schema === void 0 ? void 0 : _dataFilter$schema.length) {
      var _dataFilter$schema2;

      if ( /*#__PURE__*/(0, _react.isValidElement)(dataFilter === null || dataFilter === void 0 ? void 0 : dataFilter.content)) {
        // 自定义渲染内容
        return /*#__PURE__*/_react.default.createElement(_form.QueryFilterLayout, _objectSpread({
          onExpand: function onExpand(expand) {
            // 全屏模式下展开收起过滤器需要重新计算tableMaxHeight
            setTimeout(function () {
              var _actionRef$current, _actionRef$current$re;

              // if (actionRef.current?.getState?.().fullScreenState) {
              // 偏移量8像素为面板展开后的下margin
              (_actionRef$current = actionRef.current) === null || _actionRef$current === void 0 ? void 0 : (_actionRef$current$re = _actionRef$current.resetTableMaxBodyHeight) === null || _actionRef$current$re === void 0 ? void 0 : _actionRef$current$re.call(_actionRef$current, expand ? 8 : 0); // }
            });
            (dataFilter === null || dataFilter === void 0 ? void 0 : dataFilter.onExpand) && (dataFilter === null || dataFilter === void 0 ? void 0 : dataFilter.onExpand(expand));
          },
          panelContent: dataFilter.content,
          addonBefore: renderMainAction(),
          addonAfter: renderAddonAfter()
        }, dataFilter));
      } else if ((dataFilter === null || dataFilter === void 0 ? void 0 : (_dataFilter$schema2 = dataFilter.schema) === null || _dataFilter$schema2 === void 0 ? void 0 : _dataFilter$schema2.length) >= 0) {
        return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.QueryFilter, _objectSpread({
          formRef: dataFilterFormRef,
          onExpand: function onExpand(expand) {
            // 全屏模式下展开收起过滤器需要重新计算tableMaxHeight
            setTimeout(function () {
              var _actionRef$current2, _actionRef$current2$r;

              // if (actionRef.current?.getState?.().fullScreenState) {
              // 偏移量8像素为面板展开后的下margin
              (_actionRef$current2 = actionRef.current) === null || _actionRef$current2 === void 0 ? void 0 : (_actionRef$current2$r = _actionRef$current2.resetTableMaxBodyHeight) === null || _actionRef$current2$r === void 0 ? void 0 : _actionRef$current2$r.call(_actionRef$current2, expand ? 8 : 0); // }
            });
            (dataFilter === null || dataFilter === void 0 ? void 0 : dataFilter.onExpand) && (dataFilter === null || dataFilter === void 0 ? void 0 : dataFilter.onExpand(expand));
          },
          addonBefore: renderMainAction(),
          addonAfter: renderAddonAfter()
        }, dataFilter)), afterDataFilter && /*#__PURE__*/_react.default.createElement("div", {
          className: "mb8 ".concat(cls('after-data-filter-wrapper'))
        }, afterDataFilter));
      }
    } else if (mainAction) {
      // 没传 dataFilter 但是传了 mainAction
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.QueryFilterLayout, {
        addonBefore: renderMainAction(),
        addonAfter: renderAddonAfter(),
        children: afterDataFilter ? /*#__PURE__*/_react.default.createElement("div", {
          className: "mb8 ".concat(cls('after-data-filter-wrapper'))
        }, afterDataFilter) : null
      }));
    } else {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.QueryFilterLayout, {
        addonAfter: renderAddonAfter(),
        children: afterDataFilter ? /*#__PURE__*/_react.default.createElement("div", {
          className: "mb8 ".concat(cls('after-data-filter-wrapper'))
        }, afterDataFilter) : null
      }));
    }
  }; // 区域组合渲染


  var renderLayout = function renderLayout() {
    if (header && mainAction) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
        className: cls('wrapper')
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: cls('left')
      }, header && /*#__PURE__*/_react.default.createElement(_field.Header, _objectSpread({}, header))), /*#__PURE__*/_react.default.createElement("div", {
        className: cls('right')
      }, extra && /*#__PURE__*/_react.default.createElement(_QuickAction.default, {
        actionRef: actionRef,
        quickAction: extra,
        rowSelection: rowSelection
      }))), renderQueryFilter());
    } else if (header && !mainAction) {
      var _dataFilter$schema3, _dataFilter$schema4;

      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
        className: cls('wrapper')
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: cls('left')
      }, header && /*#__PURE__*/_react.default.createElement(_field.Header, _objectSpread({}, header))), /*#__PURE__*/_react.default.createElement("div", {
        className: cls('right')
      }, extra && /*#__PURE__*/_react.default.createElement(_QuickAction.default, {
        actionRef: actionRef,
        quickAction: extra,
        rowSelection: rowSelection
      }), !(dataFilter === null || dataFilter === void 0 ? void 0 : (_dataFilter$schema3 = dataFilter.schema) === null || _dataFilter$schema3 === void 0 ? void 0 : _dataFilter$schema3.length) && renderQueryFilter())), (dataFilter === null || dataFilter === void 0 ? void 0 : (_dataFilter$schema4 = dataFilter.schema) === null || _dataFilter$schema4 === void 0 ? void 0 : _dataFilter$schema4.length) && renderQueryFilter());
    } else if (!header && mainAction) {
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderQueryFilter());
    } else if (!header && !mainAction) {
      if (!dataFilterFormRef && !extra) {
        return null;
      }

      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderQueryFilter());
    }
  };

  if ((dataFilter === null || dataFilter === void 0 ? void 0 : dataFilter.schema) || mainAction || header || extra || toolBar !== false) {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: cls()
    }, renderLayout());
  }

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
};

var _default = /*#__PURE__*/_react.default.memo(Layout);

exports.default = _default;