document.addEventListener('DOMContentLoaded', function () {
  initConfirmActions();
  initLiveShippingVisibility();
  initAppearancePresetUi();
});

function initConfirmActions() {
  document.addEventListener('click', function (event) {
    var confirmTrigger = event.target.closest('.wcmv-js-confirm');
    if (!confirmTrigger) {
      return;
    }
    var message = confirmTrigger.getAttribute('data-confirm') || 'Are you sure?';
    if (!window.confirm(message)) {
      event.preventDefault();
    }
  });
}

function initLiveShippingVisibility() {
  var providerSelect = document.getElementById('wcmv-live-shipping-provider');
  var apiModeSelect = document.getElementById('wcmv-live-shipping-api-mode');
  var providerRows = document.querySelectorAll('.wcmv-provider-row');

  if (!providerSelect || !providerRows.length) {
    return;
  }

  function getActiveProvider() {
    return providerSelect.value || 'easypost';
  }

  function getActiveEnv() {
    if (!apiModeSelect) {
      return 'test';
    }
    return apiModeSelect.value === 'live' ? 'live' : 'test';
  }

  function applyVisibility() {
    var activeProvider = getActiveProvider();
    var activeEnv = getActiveEnv();

    for (var i = 0; i < providerRows.length; i++) {
      var row = providerRows[i];
      var matchProvider = row.classList.contains('wcmv-provider-' + activeProvider);
      var matchEnv = row.classList.contains('wcmv-env-' + activeEnv);
      row.classList.toggle('wcmv-hidden', !(matchProvider && matchEnv));
    }
  }

  providerSelect.addEventListener('change', applyVisibility);
  if (apiModeSelect) {
    apiModeSelect.addEventListener('change', applyVisibility);
  }
  applyVisibility();
}

function initAppearancePresetUi() {
  var presetInputs = document.querySelectorAll('input[name="stakmv_theme_preset_choice"]');
  if (!presetInputs.length) {
    return;
  }

  var customPalette = document.getElementById('wcmv-custom-palette-section');

  function refreshState() {
    var activeValue = '';

    for (var i = 0; i < presetInputs.length; i++) {
      if (presetInputs[i].checked) {
        activeValue = presetInputs[i].value;
        break;
      }
    }

    for (var j = 0; j < presetInputs.length; j++) {
      var card = presetInputs[j].closest('.wcmv-preset-card');
      if (!card) {
        continue;
      }
      card.classList.toggle('is-active', presetInputs[j].value === activeValue);
    }

    if (customPalette) {
      customPalette.classList.toggle('wcmv-hidden', activeValue !== 'custom');
    }
  }

  for (var k = 0; k < presetInputs.length; k++) {
    presetInputs[k].addEventListener('change', function (event) {
      refreshState();

      var selected = event.target && event.target.value ? event.target.value : '';
      if (selected === 'custom') {
        return;
      }

      var form = event.target.closest('form');
      if (!form) {
        return;
      }

      var saveInput = document.createElement('input');
      saveInput.type = 'hidden';
      saveInput.name = 'save_appearance';
      saveInput.value = '1';
      form.appendChild(saveInput);
      form.submit();
    });
  }

  refreshState();
}
