/**
 * Shows/hides audit retention field based on the audit-enabled toggle.
 */
import { syncControlState, createConditionalToggleInit } from '../core/app-conditional-controls.js';

const initRoot = (root) => {
  const enabledInput = root.querySelector('[data-audit-enabled-toggle]');
  const dependent = root.querySelector('[data-audit-when-enabled]');
  if (!(enabledInput instanceof HTMLInputElement) || !(dependent instanceof HTMLElement)) {
    return null;
  }
  const sync = () => syncControlState(dependent, enabledInput.checked);
  enabledInput.addEventListener('change', sync);
  sync();
  return () => {
    enabledInput.removeEventListener('change', sync);
  };
};

export const initSettingsAudit = createConditionalToggleInit({
  rootSelector: '[data-settings-audit-root]',
  boundKey: 'settingsAuditBound',
  initRoot,
});
