import _slicedToArray from"@babel/runtime/helpers/slicedToArray";import{format as _format,isValid,isWithinInterval,parse as _parse}from"date-fns";import{useCallback,useEffect}from"react";import{IMask,useIMask}from"react-imask";import{useMutableRef}from"../../../hooks/useMutableRef/useMutableRef";import{datePickerPropSeparatorDefault,getPartDate,getParts,getPartsDate}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=useMutableRef(c),k=useMutableRef(b),l=useMutableRef(d),m=useCallback(function(a,b,c){var d=j.current,e=k.current;if(!(e&&isValid(e)&&_format(e,f)===a)&&d){if(!a)return void(e&&d(null,{e:c}));var m=getParts(f,g,!1),n=getParts(a,g,!1),o=m.map(function(a){return getPartDate(m,n,a)}).filter(function(a){return!!a});if(m.length===o.length){var u=_parse(n.join(datePickerPropSeparatorDefault),m.join(datePickerPropSeparatorDefault),new Date);if(!isWithinInterval(u,{start:i,end:h})){var p,q=getPartsDate(a,f,g,!1,["MM","yyyy"]),r=_slicedToArray(q,2),s=r[0],t=r[1];return null===(p=l.current)||void 0===p?void 0:p.call(l,{type:datePickerErrorTypes[0],stringValue:a,MM:s,yyyy:t,date:u}),void(e&&d(null,{e:c}))}d(u,{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]),n=useCallback(function(a,b,c){if((null===a||void 0===a?void 0:a.length)!==f.length&&k.current){var d;null===(d=j.current)||void 0===d?void 0:d.call(j,null,{e:c})}},[f]),o=useIMask({mask:Date,pattern:f,blocks:{yyyy:{mask:IMask.MaskedRange,from:1,to:9999},MM:{mask:IMask.MaskedRange,from:1,to:12}},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,!1,["MM","yyyy"]),i=_slicedToArray(h,2),j=i[0],k=i[1];return null===(e=l.current)||void 0===e?void 0:e.call(l,{type:datePickerErrorTypes[1],stringValue:a,MM:j,yyyy:k}),!1}return!0}},{onAccept:n,onComplete:m}),p=o.ref,q=o.setValue;return useEffect(function(){b&&isValid(b)&&q(_format(b,f))},[null===b||void 0===b?void 0:b.getTime()]),p};
//# sourceMappingURL=helpers.js.map