<?php
/**
 * Eryndo Theme Settings
 *
 * This file handles the registration of theme settings with WordPress REST API
 * so they can be accessed and updated from the block editor.
 *
 * @package Eryndo
 * @since 1.0.0
 */

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Register theme settings
 */
function eryndo_register_theme_settings() {
    // Register the scroll-to-top setting
    register_setting(
        'theme',
        'eryndo_scroll_top',
        array(
            'type'         => 'boolean',
            'description'  => __('Enable or disable the scroll to top button', 'eryndo'),
            'show_in_rest' => true,
            'default'      => false,
        )
    );

    // Register the hover effects setting
    register_setting(
        'theme',
        'eryndo_hover_effects',
        array(
            'type'         => 'boolean',
            'description'  => __('Enable or disable hover effects for interactive elements', 'eryndo'),
            'show_in_rest' => true,
            'default'      => true,
        )
    );
}
add_action('init', 'eryndo_register_theme_settings');

/**
 * Update settings when saved in the Site Editor
 */
function eryndo_update_settings_from_rest($response, $handler, $request) {
    // Check if we're getting site settings
    if ($request->get_route() !== '/wp/v2/settings') {
        return $response;
    }

    // Get the updated values from the response
    $params = $request->get_params();
    
    // Update our custom settings if they've been changed
    if (isset($params['eryndo_scroll_top'])) {
        update_option('eryndo_scroll_top', (bool) $params['eryndo_scroll_top']);
    }

    if (isset($params['eryndo_hover_effects'])) {
        update_option('eryndo_hover_effects', (bool) $params['eryndo_hover_effects']);
    }

    return $response;
}
add_filter('rest_request_after_callbacks', 'eryndo_update_settings_from_rest', 10, 3);