import './core/app-telemetry.js';
import './components/app-flash-auto-dismiss.js';
import './components/app-password-hints.js';
import './components/app-password-toggle.js';

const focusPrimaryErrorNotice = () => {
  const errorNotice = document.querySelector(
    '.notice[data-variant="error"][role="alert"][tabindex="-1"]',
  );
  if (!(errorNotice instanceof HTMLElement)) {
    return;
  }
  errorNotice.focus();
};

const lockLoginSubmitButtons = () => {
  const forms = document.querySelectorAll('form[data-login-submit-lock="1"]');
  forms.forEach((form) => {
    if (!(form instanceof HTMLFormElement)) {
      return;
    }
    form.addEventListener('submit', () => {
      if (form.dataset.submitted === '1') {
        return;
      }
      form.dataset.submitted = '1';
      form.setAttribute('aria-busy', 'true');
      const submitButton = form.querySelector('button[type="submit"]');
      if (!(submitButton instanceof HTMLButtonElement)) {
        return;
      }
      submitButton.disabled = true;
      submitButton.setAttribute('aria-busy', 'true');
      submitButton.dataset.submitState = 'loading';
    });
  });
};

focusPrimaryErrorNotice();
lockLoginSubmitButtons();
