import e from"@babel/runtime/helpers/defineProperty";import r from"@babel/runtime/helpers/slicedToArray";import{jsxs as t,Fragment as a,jsx as n}from"react/jsx-runtime";import{useRef as i,useState as o,useCallback as l,useMemo as s,useEffect as m}from"react";import c from"moment";import{Popover as u}from"../Popover/Popover.js";import{CalendarControls as d}from"./CalendarControls/CalendarControls.js";import{TimePicker as f}from"./TimePicker/TimePicker.js";import{Input as v}from"./Input/Input.js";import{applyOffsetSeconds as p,getMinDate as D,getMaxDate as y}from"./utils.js";import{CalendarGrid as b}from"./CalendarGrid/CalendarGrid.js";import h from"./DatePicker.scss.js";function g(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);r&&(a=a.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,a)}return t}function O(r){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?g(Object(a),!0).forEach((function(t){e(r,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(a)):g(Object(a)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(a,e))}))}return r}var T=function(e){var r=e.value,t=e.minDate,a=e.maxDate,n=(Array.isArray(r)?r[0]:r)||new Date;return t&&n.getTime()<t.getTime()?t:a&&n.getTime()>a.getTime()?a:n},j=function(e){var g=e.label,O=e.value,j=e.maxDate,C=e.minDate,P=e.className,N=e.testId,x=e.onChange,w=e.readOnly,A=e.type,E=void 0===A?"date":A,k=e.range,G=e.getTileClassName,I=e.getTileDisabled,S=e.placeholder,z=e.format,H=e.focusOnClear,V=void 0===H||H,q=e.labelPlacement,B=void 0===q?"top":q,F=e.size,J=void 0===F?"small":F,K=e.elementsClassNames,L=e.isNext,M=i(null),Q=i(null),R=o(!1),U=r(R,2),W=U[0],X=U[1],Y=o(T({maxDate:j,minDate:C,value:O})),Z=r(Y,2),$=Z[0],_=Z[1],ee=o(),re=r(ee,2),te=re[0],ae=re[1],ne=o("startDate"),ie=r(ne,2),oe=ie[0],le=ie[1],se=o(),me=r(se,2),ce=me[0],ue=me[1],de=o(),fe=r(de,2),ve=fe[0],pe=fe[1],De="dateTime"===E&&!!k,ye=o(),be=r(ye,2),he=be[0],ge=be[1],Oe=l((function(){ae(void 0),le("startDate"),ue(void 0),pe(void 0)}),[]),Te=l((function(){return X(!0)}),[]),je=l((function(){Oe(),X(!1)}),[Oe]),Ce=s((function(){return k?void 0:te||O}),[k,te,O]),Pe=s((function(){return!De||"startTime"!==oe&&"endTime"!==oe?Ce&&!Array.isArray(Ce)?Ce:void 0:ve||ce}),[De,oe,Ce,ve,ce]),Ne=l((function(e,r){var t;"startDate"===r||"startTime"===r?(le("startTime"),ue(e),pe(void 0)):"endDate"!==r&&"endTime"!==r||(le("endTime"),pe(e)),null===(t=Q.current)||void 0===t||t.flash()}),[]),xe=l((function(e,r,t,a){if("endDate"===t){var n=c(e).endOf("month"===a?"month":"day").toDate(),i=D(n,r),o=y(n,r);return i&&o?[i,o]:void 0}"startDate"===t&&(ue(e),pe(void 0),le("endDate"))}),[]),we=l((function(e){if(Array.isArray(e)||!e)throw new Error("Value is array or `null`, but should not be");if(De)Ne(e,oe);else if("dateTime"===E){var r;ae(e),null===(r=Q.current)||void 0===r||r.flash()}else if(k){var t=xe(e,ce,oe,E);t&&(null==x||x(t),je())}else!k&&e&&(null==x||x(e),je())}),[De,E,k,Ne,oe,xe,ce,x,je]),Ae=l((function(e){if(De){if("startTime"===oe)le("endDate"),ue((function(r){return r&&p(r,e)}));else if("endTime"===oe&&ve&&ce){var r=p(ve,e);null==x||x(ce<r?[ce,r]:[r,ce]),je()}}else if(Ce&&!Array.isArray(Ce)){var t=p(Ce,e);null==x||x(t),je()}}),[De,Ce,oe,ve,ce,x,je]);return m((function(){if(W){var e=Array.isArray(O);_(T({maxDate:j,minDate:C,value:O})),k&&e&&O[0]&&O[1]&&(ue(O[0]),pe(O[1]),le("startDate"))}}),[W,O,C,j,k]),t(a,{children:[n(v,{open:Te,isOpen:W,className:P,testId:N,value:O,label:g,placeholder:S,ref:M,format:z,range:k,type:E,setValue:x,readOnly:w,focusOnClear:V,labelPlacement:B,size:J,elementsClassNames:K,isNext:L}),$&&t(u,{anchorEl:M.current,onClose:je,open:W,className:h.popover,children:[t("div",{children:[n(d,{date:$,setDate:_,minDate:C,maxDate:j,view:"month"===E?"year":"month",className:h.controls}),n(b,{onChange:we,activeStartDate:$,view:"month"===E?"year":"month",maxDate:j,minDate:C,tileDisabled:I,selected:Ce,rangeStartDate:ce,rangeEndDate:ve,rangeHoveredDate:"endDate"===oe?he:void 0,setHoveredDate:ge,getTileClassName:G,className:h.grid})]}),"dateTime"===E&&n(f,{value:Pe,onChange:Ae,ref:Q})]})]})},C=function(e){return j(O(O({},e),{},{isNext:!1}))};C.Controls=d,C.Grid=b;var P=C,N=function(e){return j(O(O({},e),{},{isNext:!0}))};N.Controls=d,N.Grid=b;var x=N;export{P as DatePicker,x as DatePickerNext,T as getActiveStartDate};
//# sourceMappingURL=DatePicker.js.map
