import{i as e}from"./chunk-aKtaBQYM.js";import{Ft as t,Mt as n,r,vt as i}from"./mui-BgU4WrXF.js";import{a,r as o,t as s}from"./Select-Bu2vmaew.js";import{t as c}from"./EditAttributesAccordion-J3XCwwrK.js";import{i as l,n as u,r as d,t as f}from"./ExpandMore-D0ycvrdR.js";import{t as p}from"./Typography-7EcQ2i3p.js";import{t as m}from"./TextField-DctlzLY-.js";import{t as h}from"./dayjs.min-C4hTNXA3.js";import{Dt as g,Y as _,ct as v,pt as y,w as b}from"./index-bel1r0Sr.js";import{n as x,t as S}from"./useSettingsStyles-Bugi6oTj.js";import{t as C}from"./FileInput-C3yEGmbG.js";import{t as w}from"./EditItemView-BPUZddYO.js";var T=e(h(),1),E=e(t(),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(),t=n(),i=y(),[h,N]=(0,E.useState)(),[P,F]=(0,E.useState)(null),I=h&&h.data&&window.atob(h.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({...h,data:t.slice(t.indexOf(`,`)+1)})},t.readAsDataURL(e)}},V=v(async()=>{let e=await b(`/api/calendars`);t(r.refresh(await e.json()))});return(0,D.jsx)(w,{endpoint:`calendars`,item:h,setItem:N,defaultItem:{data:M()},validate:()=>h&&h.name&&h.data,onItemSaved:V,menu:(0,D.jsx)(x,{}),breadcrumbs:[`settingsTitle`,`sharedCalendar`],children:h&&(0,D.jsxs)(D.Fragment,{children:[(0,D.jsxs)(l,{defaultExpanded:!0,children:[(0,D.jsx)(u,{expandIcon:(0,D.jsx)(f,{}),children:(0,D.jsx)(p,{variant:`subtitle1`,children:i(`sharedRequired`)})}),(0,D.jsxs)(d,{className:e.details,children:[(0,D.jsx)(m,{value:h.name||``,onChange:e=>N({...h,name:e.target.value}),label:i(`sharedName`)}),(0,D.jsxs)(a,{children:[(0,D.jsx)(o,{children:i(`sharedType`)}),(0,D.jsxs)(s,{label:i(`sharedType`),value:L?`simple`:`custom`,onChange:e=>N({...h,data:e.target.value===`simple`?M():null}),children:[(0,D.jsx)(g,{value:`simple`,children:i(`calendarSimple`)}),(0,D.jsx)(g,{value:`custom`,children:i(`reportCustom`)})]})]}),L?(0,D.jsxs)(D.Fragment,{children:[(0,D.jsx)(m,{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({...h,data:j(R,5,`DTSTART;${t}`)})}}),(0,D.jsx)(m,{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({...h,data:j(R,6,`DTEND;${t}`)})}}),(0,D.jsxs)(a,{children:[(0,D.jsx)(o,{children:i(`calendarRecurrence`)}),(0,D.jsx)(s,{label:i(`calendarRecurrence`),value:z.frequency,onChange:e=>N({...h,data:j(R,7,A({frequency:e.target.value}))}),children:[`ONCE`,`DAILY`,`WEEKLY`,`MONTHLY`].map(e=>(0,D.jsx)(g,{value:e,children:i(_(`calendar`,e.toLowerCase()))},e))})]}),[`WEEKLY`,`MONTHLY`].includes(z.frequency)&&(0,D.jsxs)(a,{children:[(0,D.jsx)(o,{children:i(`calendarDays`)}),(0,D.jsx)(s,{multiple:!0,label:i(`calendarDays`),value:z.by,onChange:e=>N({...h,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)(g,{value:e.substring(0,2).toUpperCase(),children:i(_(`calendar`,e))},e)):Array.from({length:31},(e,t)=>t+1).map(e=>(0,D.jsx)(g,{value:String(e),children:e},e))})]})]}):(0,D.jsx)(C,{placeholder:i(`sharedSelectFile`),value:P,onChange:B})]})]}),(0,D.jsx)(c,{attributes:h.attributes,setAttributes:e=>N({...h,attributes:e}),definitions:{}})]})})};export{N as default};