import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React, { Component } from "react";
import merge from "lodash/merge";
import { connect } from "react-redux";
import context from "../stores/context";
import PropTypes from "prop-types";
import * as actions from "../actions";
import { immutableEqual } from "../utils/stuff";
import { useOnPropsChanged, liteShouldComponentUpdate, bindActionCreators } from "../utils/reactUtils";
var Query = /*#__PURE__*/function (_Component) {
  function Query(props) {
    var _this;
    _classCallCheck(this, Query);
    _this = _callSuper(this, Query, [props]);
    _this.shouldComponentUpdate = liteShouldComponentUpdate(_this, {
      tree: function tree(nextValue) {
        if (nextValue === _this.oldValidatedTree && _this.oldValidatedTree === _this.validatedTree) {
          // Got value dispatched from QueryContainer
          // Ignore, because we've just rendered it
          return false;
        }
        return true;
      }
    });
    useOnPropsChanged(_this);
    _this._updateActions(props);

    // For preventive validation (tree and config consistency)
    // When config has changed from QueryContainer, 
    //  but new dispatched validated tree value is not in redux store yet (tree prop is old)
    _this.validatedTree = props.getMemoizedTree(props.config, props.tree, undefined, props.sanitizeTree);
    _this.oldValidatedTree = _this.validatedTree;
    var basicConfig = props.getBasicConfig(props.config);
    props.onInit && props.onInit(_this.validatedTree, basicConfig, undefined, _this.actions);
    return _this;
  }
  _inherits(Query, _Component);
  return _createClass(Query, [{
    key: "_updateActions",
    value: function _updateActions(props) {
      var config = props.config,
        dispatch = props.dispatch;
      this.actions = bindActionCreators(_objectSpread(_objectSpread(_objectSpread({}, actions.tree), actions.group), actions.rule), config, dispatch);
    }
  }, {
    key: "onPropsChanged",
    value: function onPropsChanged(nextProps) {
      var onChange = nextProps.onChange;
      var oldConfig = this.props.config;
      var newTree = nextProps.tree;
      var oldTree = this.props.tree;
      var newConfig = nextProps.config;
      this.oldValidatedTree = this.validatedTree;
      this.validatedTree = newTree;
      if (oldConfig !== newConfig) {
        this._updateActions(nextProps);
        this.validatedTree = nextProps.getMemoizedTree(newConfig, newTree, oldConfig);
      }
      var validatedTreeChanged = !immutableEqual(this.validatedTree, this.oldValidatedTree);
      if (validatedTreeChanged) {
        var newBasicConfig = nextProps.getBasicConfig(newConfig);
        onChange && onChange(this.validatedTree, newBasicConfig, nextProps.__lastAction, this.actions);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        config = _this$props.config,
        renderBuilder = _this$props.renderBuilder,
        dispatch = _this$props.dispatch;
      var builderProps = {
        tree: this.validatedTree,
        actions: this.actions,
        config: config,
        dispatch: dispatch
      };
      return renderBuilder(builderProps);
    }
  }]);
}(Component);
Query.propTypes = {
  config: PropTypes.object.isRequired,
  onChange: PropTypes.func,
  onInit: PropTypes.func,
  renderBuilder: PropTypes.func,
  tree: PropTypes.any //instanceOf(Immutable.Map)
  //dispatch: PropTypes.func.isRequired,
  //__lastAction
  //getMemoizedTree: PropTypes.func.isRequired,
  //getBasicConfig: PropTypes.func.isRequired,
  //sanitizeTree
};
var ConnectedQuery = connect(function (state) {
  return {
    tree: state.tree,
    __lastAction: state.__lastAction
  };
}, null, null, {
  context: context
})(Query);
ConnectedQuery.displayName = "ConnectedQuery";
export default ConnectedQuery;