<?php
defined("ABSPATH") || exit;
/**
 * Currency Converter Widget Settings
 * Used by: WidgetRenderCtrl (render), WidgetEmbedCtrl (doc page)
 */
return [
    'slug'      => 'currency-converter',
    'component' => 'vunelix-currency-converter',
    'js'        => 'widgets/vunelix-currency-converter.js',

    // View to render for Shadow DOM
    'view' => 'Fx_live/Views/components/converter',

    // SVG icons needed
    'icons' => ['swap'],

    // Lang key path in common.php — loads $lang['converter'] for view
    'lang_key' => 'currency_converter',

    // Branding position — developer sets per widget (NOT user-configurable)
    'branding_position' => 'top-left',
    'branding_offset_x' => 30,
    'branding_offset_y' => 0,

    // Default values (merged with user data-* attrs)
    'defaults' => [
        'from'           => 'USD',
        'to'             => 'EUR',
        'show_chart'     => true,
        'show_rate_info' => true,
        'show_swap_btn'  => true,
    ],

    // Which config keys to send back to JS (src_key => response.dot.path)
    'config_map' => [
        'from' => 'CONVERTER_INITIAL.from',
        'to'   => 'CONVERTER_INITIAL.to',
    ],

    // View variable aliases (config key => PHP variable name for the view)
    'view_aliases' => [
        'from' => 'from_currency',
        'to'   => 'to_currency',
    ],

    // Widget-specific view data builder (logo paths, computed values)
    'buildViewData' => function(array $viewData, array $config): array {
        // Logo path: "USD" → "currency/US" (first 2 chars of currency code)
        $from = strtoupper($config['from'] ?? 'USD');
        $to   = strtoupper($config['to'] ?? 'EUR');
        $viewData['from_logo'] = 'country/' . substr($from, 0, 2);
        $viewData['to_logo']   = 'country/' . substr($to, 0, 2);
        return $viewData;
    },

    // Settings groups (for WidgetEmbedCtrl customize UI)
    'settings' => [
        'data' => [
            'label'  => 'Data',
            'pkg'    => 1,
            'fields' => [
                'from' => [
                    'type'        => 'ajax-select',
                    'label'       => 'From Currency',
                    'description' => 'The starting currency to convert from (e.g. USD). Visitors can change it.',
                    'default'     => 'USD',
                    'key'         => 'from',
                    'pkg'         => 1,
                    'multiple'    => false,
                    'searchable'  => true,
                    'placeholder' => 'Search currency...',
                    'api'         => 'forex/list',
                    'api_params'  => [
                        'type'    => 'forex',
                        'exchange'=> 'FX',
                        'merge'   => 'profile,meta',
                        'columns' => 'profile.currency,profile.base_logo,profile.curr_logo,meta.base_currency,meta.base_currency_desc,meta.currency_desc',
                    ],
                    'value_key'   => 'meta.base_currency',
                    'label_key'   => 'meta.base_currency_desc',
                    'icon_key'    => 'profile.base_logo',
                ],
                'to' => [
                    'type'        => 'ajax-select',
                    'label'       => 'To Currency',
                    'description' => 'The target currency to convert into (e.g. EUR). Visitors can change it.',
                    'default'     => 'EUR',
                    'key'         => 'to',
                    'pkg'         => 1,
                    'multiple'    => false,
                    'searchable'  => true,
                    'placeholder' => 'Search currency...',
                    'api'         => 'forex/list',
                    'api_params'  => [
                        'type'    => 'forex',
                        'exchange'=> 'FX',
                        'merge'   => 'profile,meta',
                        'columns' => 'profile.currency,profile.base_logo,profile.curr_logo,meta.base_currency,meta.base_currency_desc,meta.currency_desc',
                    ],
                    'value_key'   => 'meta.base_currency',
                    'label_key'   => 'meta.base_currency_desc',
                    'icon_key'    => 'profile.base_logo',
                ],
            ],
        ],
        'display' => [
            'label'  => 'Display',
            'pkg'    => 1,
            'fields' => [
                'show_chart' => [
                    'type'        => 'toggle',
                    'label'       => 'Show Chart',
                    'description' => 'Display a historical exchange rate chart below the converter.',
                    'default'     => true,
                    'key'         => 'show_chart',
                    'pkg'         => 1,
                ],
                'show_rate_info' => [
                    'type'        => 'toggle',
                    'label'       => 'Show Rate Info',
                    'description' => 'Display the current exchange rate and last update time.',
                    'default'     => true,
                    'key'         => 'show_rate_info',
                    'pkg'         => 1,
                ],
                'show_swap_btn' => [
                    'type'        => 'toggle',
                    'label'       => 'Show Swap Button',
                    'description' => 'Show a button to quickly swap the From and To currencies.',
                    'default'     => true,
                    'key'         => 'show_swap_btn',
                    'pkg'         => 1,
                ],
            ],
        ],
    ],
];
