"use strict";function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function t(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function r(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function n(e){for(var n=1;n<arguments.length;n++){var a=null!=arguments[n]?arguments[n]:{};n%2?r(Object(a),!0).forEach((function(r){t(e,r,a[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):r(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function a(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var r=[],n=!0,a=!1,i=void 0;try{for(var o,u=e[Symbol.iterator]();!(n=(o=u.next()).done)&&(r.push(o.value),!t||r.length!==t);n=!0);}catch(e){a=!0,i=e}finally{try{n||null==u.return||u.return()}finally{if(a)throw i}}return r}(e,t)||o(e,t)||function(){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 i(e){return function(e){if(Array.isArray(e))return u(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||o(e)||function(){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 o(e,t){if(e){if("string"==typeof e)return u(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?u(e,t):void 0}}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}var c=function(){function t(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t)}var r,o,u;return r=t,(o=[{key:"getOption",value:function(e,t){return n(n({},t||this.getDefOption()),e)}},{key:"getDefOption",value:function(){return{page:0,increment:[10],incrementIdx:0,maxSpread:3}}},{key:"getState",value:function(e,t){var r=this.getDefOption(),i=a(r.increment,1)[0],o=Object.assign(r,t),u=o.page,c=o.increment,l=o.incrementIdx,g=o.maxSpread,s=this.getNoPerPage(c,l,i),p=this.getDefState(e,s);if(e<=1)return p;var f=this.getTotalPage(e,s);if(f<=1)return p;var d=this.getCurrPage(u,f-1),v=d.curr,y=d.pageNo,b=this.getPageSliceIdx(e,s,v),h=b.startIdx,m=b.endIdx,P=this.getRecordCtx(e,h,m),S=this.getSpreadCtx(y,f,g),x=this.getRelPage(f,v),O=this.getRelPageCtx({curr:v,last:x.last},x);return n(n(n(n({curr:v},x=this.parseRelPage(x,O)),b),{},{pageNo:y,perPage:s,totalPage:f},P),S)}},{key:"getDefState",value:function(e,t){return n(n({},this.getRecordCtx(e,0)),{},{perPage:t,totalPage:1,startIdx:0,pageNo:1})}},{key:"getRecordCtx",value:function(e,t,r){var n=e>=1;return{startRecord:n&&Number.isInteger(t)?t+1:0,endRecord:n&&Number.isInteger(r)?r:e,totalRecord:e}}},{key:"getNoPerPage",value:function(e,t,r){if(!e.length)return r;var n=e[t];return Number.isInteger(n)&&n>0?e[t]:r}},{key:"parseNoPerPage",value:function(e){return e.filter((function(e){return Number.isInteger(e)&&e>0}))}},{key:"getTotalPage",value:function(e,t){var r=e>t?e/t:1;return Math.ceil(r)}},{key:"getCurrPage",value:function(e,t){var r=e>=0&&e<=t?e:0;return{curr:r,pageNo:r+1}}},{key:"getRelPage",value:function(e,t){return{first:0,prev:t-1,next:t+1,last:e-1}}},{key:"getRelPageCtx",value:function(e,t){var r=this;return Object.getOwnPropertyNames(t).reduce((function(n,a){var i={type:a,target:t[a]};return n[a]=r.canNavToPage(e,i),n}),{})}},{key:"parseRelPage",value:function(e,t){return Object.getOwnPropertyNames(e).forEach((function(r){var n=e[r];e[r]=t[r]?n:null})),e}},{key:"getPageSliceIdx",value:function(e,t,r){var n=r*t,a=n+t;return{startIdx:n=n<e?n:void 0,endIdx:a=a<e?a:void 0}}},{key:"getSpreadCtx",value:function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:3,n=i(Array(r+1)),a=1,o="...",u=t-e,c=e-a,l=u>1&&u<t,g=c>1&&c<t,s=l?n.reduce((function(n,a,i){var u=e+i+1,c=i===r&&t-u>=1;return u>1&&u<t&&n.push(c?o:u),n}),[]):null,p=g?n.reduce((function(n,a,i){var u=e-i-1,c=i===r&&e-u>=1;return u>1&&u<t&&n.unshift(c?o:u),n}),[]):null;return{ltSpread:p,rtSpread:s,maxSpread:r}}},{key:"getPageIdxForSpread",value:function(e,t,r){return r?e-t-1:e+t+1}},{key:"canNavToPage",value:function(e,t){var r=e.curr,n=e.last,a=t.type,i=t.target;if(!this.isGteZero([r,n]))return!1;switch(a){case"prev":return i>=0;case"next":return i<=n;case"first":return 0!==r&&i<r;case"last":return i>r;case"page":return this.isGteZero(i)&&i!==r&&i<=n;default:return!1}}},{key:"isGteZero",value:function(e){return Array.isArray(e)?e.every((function(e){return Number.isInteger(e)&&e>=0})):Number.isInteger(e)&&e>=0}},{key:"createGenericCmpAttr",value:function(e){var t=this,r=e.totalRecord,n=e.option,a=e.state,i=e.callback,o=a.first,u=a.prev,c=a.next,l=a.last,g=a.ltSpread,s=a.rtSpread,p=this.getGenericCmpEvtHandler(r,n,i);return{firstBtnAttr:this.getTextBtnAttr(p,["first",o]),prevBtnAttr:this.getTextBtnAttr(p,["prev",u]),nextBtnAttr:this.getTextBtnAttr(p,["next",c]),lastBtnAttr:this.getTextBtnAttr(p,["last",l]),ltSpreadBtnsAttr:g?g.map((function(e){return t.getSpreadBtnAttr(p,a,[e,!0])})):null,rtSpreadBtnsAttr:s?s.map((function(e){return t.getSpreadBtnAttr(p,a,[e,!1])})):null,perPageSelectAttr:this.getPerPageSelectAttr(p,n),pageSelectAttr:this.getPageSelectAttr(p,a)}}},{key:"getTextBtnAttr",value:function(e,t){var r=a(t,2),n=r[0],i=r[1];return{title:n,disabled:!Number.isInteger(i),onClick:function(){return e({page:i})}}}},{key:"getSpreadBtnAttr",value:function(e,t,r){var n=a(r,2),i=n[0],o=n[1],u=t.curr,c=t.maxSpread,l="number"==typeof i,g=o?l?u-1:this.getPageIdxForSpread(u,c,!0):l?u+1:this.getPageIdxForSpread(u,c,!1);return{title:l?i:o?"left-spread":"right-spread",isSpread:!l,onClick:function(){return e({page:g})}}}},{key:"getPageSelectAttr",value:function(e,t){var r=this,n=t.pageNo,a=t.totalPage,o=t.ltSpread,u=t.rtSpread,c=a<=1,l=c||1===n?[1]:[1].concat(i(o||[]),[n]),g=c||n===a?[]:[].concat(i(u||[]),[a]),s=[].concat(i(l),i(g)),p=l.length-1;return{title:"page select",disabled:c,options:s,selectedOptionValue:n,selectedOptionIdx:p,onSelect:function(n){var a=n.target,i=r.getTargetPageIdxByPos(t,s,[parseInt(a.value,10),p]);e({page:i})}}}},{key:"getPerPageSelectAttr",value:function(e,t){var r=t.increment,n=t.incrementIdx;return{title:"per page select",disabled:r.length<=1,options:r,selectedOptionValue:r[n],selectedOptionIdx:n,onSelect:function(t){var r=t.target;return e({page:0,incrementIdx:parseInt(r.value,10)})}}}},{key:"getGenericCmpEvtHandler",value:function(e,t,r){var n=this;return function(a){var i=n.getOption(a,t),o=n.getState(e,i);r&&r({pgnOption:i,pgnState:o})}}},{key:"getTargetPageIdxByPos",value:function(e,t,r){var n=a(r,2),i=n[0],o=n[1],u=e.curr,c=e.maxSpread,l=t[i];return"number"==typeof l?l-1:this.getPageIdxForSpread(u,c,i<o)}}])&&e(r.prototype,o),u&&e(r,u),t}();module.exports=c;
