"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.Carousel=void 0;var _react=_interopRequireWildcard(require("react")),_propTypes=_interopRequireDefault(require("prop-types")),_ClassNames=require("../utils/ClassNames"),_UniqueComponentId=_interopRequireDefault(require("../utils/UniqueComponentId")),_DomHandler=_interopRequireDefault(require("../utils/DomHandler")),_Ripple=require("../ripple/Ripple");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _getRequireWildcardCache(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,s=new WeakMap;return(_getRequireWildcardCache=function(e){return e?s:t})(e)}function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!==_typeof(e)&&"function"!=typeof e)return{default:e};t=_getRequireWildcardCache(t);if(t&&t.has(e))return t.get(e);var s,a,r={},i=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&((a=i?Object.getOwnPropertyDescriptor(e,s):null)&&(a.get||a.set)?Object.defineProperty(r,s,a):r[s]=e[s]);return r.default=e,t&&t.set(e,r),r}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_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(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var s=Object.prototype.toString.call(e).slice(8,-1);return"Map"===(s="Object"===s&&e.constructor?e.constructor.name:s)||"Set"===s?Array.from(e):"Arguments"===s||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(s)?_arrayLikeToArray(e,t):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var s=0,a=new Array(t);s<t;s++)a[s]=e[s];return a}function ownKeys(t,e){var s,a=Object.keys(t);return Object.getOwnPropertySymbols&&(s=Object.getOwnPropertySymbols(t),e&&(s=s.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),a.push.apply(a,s)),a}function _objectSpread(t){for(var e=1;e<arguments.length;e++){var s=null!=arguments[e]?arguments[e]:{};e%2?ownKeys(Object(s),!0).forEach(function(e){_defineProperty(t,e,s[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(s)):ownKeys(Object(s)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(s,e))})}return t}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var s=0;s<t.length;s++){var a=t[s];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}function _createClass(e,t,s){return t&&_defineProperties(e.prototype,t),s&&_defineProperties(e,s),e}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _createSuper(s){var a=_isNativeReflectConstruct();return function(){var e,t=_getPrototypeOf(s);return _possibleConstructorReturn(this,a?(e=_getPrototypeOf(this).constructor,Reflect.construct(t,arguments,e)):t.apply(this,arguments))}}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?_assertThisInitialized(e):t}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(e){return!1}}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _defineProperty(e,t,s){return t in e?Object.defineProperty(e,t,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[t]=s,e}var CarouselItem=function(){_inherits(t,_react.Component);var e=_createSuper(t);function t(){return _classCallCheck(this,t),e.apply(this,arguments)}return _createClass(t,[{key:"render",value:function(){var e=this.props.template(this.props.item),t=(0,_ClassNames.classNames)(this.props.className,"p-carousel-item",{"p-carousel-item-active":this.props.active,"p-carousel-item-start":this.props.start,"p-carousel-item-end":this.props.end});return _react.default.createElement("div",{className:t},e)}}]),t}();_defineProperty(CarouselItem,"defaultProps",{template:null,item:null,active:!1,start:!1,end:!1,className:null}),_defineProperty(CarouselItem,"propTypes",{template:_propTypes.default.func,item:_propTypes.default.any,active:_propTypes.default.bool,start:_propTypes.default.bool,end:_propTypes.default.bool,className:_propTypes.default.string});var Carousel=function(){_inherits(a,_react.Component);var s=_createSuper(a);function a(e){var t;return _classCallCheck(this,a),(t=s.call(this,e)).state={numVisible:e.numVisible,numScroll:e.numScroll,totalShiftedItems:e.page*e.numScroll*-1},t.props.onPageChange||(t.state=_objectSpread(_objectSpread({},t.state),{},{page:e.page})),t.navBackward=t.navBackward.bind(_assertThisInitialized(t)),t.navForward=t.navForward.bind(_assertThisInitialized(t)),t.onTransitionEnd=t.onTransitionEnd.bind(_assertThisInitialized(t)),t.onTouchStart=t.onTouchStart.bind(_assertThisInitialized(t)),t.onTouchMove=t.onTouchMove.bind(_assertThisInitialized(t)),t.onTouchEnd=t.onTouchEnd.bind(_assertThisInitialized(t)),t.totalIndicators=0,t.remainingItems=0,t.allowAutoplay=!!t.props.autoplayInterval,t.circular=t.props.circular||t.allowAutoplay,t.attributeSelector=(0,_UniqueComponentId.default)(),t.swipeThreshold=20,t}return _createClass(a,[{key:"step",value:function(e,t){var s,a=this.state.totalShiftedItems,r=this.isCircular();null!=t?(a=this.state.numScroll*t*-1,r&&(a-=this.state.numVisible),this.isRemainingItemsAdded=!1):(a+=this.state.numScroll*e,this.isRemainingItemsAdded&&(a+=this.remainingItems-this.state.numScroll*e,this.isRemainingItemsAdded=!1),s=r?a+this.state.numVisible:a,t=Math.abs(Math.floor(s/this.state.numScroll))),r&&this.state.page===this.totalIndicators-1&&-1===e?(a=-1*(this.props.value.length+this.state.numVisible),t=0):r&&0===this.state.page&&1===e?(a=0,t=this.totalIndicators-1):t===this.totalIndicators-1&&0<this.remainingItems&&(a+=-1*this.remainingItems-this.state.numScroll*e,this.isRemainingItemsAdded=!0),this.itemsContainer&&(_DomHandler.default.removeClass(this.itemsContainer,"p-items-hidden"),this.changePosition(a),this.itemsContainer.style.transition="transform 500ms ease 0s"),this.props.onPageChange?(this.setState({totalShiftedItems:a}),this.props.onPageChange({page:t})):this.setState({page:t,totalShiftedItems:a})}},{key:"calculatePosition",value:function(){if(this.itemsContainer&&this.responsiveOptions){for(var e=window.innerWidth,t={numVisible:this.props.numVisible,numScroll:this.props.numScroll},s=0;s<this.responsiveOptions.length;s++){var a=this.responsiveOptions[s];parseInt(a.breakpoint,10)>=e&&(t=a)}var r,i,n={};this.state.numScroll!==t.numScroll&&(r=this.getPage(),r=Math.floor(r*this.state.numScroll/t.numScroll),i=t.numScroll*r*-1,this.isCircular()&&(i-=t.numVisible),n={totalShiftedItems:i,numScroll:t.numScroll},this.props.onPageChange?this.props.onPageChange({page:r}):n=_objectSpread(_objectSpread({},n),{},{page:r})),this.state.numVisible!==t.numVisible&&(n=_objectSpread(_objectSpread({},n),{},{numVisible:t.numVisible})),Object.keys(n).length&&this.setState(n)}}},{key:"navBackward",value:function(e,t){!this.circular&&0===this.getPage()||this.step(1,t),this.allowAutoplay=!1,e.cancelable&&e.preventDefault()}},{key:"navForward",value:function(e,t){(this.circular||this.getPage()<this.totalIndicators-1)&&this.step(-1,t),this.allowAutoplay=!1,e.cancelable&&e.preventDefault()}},{key:"onDotClick",value:function(e,t){var s=this.getPage();s<t?this.navForward(e,t):t<s&&this.navBackward(e,t)}},{key:"onTransitionEnd",value:function(){this.itemsContainer&&(_DomHandler.default.addClass(this.itemsContainer,"p-items-hidden"),this.itemsContainer.style.transition="",0!==this.state.page&&this.state.page!==this.totalIndicators-1||!this.isCircular()||this.changePosition(this.state.totalShiftedItems))}},{key:"onTouchStart",value:function(e){e=e.changedTouches[0];this.startPos={x:e.pageX,y:e.pageY}}},{key:"onTouchMove",value:function(e){e.cancelable&&e.preventDefault()}},{key:"onTouchEnd",value:function(e){var t=e.changedTouches[0];this.isVertical()?this.changePageOnTouch(e,t.pageY-this.startPos.y):this.changePageOnTouch(e,t.pageX-this.startPos.x)}},{key:"changePageOnTouch",value:function(e,t){Math.abs(t)>this.swipeThreshold&&(t<0?this.navForward(e):this.navBackward(e))}},{key:"bindDocumentListeners",value:function(){var e=this;this.documentResizeListener||(this.documentResizeListener=function(){e.calculatePosition()},window.addEventListener("resize",this.documentResizeListener))}},{key:"unbindDocumentListeners",value:function(){this.documentResizeListener&&(window.removeEventListener("resize",this.documentResizeListener),this.documentResizeListener=null)}},{key:"isVertical",value:function(){return"vertical"===this.props.orientation}},{key:"isCircular",value:function(){return this.circular&&this.props.value.length>=this.state.numVisible}},{key:"getPage",value:function(){return(this.props.onPageChange?this.props:this.state).page}},{key:"getTotalIndicators",value:function(){return this.props.value?Math.ceil((this.props.value.length-this.state.numVisible)/this.state.numScroll)+1:0}},{key:"isAutoplay",value:function(){return this.props.autoplayInterval&&this.allowAutoplay}},{key:"startAutoplay",value:function(){var e=this;this.interval=setInterval(function(){e.state.page===e.totalIndicators-1?e.step(-1,0):e.step(-1,e.state.page+1)},this.props.autoplayInterval)}},{key:"stopAutoplay",value:function(){this.interval&&clearInterval(this.interval)}},{key:"createStyle",value:function(){this.carouselStyle||(this.carouselStyle=document.createElement("style"),document.body.appendChild(this.carouselStyle));var e="\n            .p-carousel[".concat(this.attributeSelector,"] .p-carousel-item {\n                flex: 1 0 ").concat(100/this.state.numVisible,"%\n            }\n        ");if(this.props.responsiveOptions){this.responsiveOptions=_toConsumableArray(this.props.responsiveOptions),this.responsiveOptions.sort(function(e,t){e=e.breakpoint,t=t.breakpoint;return-1*(null==e&&null!=t?-1:null!=e&&null==t?1:null==e&&null==t?0:"string"==typeof e&&"string"==typeof t?e.localeCompare(t,void 0,{numeric:!0}):e<t?-1:t<e?1:0)});for(var t=0;t<this.responsiveOptions.length;t++){var s=this.responsiveOptions[t];e+="\n                    @media screen and (max-width: ".concat(s.breakpoint,") {\n                        .p-carousel[").concat(this.attributeSelector,"] .p-carousel-item {\n                            flex: 1 0 ").concat(100/s.numVisible,"%\n                        }\n                    }\n                ")}}this.carouselStyle.innerHTML=e}},{key:"changePosition",value:function(e){this.itemsContainer&&(this.itemsContainer.style.transform=this.isVertical()?"translate3d(0, ".concat(e*(100/this.state.numVisible),"%, 0)"):"translate3d(".concat(e*(100/this.state.numVisible),"%, 0, 0)"))}},{key:"componentDidMount",value:function(){this.container&&this.container.setAttribute(this.attributeSelector,""),this.createStyle(),this.calculatePosition(),this.changePosition(this.state.totalShiftedItems),this.props.responsiveOptions&&this.bindDocumentListeners()}},{key:"componentDidUpdate",value:function(e,t){var s=this.isCircular(),a=!1,r=this.state.totalShiftedItems;this.props.autoplayInterval&&this.stopAutoplay(),(t.numScroll!==this.state.numScroll||t.numVisible!==this.state.numVisible||this.props.value&&e.value&&e.value.length!==this.props.value.length)&&(this.remainingItems=(this.props.value.length-this.state.numVisible)%this.state.numScroll,t=this.getPage(),0!==this.totalIndicators&&t>=this.totalIndicators&&(t=this.totalIndicators-1,this.props.onPageChange?this.props.onPageChange({page:t}):this.setState({page:t}),a=!0),r=t*this.state.numScroll*-1,s&&(r-=this.state.numVisible),t===this.totalIndicators-1&&0<this.remainingItems?(r+=-1*this.remainingItems+this.state.numScroll,this.isRemainingItemsAdded=!0):this.isRemainingItemsAdded=!1,r!==this.state.totalShiftedItems&&(this.setState({totalShiftedItems:r}),a=!0),this.changePosition(r)),s&&(0===this.state.page?r=-1*this.state.numVisible:0===r&&(r=-1*this.props.value.length,0<this.remainingItems&&(this.isRemainingItemsAdded=!0)),r!==this.state.totalShiftedItems&&(this.setState({totalShiftedItems:r}),a=!0)),e.page!==this.props.page&&(this.props.page>e.page&&this.props.page<=this.totalIndicators-1?this.step(-1,this.props.page):this.props.page<e.page&&this.step(1,this.props.page)),!a&&this.isAutoplay()&&this.startAutoplay()}},{key:"componentWillUnmount",value:function(){this.props.responsiveOptions&&this.unbindDocumentListeners(),this.props.autoplayInterval&&this.stopAutoplay()}},{key:"renderItems",value:function(){var r=this;if(this.props.value&&this.props.value.length){var i,n=this.isCircular(),e=null,t=null;n&&(i=null,e=(i=this.props.value.slice(-1*this.state.numVisible)).map(function(e,t){var s=-1*r.state.totalShiftedItems===r.props.value.length+r.state.numVisible,a=t===i.length-1;return _react.default.createElement(CarouselItem,{key:t+"_scloned",className:"p-carousel-item-cloned",template:r.props.itemTemplate,item:e,active:s,start:0===t,end:a})}),t=(i=this.props.value.slice(0,this.state.numVisible)).map(function(e,t){var s=0===r.state.totalShiftedItems,a=t===i.length-1;return _react.default.createElement(CarouselItem,{key:t+"_fcloned",className:"p-carousel-item-cloned",template:r.props.itemTemplate,item:e,active:s,start:0===t,end:a})}));var s=this.props.value.map(function(e,t){var s=n?-1*(r.state.totalShiftedItems+r.state.numVisible):-1*r.state.totalShiftedItems,a=s+r.state.numVisible-1;return _react.default.createElement(CarouselItem,{key:t,template:r.props.itemTemplate,item:e,active:s<=t&&t<=a,start:s===t,end:a===t})});return _react.default.createElement(_react.default.Fragment,null,e,s,t)}}},{key:"renderHeader",value:function(){return this.props.header?_react.default.createElement("div",{className:"p-carousel-header"},this.props.header):null}},{key:"renderFooter",value:function(){return this.props.footer?_react.default.createElement("div",{className:"p-carousel-footer"},this.props.footer):null}},{key:"renderContent",value:function(){var t=this,e=this.renderItems(),s=this.isVertical()?this.props.verticalViewPortHeight:"auto",a=this.renderBackwardNavigator(),r=this.renderForwardNavigator(),i=(0,_ClassNames.classNames)("p-carousel-container",this.props.containerClassName);return _react.default.createElement("div",{className:i},a,_react.default.createElement("div",{className:"p-carousel-items-content",style:{height:s}},_react.default.createElement("div",{ref:function(e){return t.itemsContainer=e},className:"p-carousel-items-container",onTransitionEnd:this.onTransitionEnd,onTouchStart:this.onTouchStart,onTouchMove:this.onTouchMove,onTouchEnd:this.onTouchEnd},e)),r)}},{key:"renderBackwardNavigator",value:function(){var e=(!this.circular||this.props.value&&this.props.value.length<this.state.numVisible)&&0===this.getPage(),t=(0,_ClassNames.classNames)("p-carousel-prev p-link",{"p-disabled":e}),s=(0,_ClassNames.classNames)("p-carousel-prev-icon pi",{"pi-chevron-left":!this.isVertical(),"pi-chevron-up":this.isVertical()});return _react.default.createElement("button",{type:"button",className:t,onClick:this.navBackward,disabled:e},_react.default.createElement("span",{className:s}),_react.default.createElement(_Ripple.Ripple,null))}},{key:"renderForwardNavigator",value:function(){var e=(!this.circular||this.props.value&&this.props.value.length<this.state.numVisible)&&(this.getPage()===this.totalIndicators-1||0===this.totalIndicators),t=(0,_ClassNames.classNames)("p-carousel-next p-link",{"p-disabled":e}),s=(0,_ClassNames.classNames)("p-carousel-next-icon pi",{"pi-chevron-right":!this.isVertical(),"pi-chevron-down":this.isVertical()});return _react.default.createElement("button",{type:"button",className:t,onClick:this.navForward,disabled:e},_react.default.createElement("span",{className:s}),_react.default.createElement(_Ripple.Ripple,null))}},{key:"renderIndicator",value:function(t){var s=this,e=this.getPage()===t,e=(0,_ClassNames.classNames)("p-carousel-indicator",{"p-highlight":e});return _react.default.createElement("li",{className:e,key:"p-carousel-indicator-"+t},_react.default.createElement("button",{type:"button",className:"p-link",onClick:function(e){return s.onDotClick(e,t)}},_react.default.createElement(_Ripple.Ripple,null)))}},{key:"renderIndicators",value:function(){for(var e=(0,_ClassNames.classNames)("p-carousel-indicators p-reset",this.props.indicatorsContentClassName),t=[],s=0;s<this.totalIndicators;s++)t.push(this.renderIndicator(s));return _react.default.createElement("ul",{className:e},t)}},{key:"render",value:function(){var t=this,e=(0,_ClassNames.classNames)("p-carousel p-component",{"p-carousel-vertical":this.isVertical(),"p-carousel-horizontal":!this.isVertical()},this.props.className),s=(0,_ClassNames.classNames)("p-carousel-content",this.props.contentClassName);this.totalIndicators=this.getTotalIndicators();var a=this.renderContent(),r=this.renderIndicators(),i=this.renderHeader(),n=this.renderFooter();return _react.default.createElement("div",{ref:function(e){return t.container=e},id:this.props.id,className:e,style:this.props.style},i,_react.default.createElement("div",{className:s},a,r),n)}}]),a}();_defineProperty(exports.Carousel=Carousel,"defaultProps",{id:null,value:null,page:0,header:null,footer:null,style:null,className:null,itemTemplate:null,circular:!1,autoplayInterval:0,numVisible:1,numScroll:1,responsiveOptions:null,orientation:"horizontal",verticalViewPortHeight:"300px",contentClassName:null,containerClassName:null,indicatorsContentClassName:null,onPageChange:null}),_defineProperty(Carousel,"propTypes",{id:_propTypes.default.string,value:_propTypes.default.any,page:_propTypes.default.number,header:_propTypes.default.any,footer:_propTypes.default.any,style:_propTypes.default.object,className:_propTypes.default.string,itemTemplate:_propTypes.default.any,circular:_propTypes.default.bool,autoplayInterval:_propTypes.default.number,numVisible:_propTypes.default.number,numScroll:_propTypes.default.number,responsiveOptions:_propTypes.default.array,orientation:_propTypes.default.string,verticalViewPortHeight:_propTypes.default.string,contentClassName:_propTypes.default.string,containerClassName:_propTypes.default.string,indicatorsContentClassName:_propTypes.default.string,onPageChange:_propTypes.default.func});