import e from"../../@babel/runtime/helpers/esm/assertThisInitialized.js";import t from"../../@babel/runtime/helpers/esm/inheritsLoose.js";import n from"react";import r from"../../prop-types/index.js";import o from"../../classnames/index.js";import i from"./CarouselItem.js";import{mapToCssModules as s}from"./utils.js";var a=function(r){function a(t){var n;return(n=r.call(this,t)||this).handleKeyPress=n.handleKeyPress.bind(e(n)),n.renderItems=n.renderItems.bind(e(n)),n.hoverStart=n.hoverStart.bind(e(n)),n.hoverEnd=n.hoverEnd.bind(e(n)),n.handleTouchStart=n.handleTouchStart.bind(e(n)),n.handleTouchEnd=n.handleTouchEnd.bind(e(n)),n.touchStartX=0,n.touchStartY=0,n.state={activeIndex:n.props.activeIndex,direction:"right",indicatorClicked:!1},n}t(a,r);var c=a.prototype;return c.getChildContext=function(){return{direction:this.state.direction}},c.componentDidMount=function(){"carousel"===this.props.ride&&this.setInterval(),document.addEventListener("keyup",this.handleKeyPress)},a.getDerivedStateFromProps=function(e,t){var n=null,r=t.activeIndex,o=t.direction,i=t.indicatorClicked;return e.activeIndex!==r&&(e.activeIndex===r+1?o="right":e.activeIndex===r-1?o="left":e.activeIndex<r?o=i?"left":"right":e.activeIndex!==r&&(o=i?"right":"left"),n={activeIndex:e.activeIndex,direction:o,indicatorClicked:!1}),n},c.componentDidUpdate=function(e,t){t.activeIndex!==this.state.activeIndex&&this.setInterval(this.props)},c.componentWillUnmount=function(){this.clearInterval(),document.removeEventListener("keyup",this.handleKeyPress)},c.setInterval=function(e){function t(){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}((function(e){void 0===e&&(e=this.props),this.clearInterval(),e.interval&&(this.cycleInterval=setInterval((function(){e.next()}),parseInt(e.interval,10)))})),c.clearInterval=function(e){function t(){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}((function(){clearInterval(this.cycleInterval)})),c.hoverStart=function(){var e;("hover"===this.props.pause&&this.clearInterval(),this.props.mouseEnter)&&(e=this.props).mouseEnter.apply(e,arguments)},c.hoverEnd=function(){var e;("hover"===this.props.pause&&this.setInterval(),this.props.mouseLeave)&&(e=this.props).mouseLeave.apply(e,arguments)},c.handleKeyPress=function(e){this.props.keyboard&&(37===e.keyCode?this.props.previous():39===e.keyCode&&this.props.next())},c.handleTouchStart=function(e){this.props.enableTouch&&(this.touchStartX=e.changedTouches[0].screenX,this.touchStartY=e.changedTouches[0].screenY)},c.handleTouchEnd=function(e){if(this.props.enableTouch){var t=e.changedTouches[0].screenX,n=e.changedTouches[0].screenY,r=Math.abs(this.touchStartX-t);r<Math.abs(this.touchStartY-n)||r<40||(t<this.touchStartX?this.props.next():this.props.previous())}},c.renderItems=function(e,t){var r=this,o=this.props.slide;return n.createElement("div",{className:t},e.map((function(e,t){var i=t===r.state.activeIndex;return n.cloneElement(e,{in:i,slide:o})})))},c.render=function(){var e=this,t=this.props,r=t.cssModule,a=t.slide,c=t.className,h=s(o(c,"carousel",a&&"slide"),r),u=s(o("carousel-inner"),r),d=this.props.children.filter((function(e){return null!=e&&"boolean"!=typeof e}));if(d.every((function(e){return e.type===i})))return n.createElement("div",{className:h,onMouseEnter:this.hoverStart,onMouseLeave:this.hoverEnd},this.renderItems(d,u));if(d[0]instanceof Array){var l=d[0],p=d[1],v=d[2];return n.createElement("div",{className:h,onMouseEnter:this.hoverStart,onMouseLeave:this.hoverEnd},this.renderItems(l,u),p,v)}var m=d[0],f=n.cloneElement(m,{onClickHandler:function(t){"function"==typeof m.props.onClickHandler&&e.setState({indicatorClicked:!0},(function(){return m.props.onClickHandler(t)}))}}),I=d[1],y=d[2],b=d[3];return n.createElement("div",{className:h,onMouseEnter:this.hoverStart,onMouseLeave:this.hoverEnd,onTouchStart:this.handleTouchStart,onTouchEnd:this.handleTouchEnd},f,this.renderItems(I,u),y,b)},a}(n.Component);a.propTypes={activeIndex:r.number,next:r.func.isRequired,previous:r.func.isRequired,keyboard:r.bool,pause:r.oneOf(["hover",!1]),ride:r.oneOf(["carousel"]),interval:r.oneOfType([r.number,r.string,r.bool]),children:r.array,mouseEnter:r.func,mouseLeave:r.func,slide:r.bool,cssModule:r.object,className:r.string,enableTouch:r.bool},a.defaultProps={interval:5e3,pause:"hover",keyboard:!0,slide:!0,enableTouch:!0},a.childContextTypes={direction:r.string};export default a;
//# sourceMappingURL=Carousel.js.map
