// @ts-check
(()=>{
  'use strict';
  const App=window.IUS_TEMPLATE,{utils,state}=App,q=s=>utils.$(s);
  const fieldMap=[['tradingName','company.trading'],['legalName','company.legal'],['registrationNumber','company.number'],['vatNumber','company.vat'],['registeredOffice','company.registered'],['tradingAddress','company.address'],['jurisdiction','company.jurisdiction'],['email','company.email'],['telephone','company.phone'],['hours','company.hours'],['accessibilityEmail','company.access'],['privacyEmail','company.privacy']];
  function render(){const data=App.data.siteConfig.company,host=q('[data-company-details]');if(!host)return;host.replaceChildren();const dl=document.createElement('dl');fieldMap.forEach(([key,label])=>{if(!data[key])return;const div=document.createElement('div'),dt=document.createElement('dt'),dd=document.createElement('dd');dt.textContent=App.modules.i18n.t(label);if(key.toLowerCase().includes('email')){const a=document.createElement('a');a.href=`mailto:${data[key]}`;a.textContent=data[key];dd.append(a)}else dd.textContent=data[key];div.append(dt,dd);dl.append(div)});host.append(dl);q('[data-company-map]').href=data.mapUrl||'#'}
  function open(trigger){render();utils.openDialog(q('[data-company-dialog]'),trigger)}
  function close(){utils.closeDialog(q('[data-company-dialog]'))}
  function printDetails(){const data=App.data.siteConfig.company,print=q('[data-print-document]');print.replaceChildren();const header=document.createElement('header'),h=document.createElement('h1');h.textContent=App.modules.i18n.t('company.title');const meta=document.createElement('div');meta.className='print-meta';meta.innerHTML=`<span>${utils.escapeHtml(data.legalName)}</span><span>${utils.escapeHtml(new Intl.DateTimeFormat(state.locale==='ja'?'ja-JP':'en-GB',{dateStyle:'long'}).format(new Date()))}</span>`;header.append(h,meta);const dl=document.createElement('dl');fieldMap.forEach(([key,label])=>{if(!data[key])return;const dt=document.createElement('dt'),dd=document.createElement('dd');dt.textContent=App.modules.i18n.t(label);dd.textContent=data[key];dl.append(dt,dd)});const footer=document.createElement('footer');footer.innerHTML='<span>Company details</span><span>Designed by I/US brand. · iusmusic.com</span>';print.append(header,dl,footer);window.print()}
  function init(){utils.$$('[data-company-open]').forEach(b=>b.addEventListener('click',()=>open(b)));utils.$$('[data-company-close]').forEach(b=>b.addEventListener('click',close));q('[data-company-copy-address]')?.addEventListener('click',async()=>{await utils.copy(App.data.siteConfig.company.registeredOffice);q('[data-company-copy-address]').textContent=App.state.locale==='ja'?'コピーしました':'Address copied'});q('[data-company-copy-email]')?.addEventListener('click',async()=>{await utils.copy(App.data.siteConfig.company.email);q('[data-company-copy-email]').textContent=App.state.locale==='ja'?'コピーしました':'Email copied'});q('[data-company-print]')?.addEventListener('click',printDetails);document.addEventListener('ius:localechange',()=>{if(q('[data-company-dialog]')?.open)render()})}
  App.modules.company={init,open,close,render};
})();
