"use strict";

exports.__esModule = true;
exports.ButtonGroup = void 0;
var _react = _interopRequireWildcard(require("react"));
var _flex = _interopRequireDefault(require("../templates/flex"));
var _button = require("./button");
var _excluded = ["items", "value", "isMulti", "onChange", "children", "ButtonComponent", "buttonProps"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var noop = function noop() {};
var getButtonGroupProps = function getButtonGroupProps(itemIndex, itemsLength) {
  var groupFirst = itemIndex == 0;
  var groupLast = itemIndex == itemsLength - 1;
  return {
    groupFirst: groupFirst,
    groupLast: groupLast,
    groupMiddle: !groupFirst && !groupLast
  };
};
var Content = function Content(_ref) {
  var children = _ref.children;
  var renderedChildren = _react.Children.toArray(children).length;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, _react.Children.map(children, function (child, index) {
    if (/*#__PURE__*/(0, _react.isValidElement)(child)) {
      var buttonGroupProps = getButtonGroupProps(index, renderedChildren);
      return /*#__PURE__*/(0, _react.cloneElement)(child, buttonGroupProps);
    }
    return child;
  }));
};
var RadioButtons = function RadioButtons(_ref2) {
  var items = _ref2.items,
    value = _ref2.value,
    isMulti = _ref2.isMulti,
    _ref2$ButtonComponent = _ref2.ButtonComponent,
    ButtonComponent = _ref2$ButtonComponent === void 0 ? _button.Button : _ref2$ButtonComponent,
    _ref2$buttonProps = _ref2.buttonProps,
    buttonProps = _ref2$buttonProps === void 0 ? {} : _ref2$buttonProps,
    onChange = _ref2.onChange;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, items.map(function (item, index) {
    var buttonGroupProps = getButtonGroupProps(index, items.length);
    var isSelected = value === item.value || Array.isArray(value) && value.includes(item.value);
    var onClick = function onClick() {
      if (isMulti) {
        var newValue = Array.isArray(value) ? value.includes(item.value) ? value.filter(function (v) {
          return v !== item.value;
        }) : [].concat(_toConsumableArray(value), [item.value]) : [item.value];
        onChange(newValue);
      } else {
        onChange(item.value);
      }
    };
    return /*#__PURE__*/_react["default"].createElement(ButtonComponent, _extends({
      key: item.value,
      label: item.label,
      onClick: onClick
    }, item.title ? {
      title: item.title
    } : {}, !isSelected ? {
      flavour: "hollow"
    } : {}, buttonGroupProps, buttonProps, item.buttonProps || {}));
  }));
};
var ButtonGroup = exports.ButtonGroup = function ButtonGroup(_ref3) {
  var items = _ref3.items,
    value = _ref3.value,
    isMulti = _ref3.isMulti,
    _ref3$onChange = _ref3.onChange,
    onChange = _ref3$onChange === void 0 ? noop : _ref3$onChange,
    children = _ref3.children,
    ButtonComponent = _ref3.ButtonComponent,
    buttonProps = _ref3.buttonProps,
    props = _objectWithoutPropertiesLoose(_ref3, _excluded);
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], _extends({
    alignItems: "center"
  }, props), items != null && items.length ? /*#__PURE__*/_react["default"].createElement(RadioButtons, {
    items: items,
    value: value,
    isMulti: isMulti,
    onChange: onChange,
    ButtonComponent: ButtonComponent,
    buttonProps: buttonProps
  }) : /*#__PURE__*/_react["default"].createElement(Content, null, children));
};