<?php

declare(strict_types=1);

namespace CliqThemes\StoreLocator\Integrations;

defined('ABSPATH') || exit;

use CliqThemes\StoreLocator\Layouts\LocatorLayouts;


/**
 * Elementor "Store Locator" widget — picks a saved locator template and renders
 * it via the [cliq_store_locator] shortcode. Defined in its own file so the
 * \Elementor\Widget_Base parent is only loaded when Elementor is active (the
 * class is referenced solely from Elementor::registerWidget()).
 */
final class ElementorLocatorWidget extends \Elementor\Widget_Base
{
    public function get_name(): string
    {
        return 'cliq-store-locator';
    }

    public function get_title(): string
    {
        return __('Store Locator', 'cliq-store-locator');
    }

    public function get_icon(): string
    {
        return 'eicon-google-maps';
    }

    /** @return string[] */
    public function get_categories(): array
    {
        return [Elementor::CATEGORY];
    }

    /** @return string[] */
    public function get_keywords(): array
    {
        return ['store', 'locator', 'map', 'cliq', 'stores'];
    }

    protected function register_controls(): void
    {
        $this->start_controls_section('cliq_sl_section', [
            'label' => __('Store Locator', 'cliq-store-locator'),
        ]);

        $this->add_control('template_id', [
            'label' => __('Template', 'cliq-store-locator'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'options' => LocatorTemplates::options(),
            'default' => '',
            'description' => __('Choose the locator layout to display.', 'cliq-store-locator'),
        ]);

        $this->add_control('category', [
            'label' => __('Category ID', 'cliq-store-locator'),
            'type' => \Elementor\Controls_Manager::TEXT,
            'description' => __('Optional category ID used as an initial filter.', 'cliq-store-locator'),
        ]);

        $this->add_control('brand', [
            'label' => __('Brand ID', 'cliq-store-locator'),
            'type' => \Elementor\Controls_Manager::TEXT,
            'description' => __('Optional brand ID used as an initial filter.', 'cliq-store-locator'),
        ]);

        $this->add_control('meta_key', [
            'label' => __('Custom field key', 'cliq-store-locator'),
            'type' => \Elementor\Controls_Manager::TEXT,
            'description' => __('The custom field machine key, for example services.', 'cliq-store-locator'),
        ]);

        $this->add_control('meta_value', [
            'label' => __('Custom field value', 'cliq-store-locator'),
            'type' => \Elementor\Controls_Manager::TEXT,
            'description' => __('Only stores whose custom field exactly matches this value are shown.', 'cliq-store-locator'),
        ]);

        $this->end_controls_section();
    }

    protected function render(): void
    {
        $settings = $this->get_settings_for_display();
        // A widget that names no layout renders the site's default one, exactly
        // as a placement with no id does. Zero means the site has not got one.
        $id = (int) ($settings['template_id'] ?? 0);
        if ($id === 0) {
            $id = LocatorLayouts::source()->defaultId();
        }
        if ($id === 0) {
            echo '<em>' . esc_html__('Select a Store Locator template in the widget settings.', 'cliq-store-locator') . '</em>';

            return;
        }

        $shortcode = sprintf(
            '[cliq_store_locator id="%d" category="%s" brand="%s" meta_key="%s" meta_value="%s"]',
            $id,
            esc_attr((string) ($settings['category'] ?? '')),
            esc_attr((string) ($settings['brand'] ?? '')),
            esc_attr(sanitize_key((string) ($settings['meta_key'] ?? ''))),
            esc_attr((string) ($settings['meta_value'] ?? ''))
        );
        echo do_shortcode($shortcode); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    }
}
