<?php

declare(strict_types=1);

namespace CliqThemes\StoreLocator\Integrations;

defined('ABSPATH') || exit;

/**
 * WPBakery Page Builder (Visual Composer) integration. Maps our existing
 * [cliq_store_locator] shortcode as a VC element with a template picker — no
 * extra render path, VC simply emits the shortcode we already handle.
 */
final class WPBakery implements Integration
{
    public function isActive(): bool
    {
        return defined('WPB_VC_VERSION');
    }

    public function register(): void
    {
        // We boot on init:10, but WPBakery fires vc_before_init on init:9 — so by
        // the time we register, that hook may already have run. Map immediately
        // if so; otherwise queue for when it fires.
        if (did_action('vc_before_init')) {
            $this->mapShortcode();
        } else {
            add_action('vc_before_init', [$this, 'mapShortcode']);
        }
    }

    public function mapShortcode(): void
    {
        if (! function_exists('vc_map')) {
            return;
        }

        // vc_map dropdowns are keyed label => value.
        $values = [];
        foreach (LocatorTemplates::options(false) as $id => $label) {
            $values[$label] = (string) $id;
        }

        vc_map([
            'name' => __('Store Locator', 'cliq-store-locator'),
            'base' => 'cliq_store_locator',
            'category' => __('Content', 'cliq-store-locator'),
            'icon' => 'icon-wpb-map-pin',
            'description' => __('Embed a saved store locator template.', 'cliq-store-locator'),
            'params' => [
                [
                    'type' => 'dropdown',
                    'heading' => __('Template', 'cliq-store-locator'),
                    'param_name' => 'id',
                    'value' => $values,
                    'description' => __('Choose the locator layout to display.', 'cliq-store-locator'),
                ],
                [
                    'type' => 'textfield',
                    'heading' => __('Category ID', 'cliq-store-locator'),
                    'param_name' => 'category',
                    'description' => __('Optional category ID used as an initial filter.', 'cliq-store-locator'),
                ],
                [
                    'type' => 'textfield',
                    'heading' => __('Brand ID', 'cliq-store-locator'),
                    'param_name' => 'brand',
                    'description' => __('Optional brand ID used as an initial filter.', 'cliq-store-locator'),
                ],
                [
                    'type' => 'textfield',
                    'heading' => __('Custom field key', 'cliq-store-locator'),
                    'param_name' => 'meta_key',
                    'description' => __('The custom field machine key, for example services.', 'cliq-store-locator'),
                ],
                [
                    'type' => 'textfield',
                    'heading' => __('Custom field value', 'cliq-store-locator'),
                    'param_name' => 'meta_value',
                    'description' => __('Only stores whose custom field exactly matches this value are shown.', 'cliq-store-locator'),
                ],
            ],
        ]);

        /**
         * Map shortcodes for a feature of your own into the page builder.
         */
        do_action('cliq_sl_register_wpbakery_elements');
    }
}
