import{i as e}from"./chunk-aKtaBQYM.js";import{Rt as t,Vt as n,r,wt as i}from"./mui-B6lh3XSP.js";import{t as a}from"./EditAttributesAccordion-BaG0aySx.js";import{i as o,n as s,r as c,t as l}from"./ExpandMore-BvigPYWL.js";import{t as u}from"./TextField-DOdnjCrL.js";import{t as d}from"./Typography-DfQXhGTT.js";import{t as f}from"./dayjs.min-C4hTNXA3.js";import{Bt as p,Ft as m,Gt as h,Nt as g,mt as _,tt as v,w as y,yt as b}from"./index-KVuxnkWj.js";import{n as x,t as S}from"./useSettingsStyles-X3caKosH.js";import{t as C}from"./FileInput-DgZJzdHa.js";import{t as w}from"./EditItemView-BNkSnNuu.js";var T=e(f(),1),E=e(n(),1),D=i(),O=e=>`TZID=${Intl.DateTimeFormat().resolvedOptions().timeZone}:${e.locale(`en`).format(`YYYYMMDDTHHmmss`)}`,k=e=>{if(e.endsWith(`COUNT=1`))return{frequency:`ONCE`};let t=e.split(`;`);return{frequency:t[0].substring(11),by:t.length>1?t[1].split(`=`)[1].split(`,`):null}},A=e=>{let t=e.by&&e.by.join(`,`);switch(e.frequency){case`DAILY`:return`RRULE:FREQ=${e.frequency}`;case`WEEKLY`:return`RRULE:FREQ=${e.frequency};BYDAY=${t||`SU`}`;case`MONTHLY`:return`RRULE:FREQ=${e.frequency};BYMONTHDAY=${t||1}`;default:return`RRULE:FREQ=DAILY;COUNT=1`}},j=(e,t,n)=>window.btoa(e.map((e,r)=>r===t?n:e).join(`
`)),M=()=>window.btoa([`BEGIN:VCALENDAR`,`VERSION:2.0`,`PRODID:-//Traccar//NONSGML Traccar//EN`,`BEGIN:VEVENT`,`UID:00000000-0000-0000-0000-000000000000`,`DTSTART;${O((0,T.default)())}`,`DTEND;${O((0,T.default)().add(1,`hours`))}`,`RRULE:FREQ=DAILY`,`SUMMARY:Event`,`END:VEVENT`,`END:VCALENDAR`].join(`
`)),N=()=>{let{classes:e}=S(),n=t(),i=b(),[f,N]=(0,E.useState)(),[P,F]=(0,E.useState)(null),I=f&&f.data&&window.atob(f.data),L=I&&I.indexOf(`//Traccar//`)>0,R=I&&I.split(`
`),z=L&&k(R[7]),B=e=>{if(F(e),e){let t=new FileReader;t.onload=e=>{let{result:t}=e.target;N({...f,data:t.slice(t.indexOf(`,`)+1)})},t.readAsDataURL(e)}},V=_(async()=>{let e=await y(`/api/calendars`);n(r.refresh(await e.json()))});return(0,D.jsx)(w,{endpoint:`calendars`,item:f,setItem:N,defaultItem:{data:M()},validate:()=>f&&f.name&&f.data,onItemSaved:V,menu:(0,D.jsx)(x,{}),breadcrumbs:[`settingsTitle`,`sharedCalendar`],children:f&&(0,D.jsxs)(D.Fragment,{children:[(0,D.jsxs)(o,{defaultExpanded:!0,children:[(0,D.jsx)(s,{expandIcon:(0,D.jsx)(l,{}),children:(0,D.jsx)(d,{variant:`subtitle1`,children:i(`sharedRequired`)})}),(0,D.jsxs)(c,{className:e.details,children:[(0,D.jsx)(u,{value:f.name||``,onChange:e=>N({...f,name:e.target.value}),label:i(`sharedName`)}),(0,D.jsxs)(h,{children:[(0,D.jsx)(p,{children:i(`sharedType`)}),(0,D.jsxs)(g,{label:i(`sharedType`),value:L?`simple`:`custom`,onChange:e=>N({...f,data:e.target.value===`simple`?M():null}),children:[(0,D.jsx)(m,{value:`simple`,children:i(`calendarSimple`)}),(0,D.jsx)(m,{value:`custom`,children:i(`reportCustom`)})]})]}),L?(0,D.jsxs)(D.Fragment,{children:[(0,D.jsx)(u,{label:i(`reportFrom`),type:`datetime-local`,value:(0,T.default)(R[5].slice(-15)).locale(`en`).format(`YYYY-MM-DDTHH:mm`),onChange:e=>{let t=O((0,T.default)(e.target.value,`YYYY-MM-DDTHH:mm`));N({...f,data:j(R,5,`DTSTART;${t}`)})}}),(0,D.jsx)(u,{label:i(`reportTo`),type:`datetime-local`,value:(0,T.default)(R[6].slice(-15)).locale(`en`).format(`YYYY-MM-DDTHH:mm`),onChange:e=>{let t=O((0,T.default)(e.target.value,`YYYY-MM-DDTHH:mm`));N({...f,data:j(R,6,`DTEND;${t}`)})}}),(0,D.jsxs)(h,{children:[(0,D.jsx)(p,{children:i(`calendarRecurrence`)}),(0,D.jsx)(g,{label:i(`calendarRecurrence`),value:z.frequency,onChange:e=>N({...f,data:j(R,7,A({frequency:e.target.value}))}),children:[`ONCE`,`DAILY`,`WEEKLY`,`MONTHLY`].map(e=>(0,D.jsx)(m,{value:e,children:i(v(`calendar`,e.toLowerCase()))},e))})]}),[`WEEKLY`,`MONTHLY`].includes(z.frequency)&&(0,D.jsxs)(h,{children:[(0,D.jsx)(p,{children:i(`calendarDays`)}),(0,D.jsx)(g,{multiple:!0,label:i(`calendarDays`),value:z.by,onChange:e=>N({...f,data:j(R,7,A({...z,by:e.target.value}))}),children:z.frequency===`WEEKLY`?[`sunday`,`monday`,`tuesday`,`wednesday`,`thursday`,`friday`,`saturday`].map(e=>(0,D.jsx)(m,{value:e.substring(0,2).toUpperCase(),children:i(v(`calendar`,e))},e)):Array.from({length:31},(e,t)=>t+1).map(e=>(0,D.jsx)(m,{value:String(e),children:e},e))})]})]}):(0,D.jsx)(C,{placeholder:i(`sharedSelectFile`),value:P,onChange:B})]})]}),(0,D.jsx)(a,{attributes:f.attributes,setAttributes:e=>N({...f,attributes:e}),definitions:{}})]})})};export{N as default};