import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _get from "@babel/runtime/helpers/get";
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 from "react";
import { Utils } from "@react-awesome-query-builder/core";
import PropTypes from "prop-types";
import GroupContainer from "../containers/GroupContainer";
import Draggable from "../containers/Draggable";
import { BasicGroup } from "./Group";
import { RuleGroupExtActions } from "./RuleGroupExtActions";
import FieldWrapper from "../rule/FieldWrapper";
import OperatorWrapper from "../rule/OperatorWrapper";
import { useOnPropsChanged } from "../../utils/reactUtils";
import { Col, dummyFn, WithConfirmFn } from "../utils";
import Widget from "../rule/Widget";
import classNames from "classnames";
var _Utils$ConfigUtils = Utils.ConfigUtils,
  getFieldConfig = _Utils$ConfigUtils.getFieldConfig,
  getFieldWidgetConfig = _Utils$ConfigUtils.getFieldWidgetConfig;
var isEmptyRuleGroupExtPropertiesAndChildren = Utils.RuleUtils.isEmptyRuleGroupExtPropertiesAndChildren;
var getTotalReordableNodesCountInTree = Utils.TreeUtils.getTotalReordableNodesCountInTree;
var RuleGroupExt = /*#__PURE__*/function (_BasicGroup) {
  function RuleGroupExt(props) {
    var _this;
    _classCallCheck(this, RuleGroupExt);
    _this = _callSuper(this, RuleGroupExt, [props]);
    _this.childrenClassName = function () {
      return "rule_group_ext--children";
    };
    _this.renderFooterWrapper = function () {
      return null;
    };
    _this.canDeleteGroup = function () {
      return true;
    };
    return _this;
  }
  _inherits(RuleGroupExt, _BasicGroup);
  return _createClass(RuleGroupExt, [{
    key: "onPropsChanged",
    value: function onPropsChanged(nextProps) {
      _get(_getPrototypeOf(RuleGroupExt.prototype), "onPropsChanged", this).call(this, nextProps);
    }
  }, {
    key: "canAddGroup",
    value: function canAddGroup() {
      return this.props.allowFurtherNesting;
    }
  }, {
    key: "canAddRule",
    value: function canAddRule() {
      var _this$props = this.props,
        config = _this$props.config,
        selectedField = _this$props.selectedField;
      var selectedFieldConfig = getFieldConfig(config, selectedField);
      var maxNumberOfRules = selectedFieldConfig.maxNumberOfRules;
      var totalRulesCnt = this.props.totalRulesCnt;
      if (maxNumberOfRules) {
        return totalRulesCnt < maxNumberOfRules;
      }
      return true;
    }
  }, {
    key: "renderHeaderWrapper",
    value: function renderHeaderWrapper() {
      return /*#__PURE__*/React.createElement("div", {
        key: "group-header",
        className: classNames("group--header", this.isOneChild() ? "one--child" : "", this.isOneChild() ? "hide--line" : "", this.isNoChildren() ? "no--children" : "", this.showDragIcon() ? "with--drag" : "hide--drag", this.showConjs() ? "with--conjs" : "hide--conjs")
      }, this.renderHeader(), this.renderGroupField(), this.renderActions());
    }
  }, {
    key: "renderHeader",
    value: function renderHeader() {
      return /*#__PURE__*/React.createElement("div", {
        className: "group--conjunctions"
      }, this.renderConjs(), this.renderDrag());
    }
  }, {
    key: "renderGroupField",
    value: function renderGroupField() {
      return /*#__PURE__*/React.createElement("div", {
        className: "group--field--count--rule"
      }, this.renderField(), this.renderOperator(), this.renderWidget(), this.renderError());
    }
  }, {
    key: "renderError",
    value: function renderError() {
      var _this$props2 = this.props,
        config = _this$props2.config,
        valueError = _this$props2.valueError;
      var _config$settings = config.settings,
        renderRuleError = _config$settings.renderRuleError,
        showErrorMessage = _config$settings.showErrorMessage;
      var oneError = _toConsumableArray((valueError === null || valueError === void 0 ? void 0 : valueError.toArray()) || []).filter(function (e) {
        return !!e;
      }).shift() || null;
      return showErrorMessage && oneError && /*#__PURE__*/React.createElement("div", {
        className: "rule_group--error"
      }, renderRuleError ? renderRuleError({
        error: oneError
      }, config.ctx) : oneError);
    }
  }, {
    key: "showNot",
    value: function showNot() {
      var _selectedFieldConfig$;
      var _this$props3 = this.props,
        config = _this$props3.config,
        selectedField = _this$props3.selectedField;
      var selectedFieldConfig = getFieldConfig(config, selectedField);
      return (_selectedFieldConfig$ = selectedFieldConfig === null || selectedFieldConfig === void 0 ? void 0 : selectedFieldConfig.showNot) !== null && _selectedFieldConfig$ !== void 0 ? _selectedFieldConfig$ : config.settings.showNot;
    }
  }, {
    key: "conjunctionOptions",
    value: function conjunctionOptions() {
      var selectedField = this.props.selectedField;
      return this.conjunctionOptionsForGroupField(selectedField);
    }
  }, {
    key: "renderField",
    value: function renderField() {
      var _this$props4 = this.props,
        config = _this$props4.config,
        selectedField = _this$props4.selectedField,
        selectedFieldSrc = _this$props4.selectedFieldSrc,
        selectedFieldType = _this$props4.selectedFieldType,
        setField = _this$props4.setField,
        setFieldSrc = _this$props4.setFieldSrc,
        setFuncValue = _this$props4.setFuncValue,
        parentField = _this$props4.parentField,
        id = _this$props4.id,
        groupId = _this$props4.groupId,
        isLocked = _this$props4.isLocked;
      var immutableFieldsMode = config.settings.immutableFieldsMode;
      return /*#__PURE__*/React.createElement(FieldWrapper, {
        key: "field",
        classname: "rule--field",
        config: config,
        canSelectFieldSource: false,
        selectedField: selectedField,
        selectedFieldSrc: selectedFieldSrc,
        selectedFieldType: selectedFieldType,
        setField: setField,
        setFuncValue: setFuncValue,
        setFieldSrc: setFieldSrc,
        parentField: parentField,
        readonly: immutableFieldsMode || isLocked,
        id: id,
        groupId: groupId
      });
    }
  }, {
    key: "renderOperator",
    value: function renderOperator() {
      var _this$props5 = this.props,
        config = _this$props5.config,
        selectedField = _this$props5.selectedField,
        selectedFieldSrc = _this$props5.selectedFieldSrc,
        selectedOperator = _this$props5.selectedOperator,
        setField = _this$props5.setField,
        setOperator = _this$props5.setOperator,
        isLocked = _this$props5.isLocked;
      var immutableFieldsMode = config.settings.immutableFieldsMode;
      var selectedFieldWidgetConfig = getFieldWidgetConfig(config, selectedField, selectedOperator) || {};
      var hideOperator = selectedFieldWidgetConfig.hideOperator;
      var showOperatorLabel = selectedField && hideOperator && selectedFieldWidgetConfig.operatorInlineLabel;
      var showOperator = selectedField && !hideOperator;
      return /*#__PURE__*/React.createElement(OperatorWrapper, {
        key: "operator",
        classname: "group--operator",
        config: config,
        selectedField: selectedField,
        selectedFieldSrc: selectedFieldSrc,
        selectedOperator: selectedOperator,
        setOperator: setOperator,
        showOperator: showOperator,
        showOperatorLabel: showOperatorLabel,
        selectedFieldWidgetConfig: selectedFieldWidgetConfig,
        readonly: immutableFieldsMode || isLocked,
        id: this.props.id,
        groupId: this.props.groupId
      });
    }
  }, {
    key: "isEmptyCurrentGroup",
    value: function isEmptyCurrentGroup() {
      var _this$props6 = this.props,
        children1 = _this$props6.children1,
        config = _this$props6.config;
      var ruleData = this._buildWidgetProps(this.props);
      return isEmptyRuleGroupExtPropertiesAndChildren(ruleData, children1, config);
    }
  }, {
    key: "_buildWidgetProps",
    value: function _buildWidgetProps(_ref) {
      var selectedField = _ref.selectedField,
        selectedFieldSrc = _ref.selectedFieldSrc,
        selectedFieldType = _ref.selectedFieldType,
        selectedOperator = _ref.selectedOperator,
        operatorOptions = _ref.operatorOptions,
        value = _ref.value,
        valueType = _ref.valueType,
        valueSrc = _ref.valueSrc,
        asyncListValues = _ref.asyncListValues,
        valueError = _ref.valueError,
        fieldError = _ref.fieldError,
        parentField = _ref.parentField;
      return {
        field: selectedField,
        fieldSrc: selectedFieldSrc,
        fieldType: selectedFieldType,
        operator: selectedOperator,
        operatorOptions: operatorOptions,
        value: value,
        valueType: valueType,
        // new Immutable.List(["number"])
        // todo: aggregation can be not only number?
        valueSrc: ["value"],
        //new Immutable.List(["value"]), // should be fixed in isEmptyRuleGroupExtPropertiesAndChildren
        //asyncListValues,
        valueError: valueError,
        fieldError: null,
        parentField: parentField
      };
    }
  }, {
    key: "renderWidget",
    value: function renderWidget() {
      var _this$props7 = this.props,
        config = _this$props7.config,
        selectedField = _this$props7.selectedField,
        selectedOperator = _this$props7.selectedOperator,
        isLocked = _this$props7.isLocked;
      var immutableValuesMode = config.settings.immutableValuesMode;
      var isFieldAndOpSelected = selectedField && selectedOperator;
      var showWidget = isFieldAndOpSelected;
      if (!showWidget) return null;
      var widget = /*#__PURE__*/React.createElement(Widget, _extends({
        key: "values",
        isForRuleGroup: true
      }, this._buildWidgetProps(this.props), {
        config: config,
        setValue: !immutableValuesMode ? this.props.setValue : dummyFn
        // todo: aggregation can be not only number?
        ,
        setValueSrc: dummyFn,
        readonly: immutableValuesMode || isLocked,
        id: this.props.id,
        groupId: this.props.groupId
      }));
      return /*#__PURE__*/React.createElement(Col, {
        key: "widget-for-" + this.props.selectedOperator,
        className: "rule--value"
      }, widget);
    }
  }, {
    key: "renderActions",
    value: function renderActions() {
      var _this$props8 = this.props,
        config = _this$props8.config,
        addRule = _this$props8.addRule,
        addGroup = _this$props8.addGroup,
        isLocked = _this$props8.isLocked,
        isTrueLocked = _this$props8.isTrueLocked,
        id = _this$props8.id;
      return /*#__PURE__*/React.createElement(RuleGroupExtActions, {
        config: config,
        addRule: addRule,
        addGroup: addGroup,
        canAddRule: this.canAddRule(),
        canAddGroup: this.canAddGroup(),
        canDeleteGroup: this.canDeleteGroup(),
        removeSelf: this.removeSelf,
        setLock: this.setLock,
        isLocked: isLocked,
        isTrueLocked: isTrueLocked,
        id: id
      });
    }
  }, {
    key: "reordableNodesCntForItem",
    value: function reordableNodesCntForItem(_item) {
      if (this.props.isLocked) return 0;
      var _this$props9 = this.props,
        children1 = _this$props9.children1,
        id = _this$props9.id;
      return getTotalReordableNodesCountInTree({
        id: id,
        type: "rule_group",
        children1: children1
      });
    }
  }, {
    key: "extraPropsForItem",
    value: function extraPropsForItem(_item) {
      var _selectedFieldConfig$2;
      var _this$props10 = this.props,
        selectedField = _this$props10.selectedField,
        lev = _this$props10.lev,
        config = _this$props10.config;
      var selectedFieldConfig = getFieldConfig(config, selectedField);
      return {
        parentField: selectedField,
        parentFieldPathSize: lev + 1,
        parentFieldCanReorder: (_selectedFieldConfig$2 = selectedFieldConfig === null || selectedFieldConfig === void 0 ? void 0 : selectedFieldConfig.canReorder) !== null && _selectedFieldConfig$2 !== void 0 ? _selectedFieldConfig$2 : config.settings.canReorder
      };
    }
  }]);
}(BasicGroup);
RuleGroupExt.propTypes = _objectSpread(_objectSpread({}, BasicGroup.propTypes), {}, {
  selectedField: PropTypes.any,
  selectedFieldSrc: PropTypes.string,
  selectedOperator: PropTypes.string,
  value: PropTypes.any,
  parentField: PropTypes.string,
  setField: PropTypes.func,
  setFieldSrc: PropTypes.func,
  setOperator: PropTypes.func,
  setValue: PropTypes.func,
  valueError: PropTypes.any,
  lev: PropTypes.number // from GroupContainer
});
export default GroupContainer(Draggable("group rule_group_ext")(WithConfirmFn(RuleGroupExt)), "rule_group");