"use client"
import{a as F}from"./chunk-Y2W7AILC.mjs";import{a as H}from"./chunk-LLRCEJB4.mjs";import{a as T}from"./chunk-HIQXFKNG.mjs";import{a as C}from"./chunk-UQUT3ZK3.mjs";import{a as p}from"./chunk-K4MP745I.mjs";import{e as O}from"./chunk-JKSIE5IM.mjs";import{enUS as W}from"date-fns/locale";import{useEffect as N,useMemo as q,useState as E}from"react";import{format as M}from"date-fns";import{createContext as J}from"react";var I=J({isOpen:!1,setIsOpen:()=>{}}),K=()=>{let r=typeof window<"u"?window.navigator.language:"en-US",l=new Intl.DateTimeFormat(r,{hour:"numeric"}).format();return Number.isInteger(Number(l))},k=(r,l,f=!1)=>{let i=!K(),o;return f?o=i?M(r,"d MMM, yyyy h:mm a",{locale:l}):M(r,"d MMM, yyyy HH:mm",{locale:l}):o=M(r,"d MMM, yyyy",{locale:l}),o},U=(r,l)=>{let{toYear:f,fromYear:i,fromMonth:o,toMonth:g,fromDay:P,toDay:y}=l;if(r&&r.length>0){let w=i,b=f;r.forEach(e=>{var v,R,$,c,x,h;if("date"in e){let d=e.date.getFullYear();if(i&&d<i)throw new Error(`Preset ${e.label} is before fromYear ${w}.`);if(f&&d>f)throw new Error(`Preset ${e.label} is after toYear ${b}.`);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(P&&e.date.getDate()<P.getDate())throw new Error(`Preset ${e.label} is before fromDay ${P}.`);if(y&&e.date.getDate()>y.getDate())throw new Error(`Preset ${e.label} is after toDay ${M(y,"MMM dd, yyyy")}.`)}if("dateRange"in e){let d=(v=e.dateRange.from)==null?void 0:v.getFullYear(),s=(R=e.dateRange.to)==null?void 0:R.getFullYear();if(d&&i&&d<i)throw new Error(`Preset ${e.label}'s 'from' is before fromYear ${w}.`);if(s&&f&&s>f)throw new Error(`Preset ${e.label}'s 'to' is after toYear ${b}.`);if(o){let n=($=e.dateRange.from)==null?void 0:$.getMonth();if(n&&n<o.getMonth())throw new Error(`Preset ${e.label}'s 'from' is before fromMonth ${M(o,"MMM, yyyy")}.`)}if(g){let n=(c=e.dateRange.to)==null?void 0:c.getMonth();if(n&&n>g.getMonth())throw new Error(`Preset ${e.label}'s 'to' is after toMonth ${M(g,"MMM, yyyy")}.`)}if(P){let n=(x=e.dateRange.from)==null?void 0:x.getDate();if(n&&n<P.getDate())throw new Error(`Preset ${e.dateRange.from}'s 'from' is before fromDay ${M(P,"MMM dd, yyyy")}.`)}if(y){let n=(h=e.dateRange.to)==null?void 0:h.getDate();if(n&&n>y.getDate())throw new Error(`Preset ${e.label}'s 'to' is after toDay ${M(y,"MMM dd, yyyy")}.`)}}})}};import{jsx as m,jsxs as Z}from"react/jsx-runtime";var X=({value:r,defaultValue:l,presetId:f,onChange:i,presets:o,disabled:g,disableNavigation:P,disabledDays:y,showYearNavigation:w=!1,locale:b=W,placeholder:e="Select date range",hasError:v,align:R="center",className:$,...c})=>{let{isDesktop:x}=p(),[h,d]=E(!1),[s,n]=E(o&&f?o==null?void 0:o.find(({id:a})=>a===f):void 0),[t,u]=E((s==null?void 0:s.dateRange)??r??l??void 0),[A,S]=E(t==null?void 0:t.from),L=q(()=>t,[h]);N(()=>{u(r)},[r]),N(()=>{let a=o==null?void 0:o.find(({id:Y})=>Y===f);n(a),u((a==null?void 0:a.dateRange)??r??l)},[o,f]),N(()=>{h?t&&S(t.from):S(t==null?void 0:t.from)},[h]);let B=(a,Y)=>{let D=(t==null?void 0:t.from)&&(t==null?void 0:t.to)?{from:Y}:a;u(D),n(void 0),(D==null?void 0:D.from)&&(D==null?void 0:D.to)&&(i==null||i(D),d(!1))},Q=a=>{u(a.dateRange),n(a),i==null||i(a.dateRange,a),d(!1)},V=()=>{u(L),d(!1)},z=a=>{a||V(),d(a)},G=q(()=>t?`${t.from?k(t.from,b):""} - ${t.to?k(t.to,b):""}`:null,[t,b]);return m(I.Provider,{value:{isOpen:h,setIsOpen:d},children:m(C,{align:R,openPopover:h,setOpenPopover:z,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-44","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(F,{currentValue:t,presets:o,onSelect:Q})})}),m("div",{className:"scrollbar-hide overflow-x-scroll",children:m(T,{mode:"range",selected:t,onSelect:B,month:A,onMonthChange:S,numberOfMonths:x?2:1,disabled:y,disableNavigation:P,showYearNavigation:w,locale:b,className:"scrollbar-hide overflow-x-scroll",classNames:{months:"flex flex-row divide-x divide-gray-200 overflow-x-scroll scrollbar-hide"},...c})})]})}),children:m(H,{placeholder:e,disabled:g,className:$,hasError:v,"aria-required":c.required||c["aria-required"],"aria-invalid":c["aria-invalid"],"aria-label":c["aria-label"],"aria-labelledby":c["aria-labelledby"],children:(s==null?void 0:s.label)??G})})})};function ge({presets:r,...l}){return r&&U(r,l),m(X,{presets:r,...l})}export{I as a,ge as b};
