import{r as F,j as o}from"./app-BBZbvTSO.js";import H from"./FieldBase-C0gzKbbb.js";import{h as d}from"./moment-C5S46NFB.js";import{D as f}from"./date-picker-D96g6NFs.js";import{B as $}from"./button-CZmOQP7j.js";import{I as P}from"./input-error-Bz7f2c6i.js";import{T as Y}from"./trash-2-BBs2y9vd.js";import{P as B}from"./plus-D8fMMvfS.js";import{f as j}from"./format-C_TaVLmE.js";/* empty css            */import"./label-BPDCCRHl.js";import"./index-13DH8-b5.js";import"./utils-YhN_nlHn.js";import"./chevron-left-CbuOobkA.js";import"./chevron-right-fDICdnKB.js";import"./chevron-down-DDM9YQ48.js";import"./index-CbDI-tXU.js";import"./Combination-9YgcTppv.js";import"./index-hDbW6ayQ.js";import"./floating-ui.dom-DtF1XweH.js";import"./index-AiAmtg3H.js";import"./index-C1VwruLz.js";import"./input-dmj4QjAp.js";import"./calendar-DoGBS4zB.js";import"./clock-gA1FmRyJ.js";function it({field:t,value:r,onChange:u,processing:p,errors:D}){var N,T,A,v,x;const[E,R]=F.useState(()=>{var a,e,l;return!((a=t.options)!=null&&a.mode)||((e=t.options)==null?void 0:e.mode)!=="range"?[]:((l=t.options)!=null&&l.repeatable?Array.isArray(r)?r:[{value:null}]:[{value:r}]).map(i=>{const n=i==null?void 0:i.value;if(n&&n.includes(" - ")){const[m,c]=n.split(" - ");return{start:d(m).toDate(),end:c?d(c).toDate():void 0}}return{}})}),V=()=>{if(!Array.isArray(r)){u(t,[{value:null}]);return}const s=[...r,{value:null}];u(t,s)},k=s=>{if(!Array.isArray(r))return;const a=[...r];a.splice(s,1),u(t,a.map(e=>({value:e.value})))},h=(s,a)=>{var l,i,n;if(!s){if((l=t.options)!=null&&l.repeatable&&typeof a=="number"){const m=Array.isArray(r)?[...r]:[{value:null}];m[a]={value:null},u(t,m)}else u(t,null);return}let e=null;if("from"in s){if(s.from){const m="yyyy-MM-dd HH:mm:ss",c=j(s.from,m),w=s.to?j(s.to,m):null;s.to&&(e=`${c} - ${w}`)}}else(i=t.options)!=null&&i.includeTime?e=j(s,"yyyy-MM-dd HH:mm:ss"):e=j(s,"yyyy-MM-dd");if((n=t.options)!=null&&n.repeatable&&typeof a=="number"){const m=Array.isArray(r)?[...r]:[{value:null}];e&&(m[a]={value:e},u(t,m))}else u(t,e)},b=(s,a,e)=>{var l,i;if(a&&typeof e=="number"){const n=[...E];n[e]={...n[e],[s]:a},R(n);const{start:m,end:c}=n[e];if(!(m&&c))return;const w=(l=t.options)!=null&&l.includeTime?"yyyy-MM-dd HH:mm:ss":"yyyy-MM-dd",M=`${j(m,w)} - ${j(c,w)}`;if((i=t.options)!=null&&i.repeatable){const S=Array.isArray(r)?[...r]:[{value:null}];S[e]={value:M},u(t,S)}else u(t,M)}},y=(s,a)=>{if(s){if(s.includes(" - ")){const[e,l]=s.split(" - ");if(a==="start")return d(e).toDate();if(a==="end")return l?d(l).toDate():void 0;const i=d(e).toDate(),n=l?d(l).toDate():void 0;return{from:i,to:n}}return d(d.parseZone(s).format("YYYY-MM-DD HH:mm:ss")).toDate()}};if((N=t.options)!=null&&N.repeatable){const s=Array.isArray(r)?r:[{value:null}];return o.jsx(H,{field:t,value:r,onChange:u,processing:p,errors:D,children:o.jsxs("div",{className:"space-y-2 w-full flex-1",children:[s.map((a,e)=>{var l,i,n,m;return o.jsxs("div",{className:"space-y-1",children:[o.jsxs("div",{className:"flex gap-2 w-full items-start",children:[((l=t.options)==null?void 0:l.mode)==="range"?o.jsxs("div",{className:"grid grid-cols-2 gap-2 w-full",children:[o.jsx(f,{date:y(a.value,"start"),onSelect:c=>b("start",c,e),disabled:p,placeholder:"Start date",includeTime:(i=t.options)==null?void 0:i.includeTime,className:"w-full"},`${e}-start`),o.jsx(f,{date:y(a.value,"end"),onSelect:c=>b("end",c,e),disabled:p,placeholder:"End date",includeTime:(n=t.options)==null?void 0:n.includeTime,className:"w-full"},`${e}-end`)]}):o.jsx("div",{className:"flex-1",children:o.jsx(f,{date:y(a.value),onSelect:c=>h(c,e),disabled:p,placeholder:t.placeholder||"Select date",includeTime:(m=t.options)==null?void 0:m.includeTime,className:"w-full"},e)}),e!==0?o.jsx($,{type:"button",variant:"destructive",size:"icon",onClick:()=>k(e),disabled:p,children:o.jsx(Y,{className:"h-4 w-4"})}):o.jsx("div",{className:"w-9"})]}),o.jsx(P,{message:D[`data.${t.name}.${e}.value`]})]},e)}),o.jsxs($,{type:"button",variant:"outline",size:"sm",onClick:V,disabled:p,className:"text-xs",children:[o.jsx(B,{className:"h-4 w-4 mr-1"}),"Add a new line"]})]})})}return o.jsx(H,{field:t,value:r,onChange:u,processing:p,errors:D,children:((T=t.options)==null?void 0:T.mode)==="range"?o.jsxs("div",{className:"grid grid-cols-2 gap-2 w-full",children:[o.jsx(f,{date:y(r,"start"),onSelect:s=>b("start",s,0),disabled:p,placeholder:"Start date",includeTime:(A=t.options)==null?void 0:A.includeTime,className:"w-full"}),o.jsx(f,{date:y(r,"end"),onSelect:s=>b("end",s,0),disabled:p,placeholder:"End date",includeTime:(v=t.options)==null?void 0:v.includeTime,className:"w-full"})]}):o.jsx(f,{date:y(r),onSelect:s=>h(s),disabled:p,placeholder:t.placeholder||"Select date",includeTime:(x=t.options)==null?void 0:x.includeTime,className:"w-full"})})}export{it as default};
