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/useMutableRef";import{datePickerPropSeparatorDefault,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,f=a.format,g=a.separator,h=a.maxDate,i=a.minDate,j=a.multiplicityHours,k=a.multiplicityMinutes,l=a.multiplicitySeconds,m=useMutableRef(c),n=useMutableRef(b),o=useMutableRef(d),p=useCallback(function(a,b,c){var d=m.current,e=n.current;if(!(e&&isValid(e)&&_format(e,f)===a)&&d){if(!a)return void(e&&d(null,{e:c}));var j=getParts(f,g,!1),k=getParts(a,g,!1),l=j.map(function(a){return getPartDate(j,k,a)}).filter(function(a){return!!a});if(j.length===l.length){var y=_parse(k.join(datePickerPropSeparatorDefault),j.join(datePickerPropSeparatorDefault),new Date);if(!isWithinInterval(y,{start:i,end:h})){var p,q=getPartsDate(a,f,g,!0,["dd","MM","yyyy","HH","mm","ss"]),r=_slicedToArray(q,6),s=r[0],t=r[1],u=r[2],v=r[3],w=r[4],x=r[5];return null===(p=o.current)||void 0===p?void 0:p.call(o,{type:datePickerErrorTypes[0],stringValue:a,dd:s,MM:t,yyyy:u,date:y,HH:v,mm:w,ss:x}),void(e&&d(null,{e:c}))}d(y,{e:c})}else e&&d(null,{e:c})}},[null===i||void 0===i?void 0:i.getTime(),null===h||void 0===h?void 0:h.getTime(),f,g]),q=useCallback(function(a,b,c){if((null===a||void 0===a?void 0:a.length)!==f.length&&n.current){var d;null===(d=m.current)||void 0===d?void 0:d.call(m,null,{e:c})}},[f]),r=useIMask({mask:Date,pattern:f,blocks:{yyyy:{mask:IMask.MaskedRange,from:1,to:9999},MM:{mask:IMask.MaskedRange,from:1,to:12},dd:{mask:IMask.MaskedRange,from:1,to:31},HH:j&&1<j?{mask:IMask.MaskedEnum,enum:getTimeEnum(24,j,startOfDay,addHours,getLabelHours)}:{mask:IMask.MaskedRange,from:0,to:23},mm:k&&1<k?{mask:IMask.MaskedEnum,enum:getTimeEnum(60,k,startOfHour,addMinutes,getLabelMinutes)}:{mask:IMask.MaskedRange,from:0,to:59},ss:l&&1<l?{mask:IMask.MaskedEnum,enum:getTimeEnum(60,l,startOfMinute,addSeconds,getLabelSeconds)}:{mask:IMask.MaskedRange,from:0,to:59}},lazy:!0,autofix:!0,format:function format(a){return _format(a,f)},parse:function parse(a){return _parse(a,f,new Date)},validate:function validate(a){var b=getParts(f,g,!1),c=getParts(a,g,!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(datePickerPropSeparatorDefault),b.join(datePickerPropSeparatorDefault),new Date))){var e,h=getPartsDate(a,f,g,!0,["dd","MM","yyyy","HH","mm","ss"]),i=_slicedToArray(h,6),j=i[0],k=i[1],l=i[2],m=i[3],n=i[4],p=i[5];return null===(e=o.current)||void 0===e?void 0:e.call(o,{type:datePickerErrorTypes[1],stringValue:a,dd:j,MM:k,yyyy:l,HH:m,mm:n,ss:p}),!1}return!0}},{onAccept:q,onComplete:p}),s=r.ref,t=r.setValue;return useEffect(function(){b&&isValid(b)&&t(_format(b,f))},[null===b||void 0===b?void 0:b.getTime()]),s};
//# sourceMappingURL=helpers.js.map