/**
 * Company profile: extra fields panel + tax inclusive label.
 */
(function () {
    const toggleBtn = document.getElementById('btn-toggle-extra-fields');
    const panel = document.getElementById('profile-extra-fields-panel');
    const chevron = toggleBtn?.querySelector('.billing-profile-extra__chevron');
    const labelEl = toggleBtn?.querySelector('[data-toggle-label]');

    function setPanelOpen(open) {
        if (!panel || !toggleBtn) return;
        panel.classList.toggle('d-none', !open);
        panel.hidden = !open;
        toggleBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
        chevron?.classList.toggle('is-open', open);
        if (labelEl) {
            labelEl.textContent = open ? 'Hide extra field options' : 'Show extra field options';
        }
    }

    toggleBtn?.addEventListener('click', () => {
        const isHidden = panel?.classList.contains('d-none') || panel?.hidden;
        setPanelOpen(Boolean(isHidden));
    });

    const taxSwitch = document.getElementById('tax_inclusive_rates');
    const taxLabel = document.getElementById('tax_inclusive_rates_label');
    taxSwitch?.addEventListener('change', () => {
        if (taxLabel) taxLabel.textContent = taxSwitch.checked ? 'ON' : 'OFF';
    });
})();
