"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _core = require("@react-awesome-query-builder/core");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _GroupContainer = _interopRequireDefault(require("../containers/GroupContainer"));
var _Draggable = _interopRequireDefault(require("../containers/Draggable"));
var _Group = require("./Group");
var _SwitchGroupActions = require("./SwitchGroupActions");
var _utils = require("../utils");
var _classnames = _interopRequireDefault(require("classnames"));
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) { (0, _defineProperty2["default"])(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 = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(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; })(); }
var _Utils$TreeUtils = _core.Utils.TreeUtils,
  getTotalReordableNodesCountInTree = _Utils$TreeUtils.getTotalReordableNodesCountInTree,
  getTotalRulesCountInTree = _Utils$TreeUtils.getTotalRulesCountInTree;
var SwitchGroup = /*#__PURE__*/function (_BasicGroup) {
  function SwitchGroup(props) {
    var _this;
    (0, _classCallCheck2["default"])(this, SwitchGroup);
    _this = _callSuper(this, SwitchGroup, [props]);
    _this.childrenClassName = function () {
      return "switch_group--children";
    };
    _this.renderFooterWrapper = function () {
      return null;
    };
    _this.hasDefaultCase = function () {
      var _this$props$children;
      return ((_this$props$children = _this.props.children1) === null || _this$props$children === void 0 ? void 0 : _this$props$children.size) && _this.props.children1.filter(function (c) {
        return c.get("children1") == null;
      }).size > 0;
    };
    return _this;
  }
  (0, _inherits2["default"])(SwitchGroup, _BasicGroup);
  return (0, _createClass2["default"])(SwitchGroup, [{
    key: "onPropsChanged",
    value: function onPropsChanged(nextProps) {
      (0, _get2["default"])((0, _getPrototypeOf2["default"])(SwitchGroup.prototype), "onPropsChanged", this).call(this, nextProps);
    }
  }, {
    key: "canAddGroup",
    value: function canAddGroup() {
      var _this$props$children2;
      var maxNumberOfCases = this.props.config.settings.maxNumberOfCases;
      var totalCasesCnt = ((_this$props$children2 = this.props.children1) === null || _this$props$children2 === void 0 ? void 0 : _this$props$children2.size) || 0;
      if (maxNumberOfCases) {
        return totalCasesCnt < maxNumberOfCases;
      }
      return true;
    }
  }, {
    key: "canAddRule",
    value: function canAddRule() {
      return false;
    }
  }, {
    key: "reordableNodesCnt",
    value: function reordableNodesCnt() {
      var _this$props$children3;
      // result will be passed to each case's `parentReordableNodesCnt` prop
      var totalCasesCnt = ((_this$props$children3 = this.props.children1) === null || _this$props$children3 === void 0 ? void 0 : _this$props$children3.size) || 0;
      var casesToReorder = totalCasesCnt;
      if (this.hasDefaultCase()) {
        casesToReorder--;
      }
      return casesToReorder;
    }
  }, {
    key: "totalRulesCntForItem",
    value: function totalRulesCntForItem(item) {
      return getTotalRulesCountInTree(item);
    }
  }, {
    key: "reordableNodesCntForItem",
    value: function reordableNodesCntForItem(item) {
      var _this$props$children4;
      if (this.props.isLocked) return 0;
      var _this$props$config$se = this.props.config.settings,
        canLeaveEmptyCase = _this$props$config$se.canLeaveEmptyCase,
        canRegroupCases = _this$props$config$se.canRegroupCases;
      var totalCasesCnt = ((_this$props$children4 = this.props.children1) === null || _this$props$children4 === void 0 ? void 0 : _this$props$children4.size) || 0;
      var casesToReorder = totalCasesCnt;
      if (this.hasDefaultCase()) {
        casesToReorder--;
      }
      var nodesInCase = getTotalReordableNodesCountInTree(item);
      var cnt = nodesInCase;
      if (cnt == 1 && canRegroupCases && canLeaveEmptyCase && casesToReorder > 1) cnt = 111;
      return cnt;
    }
  }, {
    key: "showConjs",
    value: function showConjs() {
      // always show line from "If"
      return true;
    }
  }, {
    key: "renderHeaderWrapper",
    value: function renderHeaderWrapper() {
      return /*#__PURE__*/_react["default"].createElement("div", {
        key: "group-header",
        className: (0, _classnames["default"])("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.renderActions());
    }
  }, {
    key: "renderHeader",
    value: function renderHeader() {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "group--conjunctions"
      }, this.renderConjs(), this.renderDrag());
    }
  }, {
    key: "renderConjs",
    value: function renderConjs() {
      var config = this.props.config;
      var renderSwitchPrefix = config.settings.renderSwitchPrefix;
      return renderSwitchPrefix || null;
    }
  }, {
    key: "showNot",
    value: function showNot() {
      return false;
    }
  }, {
    key: "renderActions",
    value: function renderActions() {
      var _this$props = this.props,
        config = _this$props.config,
        addCaseGroup = _this$props.addCaseGroup,
        addDefaultCaseGroup = _this$props.addDefaultCaseGroup,
        isLocked = _this$props.isLocked,
        isTrueLocked = _this$props.isTrueLocked,
        id = _this$props.id;
      return /*#__PURE__*/_react["default"].createElement(_SwitchGroupActions.SwitchGroupActions, {
        config: config,
        addCaseGroup: addCaseGroup,
        addDefaultCaseGroup: addDefaultCaseGroup,
        canAddDefault: !this.hasDefaultCase(),
        canAddGroup: this.canAddGroup(),
        setLock: this.setLock,
        isLocked: isLocked,
        isTrueLocked: isTrueLocked,
        id: id
      });
    }
  }]);
}(_Group.BasicGroup);
SwitchGroup.propTypes = _objectSpread({}, _Group.BasicGroup.propTypes);
var _default = exports["default"] = (0, _GroupContainer["default"])((0, _Draggable["default"])("group switch_group")((0, _utils.WithConfirmFn)(SwitchGroup)), "switch_group");