<?php
defined("ABSPATH") || exit;
/**
 * Symbol Search Widget Settings
 * User provides their own trigger element — modal opens on click
 *
 * Usage:
 *   <button id="mySearchBtn">Search</button>
 *   <vunelix-symbol-search data-trigger="#mySearchBtn"></vunelix-symbol-search>
 */
$vunelix_shared = require __DIR__ . '/_shared_fields.php';

return [
    'slug'      => 'symbol-search',
    'component' => 'vunelix-symbol-search',
    'js'        => 'widgets/vunelix-symbol-search.js',

    // Demo trigger button — user will use their own via data-trigger
    'view' => 'Widgets/Views/widgets/symbol_search',

    // SVG icons needed by MenuSearch
    'icons' => ['global', 'primary', 'magnifier-char'],

    'lang_key' => null,

    // Default values
    'defaults' => [
        'trigger'             => '',
        'placeholder'         => '',
        'filter_type'         => '',
        'default_filters'     => '',
        'default_exchange'    => '',
        'default_country'     => '',
        'default_sub_type'    => '',
        'default_sector'      => '',
        'default_type'        => '',
        'show_filters'        => true,
        'enable_return_data'  => false,
        'enable_overview_link'=> true,
        'overview_url'        => '',
        'enable_chart_link'   => false,
        'chart_url'           => '',
        'link_target'         => '_blank',
        'enable_storage'      => false,
    ],

    // Config to send to JS
    'config_map' => [
        'trigger'              => 'SEARCH.trigger',
        'placeholder'          => 'SEARCH.placeholder',
        'filter_type'          => 'SEARCH.filter_type',
        'default_filters'      => 'SEARCH.default_filters',
        'default_exchange'     => 'SEARCH.default_exchange',
        'default_country'      => 'SEARCH.default_country',
        'default_sub_type'     => 'SEARCH.default_sub_type',
        'default_sector'       => 'SEARCH.default_sector',
        'default_type'         => 'SEARCH.default_type',
        'show_filters'         => 'SEARCH.show_filters',
        'enable_return_data'   => 'SEARCH.enable_return_data',
        'enable_overview_link' => 'SEARCH.enable_overview_link',
        'overview_url'         => 'SEARCH.overview_url',
        'enable_chart_link'    => 'SEARCH.enable_chart_link',
        'chart_url'            => 'SEARCH.chart_url',
        'link_target'          => 'SEARCH.link_target',
        'enable_storage'       => 'enableStorage',
    ],

    // Settings UI
    'settings' => [
        'data' => [
            'label'  => 'Data',
            'pkg'    => 1,
            'fields' => [
                'trigger' => [
                    'type'        => 'text',
                    'label'       => 'Trigger Element (Required)',
                    'description' => 'Enter the ID or class of your button or link that opens the search popup (e.g. #mySearchBtn or .search-trigger). When set, the demo search bar below will be hidden and your element will open the popup instead.',
                    'default'     => '',
                    'key'         => 'trigger',
                    'pkg'         => 1,
                ],
                'placeholder' => [
                    'type'        => 'text',
                    'label'       => 'Placeholder Text',
                    'description' => 'Custom placeholder text shown inside the search input when the popup opens. Leave empty for the default.',
                    'default'     => '',
                    'key'         => 'placeholder',
                    'pkg'         => 1,
                ],
                'show_filters' => [
                    'type'        => 'toggle',
                    'label'       => 'Show Filter Tabs',
                    'description' => 'When off, the top filter tabs and all inner filters will be hidden.',
                    'default'     => true,
                    'key'         => 'show_filters',
                    'pkg'         => 1,
                ],
                'filter_type' => [
                    'type'        => 'select',
                    'label'       => 'Asset Filter',
                    'description' => 'Only the selected type will be shown. Other tabs will be hidden. Inner filters of the selected type remain visible.',
                    'default'     => '',
                    'key'         => 'filter_type',
                    'pkg'         => 1,
                    'options'     => [
                        ''       => 'All Assets',
                        'stock'  => 'Stocks',
                        'crypto' => 'Crypto',
                        'forex'  => 'Forex',
                        'fund'   => 'Funds',
                        'dex'    => 'DEX',
                        'futures'=> 'Futures',
                        'indices'=> 'Indices',
                    ],
                ],
                'default_filters' => [
                    'type'        => 'market-data-filters',
                    'full_width'  => true,
                    'label'       => 'Pre-selected Filters',
                    'description' => 'Filters applied when the search popup opens. Options change based on the Asset Filter above.',
                    'default'     => '',
                    'key'         => 'default_filters',
                    'pkg'         => 1,
                    'depends_on'  => 'filter-type',
                ],
            ],
        ],
        'storage'   => $vunelix_shared['groups']['storage'],
        'links' => [
            'label'  => 'Click Behavior',
            'pkg'    => 1,
            'fields' => [
                'enable_return_data' => [
                    'type'        => 'toggle',
                    'label'       => 'Developer Mode',
                    'description' => 'For developers only. When enabled, clicking a symbol returns its data to your page code instead of opening a link. Useful for building custom tools.',
                    'default'     => false,
                    'key'         => 'enable_return_data',
                    'pkg'         => 1,
                ],
                'return_data_example' => [
                    'type'         => 'code-example',
                    'full_width'   => true,
                    'label'        => 'JavaScript Example',
                    'visible_when' => 'enable-return-data',
                    'code'         => '// Listen for symbol selection
document.addEventListener("vunelix-symbol-select", function(e) {
  var data = e.detail;
  console.log(data.ticker);   // "BINANCE:BTCUSDT"
  console.log(data.symbol);   // "BTCUSDT"
  console.log(data.name);     // "Bitcoin / TetherUS"
  console.log(data.exchange);  // "BINANCE"
  console.log(data.type);     // "crypto"
  console.log(data.logo);     // "https://vunelix.com/assets/icons/crypto/BTC.svg"
});',
                    'output_label' => 'Live Output — select a symbol in the preview above to see the returned data here',
                ],
                'link_target' => array_merge($vunelix_shared['fields']['link_target'], [
                    'description' => 'Applies to Overview URL only. Chart URL always opens in a new tab.',
                    'hidden_when' => 'enable-return-data',
                ]),
                'enable_overview_link' => array_merge($vunelix_shared['fields']['enable_links'], [
                    'label'       => 'Overview Link',
                    'description' => 'Show an overview button next to each search result that opens your custom URL.',
                    'key'         => 'enable_overview_link',
                    'hidden_when' => 'enable-return-data',
                ]),
                'enable_chart_link' => array_merge($vunelix_shared['fields']['enable_links'], [
                    'label'       => 'Chart Link',
                    'description' => 'Show a chart button next to each search result.',
                    'key'         => 'enable_chart_link',
                    'default'     => false,
                    'hidden_when' => 'enable-return-data',
                ]),
                'overview_url' => array_merge($vunelix_shared['fields']['link_url'], [
                    'label'       => 'Overview URL',
                    'description' => 'Where the overview button takes visitors. Use placeholders for dynamic values. E.g. yoursite.com/symbol/{{symbol}} → yoursite.com/symbol/AAPL. Leave empty for Vunelix detail page.',
                    'key'         => 'overview_url',
                    'placeholder' => 'https://yoursite.com/symbol/{{symbol}}',
                    'tokens'      => ['symbol', 'name', 'exchange', 'ticker', 'type'],
                    'hidden_when' => 'enable-return-data',
                ]),
                'chart_url' => array_merge($vunelix_shared['fields']['link_url'], [
                    'label'       => 'Chart URL',
                    'description' => 'Where the chart button takes visitors. E.g. yoursite.com/chart/{{ticker}} → yoursite.com/chart/BINANCE:BTCUSD. Leave empty for Vunelix chart page.',
                    'key'         => 'chart_url',
                    'placeholder' => 'https://yoursite.com/chart/{{ticker}}',
                    'tokens'      => ['symbol', 'name', 'exchange', 'ticker', 'type'],
                    'hidden_when' => 'enable-return-data',
                ]),
            ],
        ],
        // Override common appearance — exclude positive/negative colors (not used in search)
        'appearance' => (function() {
            $common = require __DIR__ . '/_common_fields.php';
            $appearance = $common['settings']['appearance'];
            unset($appearance['fields']['positive_color'], $appearance['fields']['negative_color']);
            return $appearance;
        })(),
    ],
];
