// @ts-check
(()=>{
  'use strict';
  const App=window.IUS_TEMPLATE,{state,utils}=App;
  function t(key){return App.data.translations?.[state.locale]?.[key]??App.data.translations?.en?.[key]??key}
  function apply(){
    document.documentElement.lang=state.locale==='ja'?'ja':'en-GB';document.documentElement.dataset.locale=state.locale;
    utils.$$('[data-i18n]').forEach(el=>{el.textContent=t(el.dataset.i18n)});
    utils.$$('[data-i18n-lines]').forEach(el=>{
      const lines=String(t(el.dataset.i18nLines)).split('\n');el.replaceChildren();lines.forEach((line,index)=>{if(index)el.append(document.createElement('br'));const node=index===1&&el.tagName==='H1'?document.createElement('em'):document.createTextNode(line);if(node.nodeType===1)node.textContent=line;el.append(node)});
    });
    utils.$$('[data-i18n-placeholder]').forEach(el=>el.setAttribute('placeholder',t(el.dataset.i18nPlaceholder)));
    try{localStorage.setItem('ius-template-locale-v1',state.locale)}catch(_){}
    const config=App.data.siteConfig?.brand||{};
    utils.$$('[data-brand-name]').forEach(el=>el.innerHTML=`${utils.escapeHtml(config.name||'YOUR BRAND')}<sup>®</sup>`);
    utils.$$('[data-brand-descriptor]').forEach(el=>el.textContent=config.descriptor||'DESCRIPTOR · LOCATION');
    utils.$$('[data-brand-plain]').forEach(el=>el.textContent=(config.name||'Your Brand').replace(/YOUR BRAND/,'Your Brand'));
    document.dispatchEvent(new CustomEvent('ius:localechange',{detail:{locale:state.locale}}));
    App.modules.theme?.apply({persistState:false});
  }
  function init(){utils.$('[data-locale-toggle]')?.addEventListener('click',()=>{App.setState({locale:state.locale==='en'?'ja':'en'});apply()});apply()}
  App.modules.i18n={init,apply,t};
})();
