(() => { 'use strict'; const root=document.getElementById('organizer-calendar-defaults');if(!root)return; const ja=document.documentElement.lang.startsWith('ja'),t=(en,jp)=>ja?jp:en,api='/_/api/v1/extension/organizer.calendar/'; const status=root.querySelector('[role=status]'),content=root.querySelector('[data-calendar-defaults]'); const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;}; async function request(path,options={}){const r=await fetch(api+path,{credentials:'same-origin',cache:'no-store',signal:AbortSignal.timeout(15000),...options,headers:{'Content-Type':'application/json'}});if(!r.ok||r.redirected)throw Error(t('Unable to save or load settings. Check your session and try again.','取得・保存できません。ログイン状態を確認して再度お試しください。'));return r.json();} request('calendars').then(data=>{ const form=el('form'),list=el('div');list.className='organizer-calendar-options'; for(const calendar of data.calendars){const label=el('label'),input=el('input');input.type='checkbox';input.name='calendar';input.value=calendar.id;input.checked=data.defaults.includes(calendar.id);input.disabled=root.dataset.writable!=='true';label.append(input,document.createTextNode(calendar.name));list.append(label);} const help=el('p',t('Used for calendar navigation and upcoming events, including the dashboard. If none are selected, the first available calendar is used.','カレンダー表示と直近の予定、ダッシュボードに使用します。未選択なら最初の利用可能なカレンダーを使用します。')); const save=el('button',t('Save','保存'));save.type='submit';save.disabled=root.dataset.writable!=='true';form.append(list,help,save);content.append(form); form.onsubmit=async event=>{event.preventDefault();const calendars=[...form.querySelectorAll('input:checked')].map(n=>n.value);if(calendars.length>32){status.textContent=t('Select at most 32 calendars.','32個以内で選択してください。');return;}save.disabled=true;try{await request('preferences',{method:'PUT',body:JSON.stringify({calendars})});status.textContent=t('Saved','保存しました');}catch(e){status.textContent=e.message;}finally{save.disabled=false;}}; }).catch(e=>{status.textContent=e.message;}); })();