export const textInput = () => {
  function passwordToggle(id, toggle) {
    var x = document.getElementById(id);
    if (x.type === 'password') {
      x.type = 'text';
      toggle.classList.add('ds-btn--selected');
      toggle.setAttribute('aria-checked', 'true');
      toggle.setAttribute('aria-label', 'Dölj lösenord');
    } else {
      x.type = 'password';
      toggle.classList.remove('ds-btn--selected');
      toggle.setAttribute('aria-checked', 'false');
      toggle.setAttribute('aria-label', 'Visa lösenord');
    }
  }

  const passwordInputs = document.querySelectorAll('.ds-text-input [type=password]');
  passwordInputs.forEach((input) => {
    const toggle = document.getElementById(`${input.id}-password-toggle`);
    if (toggle) {
      toggle.addEventListener('click', () => {
        passwordToggle(input.id, toggle);
      });
    }
  });
};

export default textInput;
