const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', async (e) => {
  const file = e.target.files[0];
  if (!file) return;

  const reader = new FileReader();
  reader.onload = async (event) => {
    try {
      const data = event.target.result;

      chrome.runtime.sendMessage(
        { action: 'importSettings', data },
        (response) => {
          if (chrome.runtime.lastError) {
            alert('Error import');
          } else if (response && response.success) {
            alert('Success import!');
            window.close();
          } else {
            alert('Error import: ' + (response ? response.error : ""));
          }
        }
      );
    } catch (error) {
      alert('Error import: invalid format');
    }
  };
  reader.readAsText(file);
});


fileInput.click();
