import _slicedToArray from"@babel/runtime/helpers/slicedToArray";import{addHours,addMinutes,addSeconds,format as _format,isValid,isWithinInterval,parse as _parse,startOfDay,startOfHour,startOfMinute}from"date-fns";import{useCallback,useEffect}from"react";import{IMask,useIMask}from"react-imask";import{getLabelHours,getLabelMinutes,getLabelSeconds}from"../../DateTime/helpers";import{useMutableRef}from"../../../hooks/useMutableRef";import{getPartDate,getParts,getPartsDate,getTimeEnum}from"../helpers";import{datePickerErrorTypes}from"../types";export var usePicker=function(a){var b=a.value,c=a.onChange,d=a.onError,e=a.multiplicityHours,f=a.multiplicityMinutes,g=a.multiplicitySeconds,h=a.format,i=a.separator,j=a.maxDate,k=a.minDate,l=useMutableRef(c),m=useMutableRef(b),n=useMutableRef(d),o=useCallback(function(a,b,c){var d=l.current,e=m.current;if(!(e&&isValid(e)&&_format(e,h)===a)&&d){if(!a)return void(e&&d(null,{e:c}));var f=getParts(h,i,!1),g=getParts(a,i,!1),o=f.map(function(a){return getPartDate(f,g,a)}).filter(function(a){return!!a});if(f.length===o.length){var v=_parse(g.join(":"),f.join(":"),e||new Date);if(!isWithinInterval(v,{start:k,end:j})){var p,q=getPartsDate(a,h,":",!1,["HH","mm","ss"]),r=_slicedToArray(q,3),s=r[0],t=r[1],u=r[2];return null===(p=n.current)||void 0===p?void 0:p.call(n,{type:datePickerErrorTypes[0],stringValue:a,date:v,HH:s,mm:t,ss:u}),void(e&&d(null,{e:c}))}d(v,{e:c})}else e&&d(null,{e:c})}},[null===k||void 0===k?void 0:k.getTime(),null===j||void 0===j?void 0:j.getTime(),h,i]),p=useCallback(function(a,b,c){if((null===a||void 0===a?void 0:a.length)!==h.length&&m.current){var d;null===(d=l.current)||void 0===d?void 0:d.call(l,null,{e:c})}},[h]),q=useIMask({mask:Date,pattern:h,blocks:{HH:e&&1<e?{mask:IMask.MaskedEnum,enum:getTimeEnum(24,e,startOfDay,addHours,getLabelHours)}:{mask:IMask.MaskedRange,from:0,to:23},mm:f&&1<f?{mask:IMask.MaskedEnum,enum:getTimeEnum(60,f,startOfHour,addMinutes,getLabelMinutes)}:{mask:IMask.MaskedRange,from:0,to:59},ss:g&&1<g?{mask:IMask.MaskedEnum,enum:getTimeEnum(60,g,startOfMinute,addSeconds,getLabelSeconds)}:{mask:IMask.MaskedRange,from:0,to:59}},lazy:!0,autofix:!0,format:function format(a){return _format(a,h)},parse:function parse(a){return _parse(a,h,new Date)},validate:function validate(a){var b=getParts(h,i,!1),c=getParts(a,i,!1),d=b.map(function(a){return getPartDate(b,c,a)}).filter(function(a){return!!a});if(b.length===d.length&&!isValid(_parse(c.join(":"),b.join(":"),new Date))){var e,f=getPartsDate(a,h,":",!1,["HH","mm","ss"]),g=_slicedToArray(f,3),j=g[0],k=g[1],l=g[2];return null===(e=n.current)||void 0===e?void 0:e.call(n,{type:datePickerErrorTypes[1],stringValue:a,HH:j,mm:k,ss:l}),!1}return!0}},{onAccept:p,onComplete:o}),r=q.ref,s=q.setValue;return useEffect(function(){b&&isValid(b)&&s(_format(b,h))},[null===b||void 0===b?void 0:b.getTime()]),r};
//# sourceMappingURL=helpers.js.map