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