import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { __decorate } from "tslib";
import React, { cloneElement, Component } from 'react';
import { observer } from 'mobx-react';
import noop from 'lodash/noop';
import { pxToRem } from '../../../../es/_util/UnitConvertor';
import { getProPrefixCls as getProPrefixClsDefault } from '../../../../es/configure/utils';
import Button from '../../button/Button';
import { ButtonColor, FuncType } from '../../button/enum';
import Modal from '../../modal';
import Form from '../../form/Form';
import Icon from '../../icon';
import { $l } from '../../locale-context';
import autobind from '../../_util/autobind';
import TableButtons from './TableButtons';
import TableContext from '../TableContext';

var TableToolBar = /*#__PURE__*/function (_Component) {
  _inherits(TableToolBar, _Component);

  var _super = _createSuper(TableToolBar);

  function TableToolBar() {
    _classCallCheck(this, TableToolBar);

    return _super.apply(this, arguments);
  }

  _createClass(TableToolBar, [{
    key: "prefixCls",
    get: function get() {
      var prefixCls = this.props.prefixCls;
      var _this$context$tableSt = this.context.tableStore.getProPrefixCls,
          getProPrefixCls = _this$context$tableSt === void 0 ? getProPrefixClsDefault : _this$context$tableSt;
      return getProPrefixCls('table', prefixCls);
    }
  }, {
    key: "handleFieldEnter",
    value: function handleFieldEnter() {
      this.handleQuery();

      if (this.modal) {
        this.modal.close();
      }
    }
  }, {
    key: "handleQueryReset",
    value: function handleQueryReset() {
      var _this$props = this.props,
          queryDataSet = _this$props.queryDataSet,
          _this$props$onReset = _this$props.onReset,
          onReset = _this$props$onReset === void 0 ? noop : _this$props$onReset;

      if (queryDataSet) {
        var current = queryDataSet.current;

        if (current) {
          current.reset();
          onReset();
        }

        this.handleQuery(true);
      }
    }
  }, {
    key: "handleQuery",
    value: function () {
      var _handleQuery = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(collapse) {
        var _this$props2, dataSet, queryDataSet, _this$props2$onQuery, onQuery;

        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _this$props2 = this.props, dataSet = _this$props2.dataSet, queryDataSet = _this$props2.queryDataSet, _this$props2$onQuery = _this$props2.onQuery, onQuery = _this$props2$onQuery === void 0 ? noop : _this$props2$onQuery;
                _context.t0 = queryDataSet;

                if (!_context.t0) {
                  _context.next = 6;
                  break;
                }

                _context.next = 5;
                return queryDataSet.validate();

              case 5:
                _context.t0 = _context.sent;

              case 6:
                if (!_context.t0) {
                  _context.next = 9;
                  break;
                }

                dataSet.query();

                if (!collapse) {
                  onQuery();
                }

              case 9:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function handleQuery(_x) {
        return _handleQuery.apply(this, arguments);
      }

      return handleQuery;
    }()
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.modal) {
        this.modal.close(true);
      }
    }
  }, {
    key: "getQueryBar",
    value: function getQueryBar() {
      var _this = this;

      var _this$props3 = this.props,
          queryFieldsLimit = _this$props3.queryFieldsLimit,
          queryFields = _this$props3.queryFields,
          queryDataSet = _this$props3.queryDataSet;
      var prefixCls = this.prefixCls;

      if (queryDataSet && queryFields.length) {
        var currentFields = this.createFields(queryFields.slice(0, queryFieldsLimit), queryDataSet, false);
        var moreFields = this.createFields(queryFields.slice(queryFieldsLimit), queryDataSet, true);
        var more;
        var dirtyInfo;

        if (moreFields.length) {
          more = this.getMoreButton(moreFields);
          dirtyInfo = this.getDirtyInfo(queryDataSet, moreFields);
        }

        return /*#__PURE__*/React.createElement("span", {
          className: "".concat(prefixCls, "-query-bar")
        }, dirtyInfo, currentFields, /*#__PURE__*/React.createElement(Button, {
          color: ButtonColor.primary,
          onClick: function onClick() {
            _this.handleQuery();
          }
        }, $l('Table', 'query_button')), more);
      }
    }
  }, {
    key: "getDirtyInfo",
    value: function getDirtyInfo(dataSet, moreFields) {
      if (dataSet && moreFields.some(function (element) {
        var name = element.props.name;
        var field = dataSet.getField(name);
        return field ? field.isDirty(dataSet.current) : false;
      })) {
        var prefixCls = this.prefixCls;
        return /*#__PURE__*/React.createElement("span", {
          className: "".concat(prefixCls, "-query-bar-dirty-info")
        }, /*#__PURE__*/React.createElement(Icon, {
          type: "info"
        }), /*#__PURE__*/React.createElement("span", null, $l('Table', 'dirty_info')), /*#__PURE__*/React.createElement("a", {
          onClick: this.handleQueryReset
        }, $l('Table', 'restore')));
      }
    }
  }, {
    key: "getMoreButton",
    value: function getMoreButton(moreFields) {
      var _this2 = this;

      return /*#__PURE__*/React.createElement(Button, {
        color: ButtonColor.primary,
        funcType: FuncType.flat,
        onClick: function onClick() {
          return _this2.openMore(moreFields);
        }
      }, $l('Table', 'advanced_search'));
    }
  }, {
    key: "createFields",
    value: function createFields(elements, dataSet, isMore) {
      var _this3 = this;

      return elements.map(function (element, index) {
        var name = element.props.name;
        var props = {
          autoFocus: isMore && index === 0,
          onEnterDown: _this3.handleFieldEnter,
          style: isMore ? undefined : {
            width: pxToRem(130)
          }
        };
        var field = dataSet.getField(name);

        if (field) {
          var label = field.get('label');

          if (label) {
            if (isMore) {
              props.label = label;
            } else {
              props.placeholder = label;
            }
          }
        }

        return /*#__PURE__*/cloneElement(element, props);
      });
    }
  }, {
    key: "openMore",
    value: function openMore(children) {
      this.modal = Modal.open({
        title: $l('Table', 'advanced_search'),
        children: /*#__PURE__*/React.createElement(Form, null, children),
        okText: $l('Table', 'query_button'),
        onOk: this.handleQuery,
        style: {
          width: pxToRem(400)
        },
        drawer: true
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          pagination = _this$props4.pagination,
          buttons = _this$props4.buttons;
      var prefixCls = this.prefixCls;
      return [/*#__PURE__*/React.createElement(TableButtons, {
        key: "toolbar",
        prefixCls: prefixCls,
        buttons: buttons
      }, this.getQueryBar()), pagination];
    }
  }], [{
    key: "contextType",
    get: function get() {
      return TableContext;
    }
  }]);

  return TableToolBar;
}(Component);

TableToolBar.displayName = 'TableToolBar';
TableToolBar.defaultProps = {
  queryFieldsLimit: 1,
  pagination: null
};

__decorate([autobind], TableToolBar.prototype, "handleFieldEnter", null);

__decorate([autobind], TableToolBar.prototype, "handleQueryReset", null);

__decorate([autobind], TableToolBar.prototype, "handleQuery", null);

TableToolBar = __decorate([observer], TableToolBar);
export default TableToolBar;
//# sourceMappingURL=TableToolBar.js.map
