const initTelemetrySettings = () => {
  const toggle = document.querySelector('[data-telemetry-enabled-toggle]');
  const samplingFieldset = document.querySelector('[data-telemetry-sampling-fieldset]');
  const samplingRow = document.querySelector('[data-telemetry-sampling-row]');
  const samplingSelect = document.querySelector('[data-telemetry-sampling-select]');

  if (!toggle || !samplingFieldset) {
    return;
  }

  const syncSamplingVisibility = () => {
    const active = Boolean(toggle.checked);
    samplingFieldset.hidden = !active;
    if (samplingRow) {
      samplingRow.hidden = !active;
    }
    if (samplingSelect) {
      samplingSelect.disabled = !active || toggle.disabled;
    }
  };

  toggle.addEventListener('change', syncSamplingVisibility);
  syncSamplingVisibility();
};

if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', initTelemetrySettings);
} else {
  initTelemetrySettings();
}
