"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Item = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _utils = require("../../common/utils");
var _ = require("../..");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 && Object.prototype.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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var Overlay = _styledComponents.default.div.withConfig({
  displayName: "SegmentedControl__Overlay",
  componentId: "sc-de7zir-0"
})(["position:absolute;width:", "%;height:", "px;background-color:", ";left:", "%;pointer-events:none;transition:all 0.3s ease;"], function (p) {
  return p.$count === 0 ? 100 : 100 / p.$count;
}, function (p) {
  return p.theme.spacing.unit(8);
}, function (p) {
  return p.disabled ? p.theme.color.disabledBackground : p.theme.color.segmentedControlBackgroundSelected;
}, function (p) {
  return p.$count === 0 ? 0 : p.$selected * (100 / p.$count);
});
var Button = _styledComponents.default.button.withConfig({
  displayName: "SegmentedControl__Button",
  componentId: "sc-de7zir-1"
})(["display:flex;position:relative;align-items:center;justify-content:center;&[aria-checked='true']{pointer-events:none;}width:100%;height:100%;border:none;padding:0;background-color:transparent;cursor:", ";border-top:2px solid ", ";border-bottom:2px solid ", ";border-left:", ";border-right:", ";"], function (p) {
  return p.disabled ? 'not-allowed' : 'pointer';
}, function (p) {
  return p.theme.color.segmentedControlBorder;
}, function (p) {
  return p.theme.color.segmentedControlBorder;
}, function (p) {
  return p.theme.isDarkMode ? "1px solid ".concat(p.theme.color.segmentedControlBorder) : '';
}, function (p) {
  return p.theme.isDarkMode ? "1px solid ".concat(p.theme.color.segmentedControlBorder) : '';
});
var SegmentedControlContainer = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "SegmentedControl__SegmentedControlContainer",
  componentId: "sc-de7zir-2"
})(["position:relative;background-color:", ";height:", "px;margin:", "px;border-left:", ";border-right:", ";"], function (p) {
  return p.theme.color.segmentedControlBackground;
}, function (p) {
  return p.theme.spacing.unit(8);
}, function (p) {
  return p.theme.spacing.unit(0.5);
}, function (p) {
  return p.theme.isDarkMode ? "1px solid ".concat(p.theme.color.segmentedControlBorder) : "2px solid ".concat(p.theme.color.segmentedControlBorder);
}, function (p) {
  return p.theme.isDarkMode ? "1px solid ".concat(p.theme.color.segmentedControlBorder) : "2px solid ".concat(p.theme.color.segmentedControlBorder);
});
var StyledFlexbox = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "SegmentedControl__StyledFlexbox",
  componentId: "sc-de7zir-3"
})(["width:100%;height:100%;"]);
var Item = exports.Item = function Item(_ref) {
  var children = _ref.children;
  return _react.default.createElement("div", null, children);
};
Item.displayName = 'SegmentedControl.Item';
var isItemOrUndefined = function isItemOrUndefined(x) {
  if (x == null || typeof x === 'undefined') {
    return true;
  }
  return _typeof(x) === 'object' && Object.hasOwnProperty.call(x, 'type') && x.type.displayName === Item.displayName;
};
var SegmentedControl = function SegmentedControl(_ref2) {
  var children = _ref2.children,
    className = _ref2.className,
    _ref2$selectedInitial = _ref2.selectedInitially,
    selectedInitially = _ref2$selectedInitial === void 0 ? 0 : _ref2$selectedInitial,
    selectedControlled = _ref2.selected,
    _ref2$onClick = _ref2.onClick,
    onClick = _ref2$onClick === void 0 ? function () {} : _ref2$onClick,
    _ref2$disabled = _ref2.disabled,
    disabled = _ref2$disabled === void 0 ? false : _ref2$disabled;
  var isControlled = typeof selectedControlled !== 'undefined';
  var _useState = (0, _react.useState)(selectedInitially),
    _useState2 = _slicedToArray(_useState, 2),
    selected = _useState2[0],
    setSelected = _useState2[1];
  var clickHandler = function clickHandler(e, onItemClick, itemId) {
    setSelected(itemId);
    if (onItemClick) {
      setTimeout(function () {
        return onItemClick(e, itemId);
      });
    }
  };
  var items = [];
  _react.default.Children.forEach(children, function (c) {
    if (!isItemOrUndefined(c)) {
      (0, _utils.assert)(false, 'There should be only <SegmentedControl.Item> children inside of <SegmentedControl> component');
    } else if (c) {
      var itemSelected = isControlled ? selectedControlled : selected;
      items.push(_react.default.createElement(StyledFlexbox, {
        item: true,
        key: c.props.itemId
      }, _react.default.createElement(Button, {
        role: "radio",
        "aria-checked": itemSelected === c.props.itemId,
        onClick: isControlled ? function (e) {
          return onClick(e, c.props.itemId);
        } : function (e) {
          return clickHandler(e, c.props.onItemClick, c.props.itemId);
        },
        className: c.props.className,
        disabled: disabled
      }, _react.default.createElement(_.Typography, _extends({
        type: "secondary",
        weight: itemSelected === c.props.itemId ? 'bold' : 'regular'
      }, disabled && {
        color: function color(t) {
          return t.color.disabledText;
        }
      }), c.props.children))));
    }
  });
  return _react.default.createElement(SegmentedControlContainer, {
    container: true,
    role: "radiogroup",
    className: className,
    alignItems: "stretch"
  }, _react.default.createElement(Overlay, {
    $selected: (isControlled ? selectedControlled : selected) || 0,
    $count: items.length,
    disabled: disabled
  }), items);
};
SegmentedControl.Item = Item;
var _default = exports.default = SegmentedControl;