<?php
if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Widgets_Admin
{
    public function __construct()
    {
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
        add_action('rest_api_init', array($this, 'register_rest_routes'));
    }

    public static function render_page()
    { ?>
		<div class="wrap puffergo-admin-wrapper">
			<div id="puffergo-widgets-root"></div>
		</div>
		<?php
    }

    public function enqueue_scripts($hook)
    {
        if ('puffergo_page_puffergo-widgets' !== $hook) {
            return;
        } $plugin_url = plugin_dir_url(PUFFERGO_PLUGIN_FILE);
        $plugin_dir = plugin_dir_path(PUFFERGO_PLUGIN_FILE);
        $asset_file = $plugin_dir . 'build/admin/widgets/index.asset.php';
        $deps = array('wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n');
        $version = time();

        if (file_exists($asset_file)) {
            $asset = include $asset_file;
            $deps = $asset['dependencies'];
            $version = $asset['version'];
        } wp_enqueue_script('puffergo-widgets-admin', $plugin_url . 'build/admin/widgets/index.js', $deps, $version, true);
        $style_file = $plugin_dir . 'build/admin/widgets/index.css';

        if (file_exists($style_file)) {
            wp_enqueue_style('puffergo-widgets-admin', $plugin_url . 'build/admin/widgets/index.css', array('puffergo-admin-tailwind', 'wp-components'), $version);
        } wp_localize_script('puffergo-widgets-admin', 'puffergoWidgetsAdmin', array('restUrl' => rest_url('puffergo/v1/widgets/config'), 'nonce' => wp_create_nonce('wp_rest'), 'schemas' => PufferGo_Global_Widgets::WIDGET_SCHEMAS, ));
    }

    public function register_rest_routes()
    {
        register_rest_route('puffergo/v1', '/widgets/config', array(array('methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'rest_get_config'), 'permission_callback' => array($this, 'check_permission'), ), array('methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'rest_save_config'), 'permission_callback' => array($this, 'check_permission'), ), ));
    }

    public function rest_get_config(): WP_REST_Response
    {
        return new WP_REST_Response(PufferGo_Global_Widgets::get_all_config(), 200);
    }

    public function rest_save_config(WP_REST_Request $request): WP_REST_Response
    {
        $body = $request->get_json_params();

        if (! is_array($body)) {
            return new WP_REST_Response(array('message' => 'Invalid data'), 400);
        } $sanitized = $this->sanitize_config($body);
        PufferGo_Global_Widgets::save_all_config($sanitized);

        return new WP_REST_Response(PufferGo_Global_Widgets::get_all_config(), 200);
    }

    public function check_permission(): bool
    {
        return current_user_can('manage_options');
    }

    private function sanitize_config(array $data): array
    {
        $out = array();

        foreach (PufferGo_Global_Widgets::WIDGET_SCHEMAS as $key => $schema) {
            $entry = isset($data[ $key ]) ? (array) $data[ $key ] : array();
            $config = isset($entry['config']) && is_array($entry['config']) ? $entry['config'] : array();
            $clean_config = array();

            foreach ($schema['default'] as $field => $default) {
                if (isset($config[ $field ])) {
                    $clean_config[ $field ] = is_bool($default) ? (bool) $config[ $field ] : sanitize_text_field((string) $config[ $field ]);
                } else {
                    $clean_config[ $field ] = $default;
                }
            } $clean_i18n = array();

            if (isset($entry['i18n']) && is_array($entry['i18n'])) {
                foreach ($entry['i18n'] as $lang => $texts) {
                    $lang_key = sanitize_text_field($lang);
                    $clean_i18n[ $lang_key ] = array();

                    foreach ($schema['i18n_fields'] as $field) {
                        if (isset($texts[ $field ])) {
                            $clean_i18n[ $lang_key ][ $field ] = sanitize_text_field((string) $texts[ $field ]);
                        }
                    }
                }
            } $out[ $key ] = array('enabled' => ! empty($entry['enabled']), 'config' => $clean_config, 'i18n' => $clean_i18n, );
        }

return $out;
    }
}
