import { optionalEl, warnOnce } from '../core/app-dom.js';

export function initTemplateFeature(root = document) {
  const host = optionalEl('[data-template-feature]');
  if (!host) {
    return;
  }
  if (host.dataset.templateFeatureBound === '1') {
    return;
  }
  host.dataset.templateFeatureBound = '1';

  const action = host.querySelector('[data-template-action]');
  if (!action) {
    warnOnce('UI_EL_MISSING', 'Missing [data-template-action]', { module: 'template-feature' });
    return;
  }

  action.addEventListener('click', () => {
    host.classList.toggle('is-active');
  });
}

if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', () => initTemplateFeature());
} else {
  initTemplateFeature();
}
