"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:t,toMonth:g,fromDay:h,toDay:y}=D;if(i&&i.length>0){let v=l,u=s;i.forEach(e=>{var R,$,x,m,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 ${u}.`);if(t&&e.date.getMonth()<t.getMonth())throw new Error(`Preset ${e.label} is before fromMonth ${t}.`);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 ${u}.`);if(t){let a=(x=e.dateRange.from)==null?void 0:x.getMonth();if(a&&a<t.getMonth())throw new Error(`Preset ${e.label}'s 'from' is before fromMonth ${w(t,"MMM, yyyy")}.`)}if(g){let a=(m=e.dateRange.to)==null?void 0:m.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 c,jsxs as Z}from"react/jsx-runtime";var X=({value:i,defaultValue:D,presetId:s,onChange:l,presets:t,disabled:g,disableNavigation:h,disabledDays:y,showYearNavigation:v=!1,locale:u=J,placeholder:e="Select date range",hasError:R,align:$="center",className:x,...m})=>{let{isDesktop:E}=C(),[P,f]=S(!1),[n,a]=S(t&&s?t==null?void 0:t.find(({id:o})=>o===s):void 0),[r,b]=S((n==null?void 0:n.dateRange)??i??D??void 0),[B,k]=S(r==null?void 0:r.to),K=L(()=>r,[P]);Y(()=>{b(i)},[i]),Y(()=>{let o=t==null?void 0:t.find(({id:M})=>M===s);a(o),b((o==null?void 0:o.dateRange)??i??D)},[t,s]),Y(()=>{P?r&&k(r.to):k(r==null?void 0:r.to)},[P]);let Q=(o,M)=>{let d=(r==null?void 0:r.from)&&(r==null?void 0:r.to)?{from:M}:o;b(d),a(void 0),(d==null?void 0:d.from)&&(d==null?void 0:d.to)&&(l==null||l(d),f(!1))},N=o=>{b(o.dateRange),a(o),l==null||l(o.dateRange,o),f(!1)},V=()=>{b(K),f(!1)},p=o=>{o||V(),f(o)},z=L(()=>r?W(r.from,r.to):null,[r,u]);return T((t==null?void 0:t.filter(o=>o.shortcut).map(o=>o.shortcut))??[],o=>{let M=t==null?void 0:t.find(d=>d.shortcut===o.key);M&&N(M)}),c(q.Provider,{value:{isOpen:P,setIsOpen:f},children:c(H,{align:$,openPopover:P,setOpenPopover:p,popoverContentClassName:"rounded-xl",content:c("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:[t&&t.length>0&&c("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:c("div",{className:"absolute px-3 sm:inset-0 sm:left-0 sm:p-3",children:c(I,{currentValue:r,presets:t,onSelect:N})})}),c("div",{className:"scrollbar-hide overflow-x-scroll",children:c(F,{mode:"range",selected:r,onSelect:Q,month:B,onMonthChange:k,numberOfMonths:E?2:1,disabled:y,disableNavigation:h,showYearNavigation:v,locale:u,className:"scrollbar-hide overflow-x-scroll",classNames:{months:"flex flex-row divide-x divide-gray-200 overflow-x-scroll scrollbar-hide"},...m})})]})}),children:c(U,{placeholder:e,disabled:g,className:x,hasError:R,"aria-required":m.required||m["aria-required"],"aria-invalid":m["aria-invalid"],"aria-label":m["aria-label"],"aria-labelledby":m["aria-labelledby"],children:(n==null?void 0:n.label)??z})})})};function he({presets:i,...D}){return i&&A(i,D),c(X,{presets:i,...D})}export{q as a,he as b};
