"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", "checked", "onChange", "children", "buttonProps"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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); }
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,
    checked = _ref2.checked,
    _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 (_ref3, index) {
    var label = _ref3.label,
      value = _ref3.value,
      title = _ref3.title;
    var buttonGroupProps = getButtonGroupProps(index, items.length);
    return /*#__PURE__*/_react["default"].createElement(_button.Button, _extends({
      key: value,
      label: label,
      onClick: function onClick() {
        return onChange(value);
      }
    }, title ? {
      title: title
    } : {}, checked != value ? {
      flavour: "hollow"
    } : {}, buttonGroupProps, buttonProps));
  }));
};
var ButtonGroup = exports.ButtonGroup = function ButtonGroup(_ref4) {
  var items = _ref4.items,
    checked = _ref4.checked,
    onChange = _ref4.onChange,
    children = _ref4.children,
    buttonProps = _ref4.buttonProps,
    props = _objectWithoutPropertiesLoose(_ref4, _excluded);
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], _extends({
    alignItems: "center"
  }, props), items != null && items.length ? /*#__PURE__*/_react["default"].createElement(RadioButtons, {
    items: items,
    checked: checked,
    onChange: onChange,
    buttonProps: buttonProps
  }) : /*#__PURE__*/_react["default"].createElement(Content, null, children));
};