'use strict';

require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
var _extends = require('@babel/runtime/helpers/extends');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var React = require('react');
var PropTypes = require('prop-types');
var Button = require('../Button/Button.js');
var Settings = require('../../constants/Settings.js');
var deprecate = require('../../internal/deprecate.js');
var GroupLogic = require('./GroupLogic.js');
var RuleBuilderPropTypes = require('./RuleBuilderPropTypes.js');
var iconsReact = require('@carbon/icons-react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__namespace = /*#__PURE__*/_interopNamespace(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

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__default.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; }
var iotPrefix = Settings.settings.iotPrefix;
var defaultProps = {
  i18n: {
    addRule: 'Add rule',
    addGroup: 'Add group'
  },
  // the default logic for the primary rule builder group. Can be ALL or ANY
  groupLogic: 'ALL',
  testId: 'rule-builder-header'
};
var propTypes = {
  id: PropTypes__default.default.string.isRequired,
  groupLogic: RuleBuilderPropTypes.GroupLogicPropType,
  i18n: PropTypes__default.default.shape({
    addRule: PropTypes__default.default.string,
    addGroup: PropTypes__default.default.string
  }),
  onAddRule: PropTypes__default.default.func.isRequired,
  onChange: PropTypes__default.default.func.isRequired,
  // TODO: remove the deprecated 'testID' in v3.
  // eslint-disable-next-line react/require-default-props
  testID: deprecate(PropTypes__default.default.string, "The 'testID' prop has been deprecated. Please use 'testId' instead."),
  testId: PropTypes__default.default.string
};
var RuleBuilderHeader = function RuleBuilderHeader(_ref) {
  var id = _ref.id,
    onAddRule = _ref.onAddRule,
    onChange = _ref.onChange,
    i18n = _ref.i18n,
    groupLogic = _ref.groupLogic,
    testID = _ref.testID,
    testId = _ref.testId;
  var mergedI18n = React__namespace.useMemo(function () {
    return _objectSpread(_objectSpread({}, defaultProps.i18n), i18n !== null && i18n !== void 0 ? i18n : {});
  }, [i18n]);
  var handleChangeGroupLogic = React__namespace.useCallback(function (_ref2) {
    var selectedItem = _ref2.selectedItem;
    onChange({
      id: id,
      groupLogic: selectedItem.id
    }, true);
  }, [id, onChange]);
  return /*#__PURE__*/React__namespace.createElement("div", {
    // TODO: remove the deprecated 'testID' in v3.
    "data-testid": testID || testId,
    className: "".concat(iotPrefix, "--rule-builder-header")
  }, /*#__PURE__*/React__namespace.createElement(GroupLogic, {
    id: id,
    selected: groupLogic,
    onChange: handleChangeGroupLogic
  }), /*#__PURE__*/React__namespace.createElement("div", {
    className: "".concat(iotPrefix, "--rule-builder-header__buttons")
  }, /*#__PURE__*/React__namespace.createElement(Button
  // TODO: remove the deprecated 'testID' in v3.
  , {
    testId: "".concat(testID || testId, "-add-rule-button"),
    kind: "ghost",
    renderIcon: function renderIcon(props) {
      return /*#__PURE__*/React__namespace.createElement(iconsReact.Add, _extends__default.default({
        size: 32
      }, props));
    },
    onClick: onAddRule()
  }, mergedI18n.addRule), /*#__PURE__*/React__namespace.createElement(Button
  // TODO: remove the deprecated 'testID' in v3.
  , {
    testId: "".concat(testID || testId, "-add-group-button"),
    kind: "ghost",
    renderIcon: function renderIcon(props) {
      return /*#__PURE__*/React__namespace.createElement(iconsReact.TextNewLine, _extends__default.default({
        size: 32
      }, props));
    },
    onClick: onAddRule(undefined, true)
  }, mergedI18n.addGroup)));
};
RuleBuilderHeader.defaultProps = defaultProps;
RuleBuilderHeader.propTypes = propTypes;
RuleBuilderHeader.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "RuleBuilderHeader",
  "props": {
    "i18n": {
      "defaultValue": {
        "value": "{\n  addRule: 'Add rule',\n  addGroup: 'Add group',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "addRule": {
            "name": "string",
            "required": false
          },
          "addGroup": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "groupLogic": {
      "defaultValue": {
        "value": "'ALL'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "custom",
        "raw": "GroupLogicPropType"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'rule-builder-header'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "id": {
      "description": "",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "onAddRule": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "onChange": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "testID": {
      "description": "",
      "type": {
        "name": "custom",
        "raw": "deprecate(\n  PropTypes.string,\n  `The 'testID' prop has been deprecated. Please use 'testId' instead.`\n)"
      },
      "required": false
    }
  }
};
var RuleBuilderHeader$1 = RuleBuilderHeader;

module.exports = RuleBuilderHeader$1;
