"use client"
import{a as I}from"./chunk-T4G2DWD5.mjs";import{a as U}from"./chunk-3UBKDLRE.mjs";import{c as T}from"./chunk-FTMJ25RI.mjs";import{a as F}from"./chunk-AVPEWGF6.mjs";import{a as H}from"./chunk-YH3XACZN.mjs";import{a as C}from"./chunk-NWME2ANF.mjs";import{j as O}from"./chunk-4H4GYNAN.mjs";import{enUS as J}from"date-fns/locale";import{formatDateRange as W}from"little-date";import{useEffect as Y,useMemo as L,useState as S}from"react";import{format as w}from"date-fns";import{createContext as G}from"react";var q=G({isOpen:!1,setIsOpen:()=>{}});var A=(i,D)=>{let{toYear:s,fromYear:l,fromMonth:o,toMonth:g,fromDay:h,toDay:y}=D;if(i&&i.length>0){let v=l,M=s;i.forEach(e=>{var R,$,x,c,E,P;if("date"in e){let f=e.date.getFullYear();if(l&&f<l)throw new Error(`Preset ${e.label} is before fromYear ${v}.`);if(s&&f>s)throw new Error(`Preset ${e.label} is after toYear ${M}.`);if(o&&e.date.getMonth()<o.getMonth())throw new Error(`Preset ${e.label} is before fromMonth ${o}.`);if(g&&e.date.getMonth()>g.getMonth())throw new Error(`Preset ${e.label} is after toMonth ${g}.`);if(h&&e.date.getDate()<h.getDate())throw new Error(`Preset ${e.label} is before fromDay ${h}.`);if(y&&e.date.getDate()>y.getDate())throw new Error(`Preset ${e.label} is after toDay ${w(y,"MMM dd, yyyy")}.`)}if("dateRange"in e){let f=(R=e.dateRange.from)==null?void 0:R.getFullYear(),n=($=e.dateRange.to)==null?void 0:$.getFullYear();if(f&&l&&f<l)throw new Error(`Preset ${e.label}'s 'from' is before fromYear ${v}.`);if(n&&s&&n>s)throw new Error(`Preset ${e.label}'s 'to' is after toYear ${M}.`);if(o){let a=(x=e.dateRange.from)==null?void 0:x.getMonth();if(a&&a<o.getMonth())throw new Error(`Preset ${e.label}'s 'from' is before fromMonth ${w(o,"MMM, yyyy")}.`)}if(g){let a=(c=e.dateRange.to)==null?void 0:c.getMonth();if(a&&a>g.getMonth())throw new Error(`Preset ${e.label}'s 'to' is after toMonth ${w(g,"MMM, yyyy")}.`)}if(h){let a=(E=e.dateRange.from)==null?void 0:E.getDate();if(a&&a<h.getDate())throw new Error(`Preset ${e.dateRange.from}'s 'from' is before fromDay ${w(h,"MMM dd, yyyy")}.`)}if(y){let a=(P=e.dateRange.to)==null?void 0:P.getDate();if(a&&a>y.getDate())throw new Error(`Preset ${e.label}'s 'to' is after toDay ${w(y,"MMM dd, yyyy")}.`)}}})}};import{jsx as m,jsxs as Z}from"react/jsx-runtime";var X=({value:i,defaultValue:D,presetId:s,onChange:l,presets:o,disabled:g,disableNavigation:h,disabledDays:y,showYearNavigation:v=!1,locale:M=J,placeholder:e="Select date range",hasError:R,align:$="center",className:x,...c})=>{let{isDesktop:E}=C(),[P,f]=S(!1),[n,a]=S(o&&s?o==null?void 0:o.find(({id:r})=>r===s):void 0),[t,b]=S((n==null?void 0:n.dateRange)??i??D??void 0),[B,k]=S(t==null?void 0:t.to),K=L(()=>t,[P]);Y(()=>{b(i)},[i]),Y(()=>{let r=o==null?void 0:o.find(({id:u})=>u===s);a(r),b((r==null?void 0:r.dateRange)??i??D)},[o,s]),Y(()=>{P?t&&k(t.to):k(t==null?void 0:t.to)},[P]);let Q=(r,u)=>{let d=(t==null?void 0:t.from)&&(t==null?void 0:t.to)?{from:u}:r;b(d),a(void 0),(d==null?void 0:d.from)&&(d==null?void 0:d.to)&&(l==null||l(d),f(!1))},N=r=>{b(r.dateRange),a(r),l==null||l(r.dateRange,r),f(!1)},V=()=>{b(K),f(!1)},p=r=>{r||V(),f(r)},z=L(()=>!(t!=null&&t.from)||!(t!=null&&t.to)?null:W(t.from,t.to,{locale:M}),[t,M]);return T((o==null?void 0:o.filter(r=>r.shortcut).map(r=>r.shortcut))??[],r=>{let u=o==null?void 0:o.find(d=>d.shortcut===r.key);u&&N(u)}),m(q.Provider,{value:{isOpen:P,setIsOpen:f},children:m(H,{align:$,openPopover:P,setOpenPopover:p,popoverContentClassName:"rounded-xl",content:m("div",{className:"flex w-full",children:Z("div",{className:"scrollbar-hide flex w-full flex-col overflow-x-scroll sm:flex-row-reverse sm:items-start",children:[o&&o.length>0&&m("div",{className:O("relative flex h-16 w-full items-center sm:h-full sm:w-48","border-b border-gray-200 sm:border-b-0 sm:border-l","scrollbar-hide overflow-auto"),children:m("div",{className:"absolute px-3 sm:inset-0 sm:left-0 sm:p-3",children:m(I,{currentValue:t,presets:o,onSelect:N})})}),m("div",{className:"scrollbar-hide overflow-x-scroll",children:m(F,{mode:"range",selected:t,onSelect:Q,month:B,onMonthChange:k,numberOfMonths:E?2:1,disabled:y,disableNavigation:h,showYearNavigation:v,locale:M,className:"scrollbar-hide overflow-x-scroll",classNames:{months:"flex flex-row divide-x divide-gray-200 overflow-x-scroll scrollbar-hide"},...c})})]})}),children:m(U,{placeholder:e,disabled:g,className:x,hasError:R,"aria-required":c.required||c["aria-required"],"aria-invalid":c["aria-invalid"],"aria-label":c["aria-label"],"aria-labelledby":c["aria-labelledby"],children:(n==null?void 0:n.label)??z})})})};function he({presets:i,...D}){return i&&A(i,D),m(X,{presets:i,...D})}export{q as a,he as b};
