<?php
/**
 * Biscotti WPBakery (Visual Composer) Elements
 *
 * Registers WPBakery elements for all Biscotti shortcodes using vc_map().
 * Each element maps directly to an existing shortcode.
 *
 * @package Biscotti_CMP
 * @since 3.1.0
 */

if (!defined('ABSPATH')) {
    exit;
}

// Guard: only load if WPBakery is active
if (!function_exists('vc_map')) {
    return;
}

class Biscotti_WPBakery_Elements
{
    private static $instance = null;

    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function __construct()
    {
        add_action('vc_before_init', [$this, 'register_elements']);
    }

    private function get_lang_options()
    {
        // vc_map expects [ 'Display label' => 'value' ].
        $opts = [ esc_html__( 'Auto (from WordPress)', 'biscotti-cmp' ) => 'auto' ];
        foreach ( Biscotti_Languages::endonyms() as $bsc_code => $bsc_name ) {
            $opts[ $bsc_name ] = $bsc_code;
        }
        return $opts;
    }

    public function register_elements()
    {
        // 1. Cookie Table
        vc_map([
            'name' => esc_html__('Biscotti Cookie Table', 'biscotti-cmp'),
            'base' => 'biscotti_cookies',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-editor-table',
            'description' => esc_html__('Cookie table for privacy policy', 'biscotti-cmp'),
            'params' => [
                [
                    'type' => 'dropdown',
                    'heading' => esc_html__('Category', 'biscotti-cmp'),
                    'param_name' => 'category',
                    'value' => [
                        esc_html__('All', 'biscotti-cmp') => 'all',
                        esc_html__('Essential', 'biscotti-cmp') => 'essential',
                        esc_html__('Functional', 'biscotti-cmp') => 'functional',
                        esc_html__('Analytics', 'biscotti-cmp') => 'analytics',
                        esc_html__('Marketing', 'biscotti-cmp') => 'marketing',
                    ],
                    'std' => 'all',
                ],
                [
                    'type' => 'dropdown',
                    'heading' => esc_html__('Show Duration', 'biscotti-cmp'),
                    'param_name' => 'show_duration',
                    'value' => [esc_html__('Yes', 'biscotti-cmp') => 'true', esc_html__('No', 'biscotti-cmp') => 'false'],
                    'std' => 'true',
                ],
                [
                    'type' => 'dropdown',
                    'heading' => esc_html__('Show Provider', 'biscotti-cmp'),
                    'param_name' => 'show_provider',
                    'value' => [esc_html__('Yes', 'biscotti-cmp') => 'true', esc_html__('No', 'biscotti-cmp') => 'false'],
                    'std' => 'true',
                ],
            ],
        ]);

        // 2. Consent Link
        vc_map([
            'name' => esc_html__('Biscotti Consent Link', 'biscotti-cmp'),
            'base' => 'biscotti_consent_link',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-admin-links',
            'description' => esc_html__('Link to reopen consent banner', 'biscotti-cmp'),
            'params' => [
                [
                    'type' => 'textfield',
                    'heading' => esc_html__('Link Text', 'biscotti-cmp'),
                    'param_name' => 'text',
                    'value' => esc_html__('Change cookie settings', 'biscotti-cmp'),
                ],
                [
                    'type' => 'textfield',
                    'heading' => esc_html__('CSS Class', 'biscotti-cmp'),
                    'param_name' => 'class',
                    'value' => 'biscotti-consent-link',
                ],
            ],
        ]);

        // 3. Settings Link
        vc_map([
            'name' => esc_html__('Biscotti Settings Link', 'biscotti-cmp'),
            'base' => 'biscotti_settings_link',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-admin-generic',
            'description' => esc_html__('Link to open cookie settings panel', 'biscotti-cmp'),
            'params' => [
                [
                    'type' => 'textfield',
                    'heading' => esc_html__('Link Text', 'biscotti-cmp'),
                    'param_name' => 'text',
                    'value' => esc_html__('Cookie Settings', 'biscotti-cmp'),
                ],
                [
                    'type' => 'textfield',
                    'heading' => esc_html__('CSS Class', 'biscotti-cmp'),
                    'param_name' => 'class',
                    'value' => 'biscotti-settings-link',
                ],
            ],
        ]);

        // 4. Data Export
        vc_map([
            'name' => esc_html__('Biscotti Data Export', 'biscotti-cmp'),
            'base' => 'biscotti_data_export',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-download',
            'description' => esc_html__('GDPR Art. 15 data export button', 'biscotti-cmp'),
            'params' => [],
        ]);

        // 5. Do Not Sell (CCPA)
        vc_map([
            'name' => esc_html__('Biscotti Do Not Sell (CCPA)', 'biscotti-cmp'),
            'base' => 'biscotti_do_not_sell',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-shield',
            'description' => esc_html__('CCPA "Do Not Sell My Data" link', 'biscotti-cmp'),
            'params' => [
                [
                    'type' => 'textfield',
                    'heading' => esc_html__('Link Text', 'biscotti-cmp'),
                    'param_name' => 'text',
                    'value' => esc_html__('Do Not Sell My Data', 'biscotti-cmp'),
                ],
            ],
        ]);

        // Legal document elements
        $legal_params = [
            [
                'type' => 'dropdown',
                'heading' => esc_html__('Enable Cache', 'biscotti-cmp'),
                'param_name' => 'cache',
                'value' => [esc_html__('Yes', 'biscotti-cmp') => 'true', esc_html__('No', 'biscotti-cmp') => 'false'],
                'std' => 'true',
            ],
            [
                'type' => 'dropdown',
                'heading' => esc_html__('Language', 'biscotti-cmp'),
                'param_name' => 'lang',
                'value' => $this->get_lang_options(),
                'std' => 'auto',
            ],
        ];

        // 6. Impressum
        vc_map([
            'name' => esc_html__('Biscotti Impressum', 'biscotti-cmp'),
            'base' => 'biscotti_impressum',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-media-document',
            'description' => esc_html__('Legal notice from Biscotti Legal Suite', 'biscotti-cmp'),
            'params' => $legal_params,
        ]);

        // 7. Privacy Policy
        vc_map([
            'name' => esc_html__('Biscotti Privacy Policy', 'biscotti-cmp'),
            'base' => 'biscotti_privacy_policy',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-lock',
            'description' => esc_html__('Privacy policy from Biscotti Legal Suite', 'biscotti-cmp'),
            'params' => $legal_params,
        ]);

        // 8. Cookie Policy
        vc_map([
            'name' => esc_html__('Biscotti Cookie Policy', 'biscotti-cmp'),
            'base' => 'biscotti_cookie_policy',
            'category' => 'Biscotti CMP',
            'icon' => 'dashicons dashicons-info',
            'description' => esc_html__('Cookie policy from Biscotti Legal Suite', 'biscotti-cmp'),
            'params' => $legal_params,
        ]);
    }
}

// Initialize
Biscotti_WPBakery_Elements::get_instance();
