<?php
// pages/components/newsletter_collections_config_toggle.php
// Toggles Manual/Archive collections UI options in the per-user newsletter UI state
// and re-renders the collections list (outerHTML swap).

use Components\ConfigStore;

$method = (string) \mh_server('REQUEST_METHOD', 'GET');
if ($method !== 'POST') {
    return;
}

$action  = (string) mh_get('toggle', '', 'string');
$section = (string) mh_get('section', '', 'string');

if ($action === 'toggle_description') {
    if (!in_array($section, ['manual', 'archive'], true)) {
        return;
    }

    $nlUiState = ConfigStore::getNewsletterUiState();
    $map = isset($nlUiState['collections_show_description']) && is_array($nlUiState['collections_show_description'])
        ? $nlUiState['collections_show_description']
        : [];

    $current = !empty($map[$section]);
    $map[$section] = !$current;
    ConfigStore::setNewsletterUiState(['collections_show_description' => $map]);
}

// Re-render the collections list so the caller can swap it in (outerHTML)
require __DIR__ . '/newsletter_collection_list.php';
