"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_assertThisInitialized2=_interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")),_inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits")),_possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")),_getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")),_defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty")),React=_interopRequireWildcard(require("react")),_classnames=_interopRequireDefault(require("classnames")),_propTypes=_interopRequireDefault(require("prop-types")),_withStyles=_interopRequireDefault(require("../../hoc/withStyles")),_ErrorBoundary=_interopRequireDefault(require("../../hoc/ErrorBoundary")),_styles=_interopRequireDefault(require("./styles"));function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function(){var result,Super=(0,_getPrototypeOf2["default"])(Derived);if(hasNativeReflectConstruct){var NewTarget=(0,_getPrototypeOf2["default"])(this).constructor;result=Reflect.construct(Super,arguments,NewTarget)}else result=Super.apply(this,arguments);return(0,_possibleConstructorReturn2["default"])(this,result)}}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}var ToggleButton=/*#__PURE__*/function(_React$Component){function ToggleButton(props){var _this;return(0,_classCallCheck2["default"])(this,ToggleButton),_this=_super.call(this,props),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"_handleChange",function(index){if(index!==_this.state.selectedIndex){var labels=_this.props.labels;_this.setState({selectedIndex:index}),_this.props.onChange&&_this.props.onChange(labels[index],index)}}),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"_renderButtons",function(){var _this$props=_this.props,classes=_this$props.classes,labels=_this$props.labels,selectedIndex=_this.state.selectedIndex;return/*#__PURE__*/React.createElement("div",{className:(0,_classnames["default"])(classes.root)},/*#__PURE__*/React.createElement("div",{className:(0,_classnames["default"])(classes.buttons)},labels.map(function(label,index){return/*#__PURE__*/React.createElement("div",{"data-test":"button-".concat(index),onClick:function onClick(){_this._handleChange(index)},className:(0,_classnames["default"])(classes.button,{active:selectedIndex===index}),key:index},label)})),/*#__PURE__*/React.createElement("div",{className:(0,_classnames["default"])(classes.highlighter,{"is-at-first":0===selectedIndex,"is-at-last":selectedIndex===labels.length-1}),style:{left:"".concat(100/labels.length*selectedIndex,"%")}}))}),_this.state={selectedIndex:props.selectedIndex||0},_this}(0,_inherits2["default"])(ToggleButton,_React$Component);var _super=_createSuper(ToggleButton);return(0,_createClass2["default"])(ToggleButton,[{key:"componentDidUpdate",value:function componentDidUpdate(prevProps){this.props.selectedIndex!==prevProps.selectedIndex&&this.setState({selectedIndex:this.props.selectedIndex})}},{key:"render",value:function render(){var _this$props2=this.props,classes=_this$props2.classes,title=_this$props2.title,id=_this$props2.id;return/*#__PURE__*/React.createElement(_ErrorBoundary["default"],null,/*#__PURE__*/React.createElement("div",{className:(0,_classnames["default"])(classes.component),id:id},/*#__PURE__*/React.createElement("div",{className:classes.labelWrapper},!!title&&/*#__PURE__*/React.createElement("div",{className:classes.label},title)),this._renderButtons()))}}]),ToggleButton}(React.Component);(0,_defineProperty2["default"])(ToggleButton,"propTypes",{name:_propTypes["default"].string,id:_propTypes["default"].string,title:_propTypes["default"].string,labels:_propTypes["default"].arrayOf(_propTypes["default"].string).isRequired,activeColor:_propTypes["default"].string,onChange:_propTypes["default"].func,selectedIndex:_propTypes["default"].number,classes:_propTypes["default"].object,// will come from withStyles HOC
theme:_propTypes["default"].any}),(0,_defineProperty2["default"])(ToggleButton,"defaultProps",{id:"",onChange:function onChange(){}});var _default=(0,_withStyles["default"])(_styles["default"])(ToggleButton);exports["default"]=_default;