import _objectSpread from "@babel/runtime/helpers/objectSpread2";
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 { __decorate } from "tslib";
import React, { cloneElement, Component } from 'react';
import { observer } from 'mobx-react';
import isFunction from 'lodash/isFunction';
import noop from 'lodash/noop';
import { pxToRem } from '../../../../es/_util/UnitConvertor';
import { getProPrefixCls as getProPrefixClsDefault } from '../../../../es/configure/utils';
import Button from '../../button';
import { ButtonColor, FuncType } from '../../button/enum';
import { $l } from '../../locale-context';
import autobind from '../../_util/autobind';
import TableButtons from './TableButtons';
import FilterSelect from './FilterSelect';
import Modal from '../../modal';
import Form from '../../form/Form';
import { LabelLayout } from '../../form/enum';
import TableContext from '../TableContext';

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

  var _super = _createSuper(TableAdvancedQueryBar);

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

    return _super.apply(this, arguments);
  }

  _createClass(TableAdvancedQueryBar, [{
    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: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.modal) {
        this.modal.close(true);
      }
    }
  }, {
    key: "handleFieldEnter",
    value: function handleFieldEnter() {
      this.handleQuery();

      if (this.modal) {
        this.modal.close();
      }
    }
  }, {
    key: "handleQuery",
    value: function handleQuery() {
      var dataSet = this.props.dataSet;
      dataSet.query();
    }
  }, {
    key: "valueFilter",
    value: function valueFilter(value) {
      var _this$props = this.props,
          queryFields = _this$props.queryFields,
          queryFieldsLimit = _this$props.queryFieldsLimit;
      return queryFields.slice(0, queryFieldsLimit).every(function (element) {
        return element.props.name !== value;
      });
    }
  }, {
    key: "getMoreFieldsButton",
    value: function getMoreFieldsButton(fields) {
      var _this = this;

      if (fields.length) {
        return /*#__PURE__*/React.createElement(Button, {
          funcType: FuncType.raised,
          color: ButtonColor.primary,
          onClick: function onClick() {
            return _this.openMore(fields);
          }
        }, $l('Table', 'advanced_query'));
      }
    }
  }, {
    key: "openMore",
    value: function openMore(children) {
      this.modal = Modal.open({
        title: $l('Table', 'advanced_query'),
        children: /*#__PURE__*/React.createElement(Form, {
          labelLayout: LabelLayout["float"]
        }, children),
        okText: $l('Table', 'query_button'),
        onOk: this.handleQuery,
        style: {
          width: pxToRem(380)
        },
        drawer: true
      });
    }
  }, {
    key: "getResetButton",
    value: function getResetButton() {
      return /*#__PURE__*/React.createElement(Button, {
        funcType: FuncType.raised,
        onClick: this.handleQueryReset
      }, $l('Table', 'reset_button'));
    }
  }, {
    key: "getQueryBar",
    value: function getQueryBar() {
      var _this$props2 = this.props,
          queryFieldsLimit = _this$props2.queryFieldsLimit,
          queryFields = _this$props2.queryFields,
          buttons = _this$props2.buttons,
          queryDataSet = _this$props2.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 moreFieldsButton = this.getMoreFieldsButton(moreFields);
        return /*#__PURE__*/React.createElement("div", {
          key: "toolbar",
          className: "".concat(prefixCls, "-advanced-query-bar")
        }, currentFields, /*#__PURE__*/React.createElement("span", {
          className: "".concat(prefixCls, "-advanced-query-bar-button")
        }, this.getResetButton(), moreFieldsButton, buttons));
      }
    }
  }, {
    key: "createFields",
    value: function createFields(elements, dataSet, isMore) {
      var _this2 = this;

      return elements.map(function (element) {
        var _element$props = element.props,
            name = _element$props.name,
            style = _element$props.style,
            onEnterDown = _element$props.onEnterDown;
        var newStyle = {};

        if (!isMore) {
          newStyle.width = pxToRem(260);
          newStyle.marginRight = pxToRem(10);
        }

        var props = {
          onEnterDown: isFunction(onEnterDown) ? onEnterDown : _this2.handleFieldEnter,
          labelLayout: LabelLayout["float"],
          style: _objectSpread({
            marginRight: !isMore ? pxToRem(10) : 0
          }, style)
        };
        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: "handleQueryReset",
    value: function handleQueryReset() {
      var _this$props3 = this.props,
          queryDataSet = _this$props3.queryDataSet,
          dataSet = _this$props3.dataSet,
          queryFields = _this$props3.queryFields,
          _this$props3$onReset = _this$props3.onReset,
          onReset = _this$props3$onReset === void 0 ? noop : _this$props3$onReset;

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

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

        dataSet.fireEvent('queryBarReset', {
          dataSet: dataSet,
          queryFields: queryFields
        });
        this.handleQuery();
      }
    }
  }, {
    key: "getFilterSelect",
    value: function getFilterSelect() {
      var _this$props4 = this.props,
          dataSet = _this$props4.dataSet,
          queryDataSet = _this$props4.queryDataSet,
          _this$props4$onQuery = _this$props4.onQuery,
          onQuery = _this$props4$onQuery === void 0 ? noop : _this$props4$onQuery;
      var prefixCls = this.prefixCls;
      return /*#__PURE__*/React.createElement(FilterSelect, {
        key: "filter",
        prefixCls: "".concat(prefixCls, "-filter-select"),
        className: "".concat(prefixCls, "-advanced-query-bar-options"),
        optionDataSet: dataSet,
        queryDataSet: queryDataSet,
        prefix: "".concat($l('Table', 'advanced_query_conditions'), ":"),
        editable: false,
        filter: this.valueFilter,
        hiddenIfNone: true,
        onQuery: onQuery
      });
    }
  }, {
    key: "render",
    value: function render() {
      var buttons = this.props.buttons;
      var prefixCls = this.prefixCls;
      var queryBar = this.getQueryBar();

      if (queryBar) {
        return [queryBar, this.getFilterSelect()];
      }

      return /*#__PURE__*/React.createElement(TableButtons, {
        key: "toolbar",
        prefixCls: prefixCls,
        buttons: buttons
      });
    }
  }], [{
    key: "contextType",
    get: function get() {
      return TableContext;
    }
  }]);

  return TableAdvancedQueryBar;
}(Component);

TableAdvancedQueryBar.defaultProps = {
  queryFieldsLimit: 1
};

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

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

__decorate([autobind], TableAdvancedQueryBar.prototype, "valueFilter", null);

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

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