/******/ (() => { // webpackBootstrap
/*!**********************************************!*\
  !*** ./assets/src/blocks/login-icon/view.js ***!
  \**********************************************/
document.addEventListener('DOMContentLoaded', function () {
  const loginIcons = document.querySelectorAll('.thim-blocks-login-icon');
  loginIcons.forEach(function (iconBlock) {
    const enablePopup = iconBlock.dataset.enablePopup === 'true';
    if (!enablePopup) {
      return;
    }

    // Get popup content from inner blocks (hidden div)
    const popupContent = iconBlock.querySelector('.thim-login-popup-inner-content');
    if (!popupContent) {
      console.warn('No popup content found in login-icon block');
      return;
    }
    const loginLink = iconBlock.querySelector('.thim-login-login');
    const registerLink = iconBlock.querySelector('.thim-login-register');
    const labelsConfig = {
      showLabels: iconBlock.dataset.showLabels === 'true' ? '1' : '0',
      lblUsername: iconBlock.dataset.lblUsername || '',
      lblEmail: iconBlock.dataset.lblEmail || '',
      lblPassword: iconBlock.dataset.lblPassword || '',
      lblRepeatPassword: iconBlock.dataset.lblRepeat || '',
      lblUsernameOrEmail: iconBlock.dataset.lblUserOr || ''
    };
    if (loginLink) {
      loginLink.addEventListener('click', function (e) {
        e.preventDefault();
        showLoginPopup(popupContent, 'login', labelsConfig);
      });
    }
    if (registerLink) {
      registerLink.addEventListener('click', function (e) {
        e.preventDefault();
        showLoginPopup(popupContent, 'register', labelsConfig);
      });
    }
  });
  function showLoginPopup(contentElement, initialAction, labelsConfig) {
    // Create popup overlay
    const overlay = document.createElement('div');
    overlay.className = 'thim-login-popup-overlay';
    const popup = document.createElement('div');
    popup.className = 'thim-login-popup';
    const closeBtn = document.createElement('button');
    closeBtn.className = 'thim-login-popup-close';
    closeBtn.setAttribute('aria-label', thimBlocksLoginPopup?.i18n?.close || 'Close');
    closeBtn.innerHTML = '&times;';
    const contentWrapper = document.createElement('div');
    contentWrapper.className = 'thim-login-popup-content';

    // Clone the static content (title, description, etc.)
    const clonedContent = contentElement.cloneNode(true);
    clonedContent.style.display = 'block';
    contentWrapper.appendChild(clonedContent);

    // Add form container
    const formContainer = document.createElement('div');
    formContainer.className = 'thim-login-popup-form-container';
    formContainer.innerHTML = '<div class="thim-login-popup-loading">' + (thimBlocksLoginPopup?.i18n?.loading || 'Loading...') + '</div>';
    contentWrapper.appendChild(formContainer);
    popup.appendChild(closeBtn);
    popup.appendChild(contentWrapper);
    overlay.appendChild(popup);
    document.body.appendChild(overlay);
    document.body.style.overflow = 'hidden';

    // Close popup handlers
    closeBtn.addEventListener('click', closePopup);
    overlay.addEventListener('click', function (e) {
      if (e.target === overlay) {
        closePopup();
      }
    });
    function closePopup() {
      document.body.removeChild(overlay);
      document.body.style.overflow = '';
    }

    // Load form via AJAX
    loadForm(initialAction, formContainer, contentWrapper, overlay, labelsConfig);
  }
  function loadForm(action, formContainer, contentWrapper, overlay, labelsConfig) {
    const formData = new FormData();
    formData.append('action', 'thim_blocks_get_login_form');
    formData.append('form_action', action);
    formData.append('nonce', thimBlocksLoginPopup?.nonce || '');
    if (labelsConfig) {
      formData.append('showLabels', labelsConfig.showLabels);
      formData.append('lblUsername', labelsConfig.lblUsername);
      formData.append('lblEmail', labelsConfig.lblEmail);
      formData.append('lblPassword', labelsConfig.lblPassword);
      formData.append('lblRepeatPassword', labelsConfig.lblRepeatPassword);
      formData.append('lblUsernameOrEmail', labelsConfig.lblUsernameOrEmail);
    }
    fetch(thimBlocksLoginPopup?.ajaxUrl || '/wp-admin/admin-ajax.php', {
      method: 'POST',
      body: formData
    }).then(response => response.json()).then(data => {
      if (data.success) {
        formContainer.innerHTML = data.data.html;
        initFormHandlers(formContainer, contentWrapper, overlay, labelsConfig);
      } else {
        formContainer.innerHTML = '<p class="error">' + (thimBlocksLoginPopup?.i18n?.failed_load || 'Failed to load form.') + '</p>';
      }
    }).catch(error => {
      formContainer.innerHTML = '<p class="error">' + (thimBlocksLoginPopup?.i18n?.error_occurred || 'An error occurred.') + '</p>';
    });
  }
  function initFormHandlers(formContainer, contentWrapper, overlay, labelsConfig) {
    // Handle toggle password
    const toggleButtons = formContainer.querySelectorAll('.toggle-password');
    toggleButtons.forEach(function (btn) {
      btn.addEventListener('click', function () {
        const targetId = btn.getAttribute('data-target');
        const input = formContainer.querySelector('#' + targetId);
        if (input) {
          input.type = input.type === 'password' ? 'text' : 'password';
        }
      });
    });

    // Handle switch between login/register/lost password
    const switchLinks = formContainer.querySelectorAll('a[data-action]');
    switchLinks.forEach(link => {
      link.addEventListener('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
        const action = link.getAttribute('data-action') || 'login';

        // Reload form with new action
        formContainer.innerHTML = '<div class="thim-login-popup-loading">' + (thimBlocksLoginPopup?.i18n?.loading || 'Loading...') + '</div>';
        loadForm(action, formContainer, contentWrapper, overlay, labelsConfig);
      });
    });

    // Handle form submission
    const form = formContainer.querySelector('form');
    if (form) {
      form.addEventListener('submit', function (e) {
        e.preventDefault();
        const formData = new FormData(form);
        formData.append('action', 'thim_blocks_submit_login_form');
        formData.append('ajax_submit', '1');
        fetch(thimBlocksLoginPopup?.ajaxUrl || '/wp-admin/admin-ajax.php', {
          method: 'POST',
          body: formData
        }).then(response => response.json()).then(data => {
          if (data.success) {
            if (data.data.redirect) {
              window.location.href = data.data.redirect;
            } else {
              window.location.reload();
            }
          } else {
            // Show error message
            const errorDiv = document.createElement('div');
            errorDiv.className = 'login-errors';
            errorDiv.innerHTML = '<p class="login-error">' + (data.data.message || thimBlocksLoginPopup?.i18n?.error_occurred || 'An error occurred') + '</p>';
            const existingError = formContainer.querySelector('.login-errors');
            if (existingError) {
              existingError.remove();
            }
            form.insertBefore(errorDiv, form.firstChild);
          }
        }).catch(error => {
          console.error('Form submission error:', error);
        });
      });
    }
  }
});
/******/ })()
;
//# sourceMappingURL=view.js.map