<?php

namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use WC_Product;

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

/**
 * Renders a Swiper-powered WooCommerce product slider for Elementor.
 */
class WooCommerceSliderWidget extends BaseWidget
{
    public function get_name(): string
    {
        return 'calio-woocommerce-slider';
    }

    public function get_title(): string
    {
        return esc_html__('WooCommerce Slider', 'calio-addon');
    }

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

    public function get_keywords(): array
    {
        return ['calio', 'woocommerce', 'product', 'shop', 'slider', 'carousel'];
    }

    public function get_style_depends(): array
    {
        $depends = $this->resolve_widget_style_depends('calio-widget-woocommerce-slider');
        $depends[] = 'swiper';

        return array_values(array_unique($depends));
    }

    public function get_script_depends(): array
    {
        return $this->resolve_widget_script_depends('calio-widget-woocommerce-slider');
    }

    protected function register_controls(): void
    {
        $this->register_query_controls();
        $this->register_slider_controls();
        $this->register_navigation_controls();
        $this->register_additional_controls();
        $this->register_button_controls();
        $this->register_card_style_controls();
        $this->register_image_style_controls();
        $this->register_content_style_controls();
        $this->register_badge_style_controls();
        $this->register_button_style_controls();
        $this->register_navigation_style_controls();
    }

    private function register_query_controls(): void
    {
        $this->start_controls_section('query_section', [
            'label' => esc_html__('Query', 'calio-addon'),
        ]);

        $this->add_control('product_source', [
            'label' => esc_html__('Product Source', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'latest',
            'options' => [
                'latest' => esc_html__('Latest Products', 'calio-addon'),
                'featured' => esc_html__('Featured Products', 'calio-addon'),
                'sale' => esc_html__('Sale Products', 'calio-addon'),
                'best_selling' => esc_html__('Best Selling', 'calio-addon'),
                'top_rated' => esc_html__('Top Rated', 'calio-addon'),
                'manual' => esc_html__('Manual Product IDs', 'calio-addon'),
            ],
        ]);

        $this->add_control('manual_product_ids', [
            'label' => esc_html__('Product IDs', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'description' => esc_html__('Comma separated product IDs.', 'calio-addon'),
            'placeholder' => '12, 24, 36',
            'condition' => ['product_source' => 'manual'],
        ]);

        $this->add_control('product_categories', [
            'label' => esc_html__('Categories', 'calio-addon'),
            'type' => Controls_Manager::SELECT2,
            'multiple' => true,
            'label_block' => true,
            'options' => $this->get_product_category_options(),
            'condition' => ['product_source!' => 'manual'],
        ]);

        $this->add_control('products_per_page', [
            'label' => esc_html__('Products Per Page', 'calio-addon'),
            'type' => Controls_Manager::NUMBER,
            'default' => 8,
            'min' => 1,
            'max' => 24,
        ]);

        $this->add_control('orderby', [
            'label' => esc_html__('Order By', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'date',
            'options' => [
                'date' => esc_html__('Date', 'calio-addon'),
                'title' => esc_html__('Title', 'calio-addon'),
                'price' => esc_html__('Price', 'calio-addon'),
                'popularity' => esc_html__('Popularity', 'calio-addon'),
                'rating' => esc_html__('Rating', 'calio-addon'),
                'rand' => esc_html__('Random', 'calio-addon'),
            ],
            'condition' => ['product_source' => ['latest', 'featured', 'sale']],
        ]);

        $this->add_control('order', [
            'label' => esc_html__('Order', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'DESC',
            'options' => [
                'DESC' => 'DESC',
                'ASC' => 'ASC',
            ],
            'condition' => ['product_source!' => 'manual'],
        ]);

        $this->add_control('hide_out_of_stock', [
            'label' => esc_html__('Hide Out Of Stock', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->end_controls_section();
    }

    private function register_slider_controls(): void
    {
        $this->start_controls_section('slider_section', [
            'label' => esc_html__('Sliders', 'calio-addon'),
        ]);

        $this->add_control('skin', [
            'label' => esc_html__('Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'modern',
            'options' => [
                'modern' => esc_html__('Modern Card', 'calio-addon'),
                'showcase' => esc_html__('Showcase Hero', 'calio-addon'),
                'overlay' => esc_html__('Image Overlay', 'calio-addon'),
                'minimal' => esc_html__('Minimal Shop', 'calio-addon'),
            ],
        ]);

        $this->add_control('slides_desktop', [
            'label' => esc_html__('Slides Desktop', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => '4',
            'options' => ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'],
        ]);

        $this->add_control('slides_tablet', [
            'label' => esc_html__('Slides Tablet', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => '2',
            'options' => ['1' => '1', '2' => '2', '3' => '3'],
        ]);

        $this->add_control('slides_mobile', [
            'label' => esc_html__('Slides Mobile', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => '1',
            'options' => ['1' => '1', '2' => '2'],
        ]);

        $this->add_control('slides_to_scroll', [
            'label' => esc_html__('Slides To Scroll', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => '1',
            'options' => ['1' => '1', '2' => '2', '3' => '3', '4' => '4'],
        ]);

        $this->add_control('space_between', [
            'label' => esc_html__('Space Between', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 0, 'max' => 48]],
            'default' => ['size' => 24],
        ]);

        $this->add_control('image_ratio', [
            'label' => esc_html__('Image Ratio', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'square',
            'options' => [
                'square' => esc_html__('Square', 'calio-addon'),
                'landscape' => esc_html__('Landscape', 'calio-addon'),
                'portrait' => esc_html__('Portrait', 'calio-addon'),
                'auto' => esc_html__('Auto', 'calio-addon'),
            ],
        ]);

        $this->add_control('show_category', [
            'label' => esc_html__('Show Category', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_sale_badge', [
            'label' => esc_html__('Show Sale Badge', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_rating', [
            'label' => esc_html__('Show Rating', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_price', [
            'label' => esc_html__('Show Price', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('show_excerpt', [
            'label' => esc_html__('Show Short Description', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('excerpt_length', [
            'label' => esc_html__('Description Length', 'calio-addon'),
            'type' => Controls_Manager::NUMBER,
            'default' => 14,
            'min' => 5,
            'max' => 40,
            'condition' => ['show_excerpt' => 'yes'],
        ]);

        $this->add_control('loop', [
            'label' => esc_html__('Loop', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('autoplay', [
            'label' => esc_html__('Autoplay', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('autoplay_speed', [
            'label' => esc_html__('Autoplay Speed', 'calio-addon'),
            'type' => Controls_Manager::NUMBER,
            'default' => 3500,
            'min' => 1000,
            'max' => 12000,
            'step' => 100,
            'condition' => ['autoplay' => 'yes'],
        ]);

        $this->add_control('transition_speed', [
            'label' => esc_html__('Transition Speed', 'calio-addon'),
            'type' => Controls_Manager::NUMBER,
            'default' => 600,
            'min' => 100,
            'max' => 3000,
            'step' => 50,
        ]);

        $this->add_control('pause_on_hover', [
            'label' => esc_html__('Pause On Hover', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
            'condition' => ['autoplay' => 'yes'],
        ]);

        $this->add_control('pause_on_interaction', [
            'label' => esc_html__('Pause After Interaction', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
            'condition' => ['autoplay' => 'yes'],
        ]);

        $this->add_control('grab_cursor', [
            'label' => esc_html__('Grab Cursor', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('centered_slides', [
            'label' => esc_html__('Centered Slides', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('highlight_active_slide', [
            'label' => esc_html__('Highlight Active Slide', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->end_controls_section();
    }

    private function register_navigation_controls(): void
    {
        $this->start_controls_section('navigation_section', [
            'label' => esc_html__('Navigation', 'calio-addon'),
        ]);

        $this->add_control('navigation_type', [
            'label' => esc_html__('Navigation', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'arrows',
            'options' => [
                'none' => esc_html__('None', 'calio-addon'),
                'arrows' => esc_html__('Arrows', 'calio-addon'),
                'dots' => esc_html__('Dots', 'calio-addon'),
                'arrows-dots' => esc_html__('Arrows and Dots', 'calio-addon'),
            ],
        ]);

        $this->add_control('show_scrollbar', [
            'label' => esc_html__('Show Scrollbar?', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('arrows_position', [
            'label' => esc_html__('Arrows Position', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'center',
            'options' => [
                'top' => esc_html__('Top', 'calio-addon'),
                'center' => esc_html__('Center', 'calio-addon'),
                'bottom' => esc_html__('Bottom', 'calio-addon'),
                'bottom-right' => esc_html__('Bottom Right', 'calio-addon'),
            ],
            'condition' => ['navigation_type' => ['arrows', 'arrows-dots']],
        ]);

        $this->add_control('prev_icon', [
            'label' => esc_html__('Previous Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-chevron-left',
                'library' => 'fa-solid',
            ],
            'condition' => ['navigation_type' => ['arrows', 'arrows-dots']],
        ]);

        $this->add_control('next_icon', [
            'label' => esc_html__('Next Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-chevron-right',
                'library' => 'fa-solid',
            ],
            'condition' => ['navigation_type' => ['arrows', 'arrows-dots']],
        ]);

        $this->add_control('hide_arrows_mobile', [
            'label' => esc_html__('Hide Arrow on Mobile ?', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
            'condition' => ['navigation_type' => ['arrows', 'arrows-dots']],
        ]);

        $this->end_controls_section();
    }

    private function register_additional_controls(): void
    {
        $this->start_controls_section('additional_settings_section', [
            'label' => esc_html__('Additional Settings', 'calio-addon'),
        ]);

        $this->add_responsive_control('height', [
            'label' => esc_html__('Height', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px', 'vh'],
            'default' => [
                'unit' => 'px',
                'size' => 520,
            ],
            'range' => [
                'px' => [
                    'min' => 120,
                    'max' => 1024,
                ],
                'vh' => [
                    'min' => 20,
                    'max' => 100,
                ],
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider' => '--calio-woo-slider-height: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-woo-slider-swiper' => 'height: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-woo-slider .swiper-wrapper' => 'height: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-woo-slider .swiper-slide' => 'height: {{SIZE}}{{UNIT}}; min-height: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-woo-slider-card' => 'height: {{SIZE}}{{UNIT}}; min-height: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('content_max_width', [
            'label' => esc_html__('Content Width', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => [
                'px' => [
                    'min' => 160,
                    'max' => 1200,
                ],
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-content' => 'width: {{SIZE}}{{UNIT}}; max-width: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_control('content_position', [
            'label' => esc_html__('Content Position', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'center',
            'options' => [
                'top-left' => esc_html__('Top Start', 'calio-addon'),
                'top-center' => esc_html__('Top Center', 'calio-addon'),
                'top-right' => esc_html__('Top End', 'calio-addon'),
                'center' => esc_html__('Center', 'calio-addon'),
                'center-left' => esc_html__('Center Start', 'calio-addon'),
                'center-right' => esc_html__('Center End', 'calio-addon'),
                'bottom-left' => esc_html__('Bottom Start', 'calio-addon'),
                'bottom-center' => esc_html__('Bottom Center', 'calio-addon'),
                'bottom-right' => esc_html__('Bottom End', 'calio-addon'),
            ],
        ]);

        $this->add_responsive_control('align', [
            'label' => esc_html__('Alignment', 'calio-addon'),
            'type' => Controls_Manager::CHOOSE,
            'default' => 'center',
            'toggle' => false,
            'options' => [
                'left' => [
                    'title' => esc_html__('Left', 'calio-addon'),
                    'icon' => 'eicon-text-align-left',
                ],
                'center' => [
                    'title' => esc_html__('Center', 'calio-addon'),
                    'icon' => 'eicon-text-align-center',
                ],
                'right' => [
                    'title' => esc_html__('Right', 'calio-addon'),
                    'icon' => 'eicon-text-align-right',
                ],
                'justify' => [
                    'title' => esc_html__('Justified', 'calio-addon'),
                    'icon' => 'eicon-text-align-justify',
                ],
            ],
            'description' => esc_html__('Use align to match position', 'calio-addon'),
            'selectors_dictionary' => [
                'left' => 'align-items: flex-start; align-self: flex-start; justify-content: flex-start; text-align: left;',
                'center' => 'align-items: center; align-self: center; justify-content: center; text-align: center;',
                'right' => 'align-items: flex-end; align-self: flex-end; justify-content: flex-end; text-align: right;',
                'justify' => 'align-items: stretch; align-self: stretch; justify-content: space-between; text-align: justify;',
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-content, {{WRAPPER}} .calio-woo-slider-category, {{WRAPPER}} .calio-woo-slider-rating, {{WRAPPER}} .calio-woo-slider-price, {{WRAPPER}} .calio-woo-slider-description, {{WRAPPER}} .calio-woo-slider-actions' => '{{VALUE}}',
            ],
        ]);

        $this->add_control('show_title', [
            'label' => esc_html__('Show Title', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('title_html_tag', [
            'label' => esc_html__('Title HTML Tag', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'h2',
            'options' => $this->get_title_tag_options(),
            'condition' => ['show_title' => 'yes'],
        ]);

        $this->add_control('show_button', [
            'label' => esc_html__('Show Button', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('scroll_to_section', [
            'label' => esc_html__('Scroll to Section', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('section_id', [
            'label' => esc_html__('Section ID', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'dynamic' => ['active' => true],
            'placeholder' => '#shop-section',
            'description' => esc_html__('Enter section ID of this page, for example: #shop-section', 'calio-addon'),
            'label_block' => true,
            'condition' => ['scroll_to_section' => 'yes'],
        ]);

        $this->add_control('scroll_to_section_icon', [
            'label' => esc_html__('Scroll to Section Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-angle-double-down',
                'library' => 'fa-solid',
            ],
            'condition' => ['scroll_to_section' => 'yes'],
        ]);

        $this->end_controls_section();
    }

    private function register_button_controls(): void
    {
        $this->start_controls_section('button_section', [
            'label' => esc_html__('Button', 'calio-addon'),
        ]);

        $this->add_control('button_text', [
            'label' => esc_html__('Button Text', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'dynamic' => ['active' => true],
            'default' => '',
            'placeholder' => esc_html__('Add to cart', 'calio-addon'),
            'description' => esc_html__('Leave empty to use each product button text.', 'calio-addon'),
            'condition' => ['show_button' => 'yes'],
        ]);

        $this->add_control('button_icon', [
            'label' => esc_html__('Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'label_block' => false,
            'skin' => 'inline',
            'condition' => ['show_button' => 'yes'],
        ]);

        $this->add_control('icon_align', [
            'label' => esc_html__('Icon Position', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'right',
            'options' => [
                'left' => esc_html__('Left', 'calio-addon'),
                'right' => esc_html__('Right', 'calio-addon'),
            ],
            'condition' => ['button_icon[value]!' => ''],
        ]);

        $this->add_control('icon_indent', [
            'label' => esc_html__('Icon Spacing', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'default' => ['size' => 8],
            'range' => [
                'px' => [
                    'max' => 50,
                ],
            ],
            'condition' => ['button_icon[value]!' => ''],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-button .calio-button-icon-align-right' => is_rtl() ? 'margin-inline-end: {{SIZE}}{{UNIT}};' : 'margin-inline-start: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-woo-slider-button .calio-button-icon-align-left' => is_rtl() ? 'margin-inline-start: {{SIZE}}{{UNIT}};' : 'margin-inline-end: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_control('show_read_more', [
            'label' => esc_html__('Show Product Link', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);

        $this->add_control('read_more_text', [
            'label' => esc_html__('Product Link Text', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('View Product', 'calio-addon'),
            'condition' => ['show_read_more' => 'yes'],
        ]);

        $this->add_control('read_more_display', [
            'label' => esc_html__('Product Link Display', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'icon-top-right',
            'options' => [
                'button' => esc_html__('Button', 'calio-addon'),
                'icon-top-right' => esc_html__('Icon Top Right', 'calio-addon'),
                'icon-overlay' => esc_html__('Icon Overlay', 'calio-addon'),
            ],
            'condition' => ['show_read_more' => 'yes'],
        ]);

        $this->add_control('read_more_icon', [
            'label' => esc_html__('Product Link Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-eye',
                'library' => 'fa-solid',
            ],
            'condition' => [
                'show_read_more' => 'yes',
                'read_more_display!' => 'button',
            ],
        ]);

        $this->end_controls_section();
    }

    private function register_card_style_controls(): void
    {
        $this->start_controls_section('style_card_section', [
            'label' => esc_html__('Card', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_dimensions_control('card_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-card', 'padding');
        $this->add_dimensions_control('card_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-card', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_background_type_control('card_background', '{{WRAPPER}} .calio-woo-slider-card');
        $this->add_background_type_control('card_background_hover', '{{WRAPPER}} .calio-woo-slider-card:hover');
        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'card_border',
            'selector' => '{{WRAPPER}} .calio-woo-slider-card',
        ]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), [
            'name' => 'card_shadow',
            'selector' => '{{WRAPPER}} .calio-woo-slider-card',
        ]);

        $this->end_controls_section();
    }

    private function register_image_style_controls(): void
    {
        $this->start_controls_section('style_image_section', [
            'label' => esc_html__('Image', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('image_min_height', [
            'label' => esc_html__('Image Min Height', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 120, 'max' => 520]],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-media' => 'min-height: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_dimensions_control('image_radius', esc_html__('Radius', 'calio-addon'), ['{{WRAPPER}} .calio-woo-slider-media', '{{WRAPPER}} .calio-woo-slider-media img'], 'border-radius', ['size_units' => ['px', '%']]);

        $this->add_control('image_fit', [
            'label' => esc_html__('Image Fit', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'default',
            'options' => [
                'default' => esc_html__('Default', 'calio-addon'),
                'cover' => esc_html__('Cover', 'calio-addon'),
                'contain' => esc_html__('Contain', 'calio-addon'),
                'fill' => esc_html__('Fill', 'calio-addon'),
                'none' => esc_html__('None', 'calio-addon'),
                'scale-down' => esc_html__('Scale Down', 'calio-addon'),
            ],
            'selectors_dictionary' => [
                'default' => '',
                'cover' => '--calio-woo-slider-image-fit: cover;',
                'contain' => '--calio-woo-slider-image-fit: contain;',
                'fill' => '--calio-woo-slider-image-fit: fill;',
                'none' => '--calio-woo-slider-image-fit: none;',
                'scale-down' => '--calio-woo-slider-image-fit: scale-down;',
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider' => '{{VALUE}}',
            ],
        ]);

        $this->add_control('image_position', [
            'label' => esc_html__('Image Position', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'default',
            'options' => [
                'default' => esc_html__('Default', 'calio-addon'),
                'top left' => esc_html__('Top Left', 'calio-addon'),
                'top center' => esc_html__('Top Center', 'calio-addon'),
                'top right' => esc_html__('Top Right', 'calio-addon'),
                'center left' => esc_html__('Center Left', 'calio-addon'),
                'center center' => esc_html__('Center Center', 'calio-addon'),
                'center right' => esc_html__('Center Right', 'calio-addon'),
                'bottom left' => esc_html__('Bottom Left', 'calio-addon'),
                'bottom center' => esc_html__('Bottom Center', 'calio-addon'),
                'bottom right' => esc_html__('Bottom Right', 'calio-addon'),
            ],
            'selectors_dictionary' => [
                'default' => '',
                'top left' => '--calio-woo-slider-image-position: top left;',
                'top center' => '--calio-woo-slider-image-position: top center;',
                'top right' => '--calio-woo-slider-image-position: top right;',
                'center left' => '--calio-woo-slider-image-position: center left;',
                'center center' => '--calio-woo-slider-image-position: center center;',
                'center right' => '--calio-woo-slider-image-position: center right;',
                'bottom left' => '--calio-woo-slider-image-position: bottom left;',
                'bottom center' => '--calio-woo-slider-image-position: bottom center;',
                'bottom right' => '--calio-woo-slider-image-position: bottom right;',
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider' => '{{VALUE}}',
            ],
        ]);

        $this->end_controls_section();
    }

    private function register_content_style_controls(): void
    {
        $this->start_controls_section('style_content_section', [
            'label' => esc_html__('Content', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'title_typography',
            'selector' => '{{WRAPPER}} .calio-woo-slider-title, {{WRAPPER}} .calio-woo-slider-title a',
        ]);
        $this->add_text_paint_controls('title_color', esc_html__('Title Color', 'calio-addon'), ['{{WRAPPER}} .calio-woo-slider-title', '{{WRAPPER}} .calio-woo-slider-title a']);
        $this->add_text_paint_controls('category_color', esc_html__('Category Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-category');
        $this->add_text_paint_controls('price_color', esc_html__('Price Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-price');
        $this->add_text_paint_controls('description_color', esc_html__('Description Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-description');

        $this->end_controls_section();
    }

    private function register_badge_style_controls(): void
    {
        $this->start_controls_section('style_badge_section', [
            'label' => esc_html__('Badge', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
            'condition' => ['show_sale_badge' => 'yes'],
        ]);

        $this->add_text_paint_controls('badge_text', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-badge');
        $this->add_background_type_control('badge_background', '{{WRAPPER}} .calio-woo-slider-badge');
        $this->add_dimensions_control('badge_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-badge', 'padding');

        $this->end_controls_section();
    }

    private function register_button_style_controls(): void
    {
        $this->start_controls_section('style_button_section', [
            'label' => esc_html__('Buttons', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('buttons_gap', [
            'label' => esc_html__('Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 0, 'max' => 64]],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-actions' => 'gap: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_control('primary_button_heading', [
            'label' => esc_html__('Add to Cart', 'calio-addon'),
            'type' => Controls_Manager::HEADING,
            'separator' => 'before',
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'button_typography',
            'selector' => '{{WRAPPER}} .calio-woo-slider-button.is-primary',
        ]);
        $this->add_dimensions_control('button_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-button.is-primary', 'padding');
        $this->add_dimensions_control('button_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-button.is-primary', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_text_paint_controls('button_text_color', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-button.is-primary');
        $this->add_background_type_control('button_background', '{{WRAPPER}} .calio-woo-slider-button.is-primary');
        $this->add_background_type_control('button_background_hover', '{{WRAPPER}} .calio-woo-slider-button.is-primary:hover, {{WRAPPER}} .calio-woo-slider-button.is-primary:focus-visible');

        $this->add_control('secondary_button_heading', [
            'label' => esc_html__('Read More', 'calio-addon'),
            'type' => Controls_Manager::HEADING,
            'separator' => 'before',
            'condition' => ['show_read_more' => 'yes'],
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'read_more_typography',
            'selector' => '{{WRAPPER}} .calio-woo-slider-button.is-secondary',
            'condition' => ['show_read_more' => 'yes'],
        ]);
        $this->add_dimensions_control('read_more_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-button.is-secondary', 'padding', ['condition' => ['show_read_more' => 'yes']]);
        $this->add_dimensions_control('read_more_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-button.is-secondary', 'border-radius', ['size_units' => ['px', '%'], 'condition' => ['show_read_more' => 'yes']]);
        $this->add_text_paint_controls('read_more_text_color', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-button.is-secondary', ['condition' => ['show_read_more' => 'yes']]);
        $this->add_background_type_control('read_more_background', '{{WRAPPER}} .calio-woo-slider-button.is-secondary', ['condition' => ['show_read_more' => 'yes']]);
        $this->add_background_type_control('read_more_background_hover', '{{WRAPPER}} .calio-woo-slider-button.is-secondary:hover, {{WRAPPER}} .calio-woo-slider-button.is-secondary:focus-visible', ['condition' => ['show_read_more' => 'yes']]);

        $this->add_control('product_link_icon_heading', [
            'label' => esc_html__('Product Link Icon', 'calio-addon'),
            'type' => Controls_Manager::HEADING,
            'separator' => 'before',
            'condition' => ['show_read_more' => 'yes'],
        ]);

        $this->add_control('product_link_icon_size', [
            'label' => esc_html__('Size', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 28, 'max' => 72]],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-product-link-icon' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}; font-size: calc({{SIZE}}{{UNIT}} * .42);',
            ],
            'condition' => ['show_read_more' => 'yes'],
        ]);

        $this->add_dimensions_control('product_link_icon_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-product-link-icon', 'border-radius', ['size_units' => ['px', '%'], 'condition' => ['show_read_more' => 'yes']]);
        $this->add_text_paint_controls('product_link_icon_color', esc_html__('Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-product-link-icon', ['condition' => ['show_read_more' => 'yes']]);
        $this->add_background_type_control('product_link_icon_background', '{{WRAPPER}} .calio-woo-slider-product-link-icon', ['condition' => ['show_read_more' => 'yes']]);
        $this->add_background_type_control('product_link_icon_background_hover', '{{WRAPPER}} .calio-woo-slider-product-link-icon:hover, {{WRAPPER}} .calio-woo-slider-product-link-icon:focus-visible', ['condition' => ['show_read_more' => 'yes']]);

        $this->end_controls_section();
    }

    private function register_navigation_style_controls(): void
    {
        $this->start_controls_section('style_navigation_section', [
            'label' => esc_html__('Navigation', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('nav_size', [
            'label' => esc_html__('Arrow Size', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 28, 'max' => 72]],
            'selectors' => [
                '{{WRAPPER}} .calio-woo-slider-arrow' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
            'condition' => ['navigation_type' => ['arrows', 'arrows-dots']],
        ]);
        $this->add_text_paint_controls('nav_text', esc_html__('Arrow Color', 'calio-addon'), '{{WRAPPER}} .calio-woo-slider-arrow', ['condition' => ['navigation_type' => ['arrows', 'arrows-dots']]]);
        $this->add_background_type_control('nav_background', '{{WRAPPER}} .calio-woo-slider-arrow', ['condition' => ['navigation_type' => ['arrows', 'arrows-dots']]]);
        $this->add_background_type_control('nav_background_hover', '{{WRAPPER}} .calio-woo-slider-arrow:hover, {{WRAPPER}} .calio-woo-slider-arrow:focus-visible', ['condition' => ['navigation_type' => ['arrows', 'arrows-dots']]]);
        $this->add_background_type_control('dot_background', '{{WRAPPER}} .calio-woo-slider-pagination .swiper-pagination-bullet', ['condition' => ['navigation_type' => ['dots', 'arrows-dots']]]);
        $this->add_background_type_control('dot_background_active', '{{WRAPPER}} .calio-woo-slider-pagination .swiper-pagination-bullet-active', ['condition' => ['navigation_type' => ['dots', 'arrows-dots']]]);
        $this->add_background_type_control('scrollbar_background', '{{WRAPPER}} .calio-woo-slider-scrollbar', ['condition' => ['show_scrollbar' => 'yes']]);
        $this->add_background_type_control('scrollbar_drag_background', '{{WRAPPER}} .calio-woo-slider-scrollbar .swiper-scrollbar-drag', ['condition' => ['show_scrollbar' => 'yes']]);

        $this->end_controls_section();
    }

    protected function render(): void
    {
        $settings = $this->get_settings_for_display();

        if (!class_exists('WooCommerce') || !function_exists('wc_get_products')) {
            echo '<div class="calio-woo-slider-empty">' . esc_html__('WooCommerce must be active to use this widget.', 'calio-addon') . '</div>';
            return;
        }

        $products = $this->get_products($settings);
        if (empty($products)) {
            echo '<div class="calio-woo-slider-empty">' . esc_html__('No products found.', 'calio-addon') . '</div>';
            return;
        }

        $slider_id = 'calio-woocommerce-slider-' . $this->get_id();
        $skin = (string) ($settings['skin'] ?? 'modern');
        $is_showcase = $skin === 'showcase';
        $space_between = $this->get_slider_control_size($settings, 'space_between', 24);
        $slider_height_styles = $this->get_responsive_dimension_styles($settings, 'height', '--calio-woo-slider-height', '520px');
        $slides_desktop = $is_showcase ? '1' : (string) ($settings['slides_desktop'] ?? '4');
        $slides_tablet = $is_showcase ? '1' : (string) ($settings['slides_tablet'] ?? '2');
        $slides_mobile = $is_showcase ? '1' : (string) ($settings['slides_mobile'] ?? '1');
        $slides_to_scroll = $is_showcase ? '1' : (string) ($settings['slides_to_scroll'] ?? '1');
        $navigation_type = sanitize_key((string) ($settings['navigation_type'] ?? ''));
        if ($navigation_type === '') {
            $navigation_type = $this->get_legacy_navigation_type($settings);
        }
        $show_arrows = in_array($navigation_type, ['arrows', 'arrows-dots'], true);
        $show_dots = in_array($navigation_type, ['dots', 'arrows-dots'], true);
        $show_scrollbar = ($settings['show_scrollbar'] ?? '') === 'yes';
        $arrows_position = $this->get_valid_arrow_position((string) ($settings['arrows_position'] ?? 'center'));
        $legacy_arrows_icon = $this->get_valid_arrow_icon((string) ($settings['arrows_icon'] ?? 'default'));
        $slider_inline_styles = trim($slider_height_styles . ' ' . $this->get_image_style_variables($settings));
        $classes = [
            'calio-woo-slider',
            'skin-' . sanitize_html_class($skin),
            'ratio-' . sanitize_html_class((string) ($settings['image_ratio'] ?? 'square')),
            'content-position-' . sanitize_html_class((string) ($settings['content_position'] ?? 'center')),
            'calio-woo-slider-align-' . sanitize_html_class((string) ($settings['align'] ?? 'center')),
            'navigation-position-' . sanitize_html_class($arrows_position),
            'navigation-icon-' . sanitize_html_class($legacy_arrows_icon),
        ];

        if (($settings['highlight_active_slide'] ?? '') === 'yes') {
            $classes[] = 'is-highlight-active';
        }

        if (($settings['hide_arrows_mobile'] ?? '') === 'yes') {
            $classes[] = 'hide-arrows-mobile';
        }

        echo '<div id="' . esc_attr($slider_id) . '" class="' . esc_attr(implode(' ', $classes)) . '"'
            . ' style="' . esc_attr($slider_inline_styles) . '"'
            . ' data-space-between="' . esc_attr((string) $space_between) . '"'
            . ' data-slides-desktop="' . esc_attr($slides_desktop) . '"'
            . ' data-slides-tablet="' . esc_attr($slides_tablet) . '"'
            . ' data-slides-mobile="' . esc_attr($slides_mobile) . '"'
            . ' data-slides-scroll="' . esc_attr($slides_to_scroll) . '"'
            . ' data-loop="' . esc_attr((string) ($settings['loop'] ?? '')) . '"'
            . ' data-autoplay="' . esc_attr((string) ($settings['autoplay'] ?? '')) . '"'
            . ' data-autoplay-speed="' . esc_attr((string) ($settings['autoplay_speed'] ?? '3500')) . '"'
            . ' data-transition-speed="' . esc_attr((string) ($settings['transition_speed'] ?? '600')) . '"'
            . ' data-pause-on-hover="' . esc_attr((string) ($settings['pause_on_hover'] ?? 'yes')) . '"'
            . ' data-pause-on-interaction="' . esc_attr((string) ($settings['pause_on_interaction'] ?? 'yes')) . '"'
            . ' data-show-arrows="' . esc_attr($show_arrows ? 'yes' : '') . '"'
            . ' data-show-dots="' . esc_attr($show_dots ? 'yes' : '') . '"'
            . ' data-show-scrollbar="' . esc_attr($show_scrollbar ? 'yes' : '') . '"'
            . ' data-grab-cursor="' . esc_attr((string) ($settings['grab_cursor'] ?? 'yes')) . '"'
            . ' data-centered-slides="' . esc_attr((string) ($settings['centered_slides'] ?? '')) . '"'
            . ' data-highlight-active="' . esc_attr((string) ($settings['highlight_active_slide'] ?? '')) . '">';

        echo '<div class="swiper calio-woo-slider-swiper">';
        echo '<div class="swiper-wrapper">';
        foreach ($products as $product) {
            if ($product instanceof WC_Product) {
                $this->render_product_slide($product, $settings);
            }
        }
        echo '</div>';
        echo '</div>';

        if ($show_arrows || $show_dots || $show_scrollbar) {
            echo '<div class="calio-woo-slider-controls">';
            if ($show_arrows) {
                echo '<div class="calio-woo-slider-nav">';
                echo '<button class="calio-woo-slider-arrow is-prev" type="button" aria-label="' . esc_attr__('Previous products', 'calio-addon') . '">' . wp_kses($this->get_navigation_arrow_icon_markup($settings['prev_icon'] ?? [], 'prev', $legacy_arrows_icon), $this->calio_allowed_icon_markup()) . '</button>';
                echo '<button class="calio-woo-slider-arrow is-next" type="button" aria-label="' . esc_attr__('Next products', 'calio-addon') . '">' . wp_kses($this->get_navigation_arrow_icon_markup($settings['next_icon'] ?? [], 'next', $legacy_arrows_icon), $this->calio_allowed_icon_markup()) . '</button>';
                echo '</div>';
            }

            if ($show_dots) {
                echo '<div class="calio-woo-slider-pagination" aria-label="' . esc_attr__('Product slider pagination', 'calio-addon') . '"></div>';
            }
            if ($show_scrollbar) {
                echo '<div class="calio-woo-slider-scrollbar" aria-hidden="true"></div>';
            }
            echo '</div>';
        }

        $this->render_scroll_to_section($settings);
        echo '</div>';
    }

    /**
     * @param array<string, mixed> $settings Widget settings.
     * @return array<int, WC_Product>
     */
    private function get_products(array $settings): array
    {
        $source = sanitize_key((string) ($settings['product_source'] ?? 'latest'));
        $limit = max(1, min(24, (int) ($settings['products_per_page'] ?? 8)));
        $order = strtoupper((string) ($settings['order'] ?? 'DESC'));
        if (!in_array($order, ['ASC', 'DESC'], true)) {
            $order = 'DESC';
        }

        $orderby = sanitize_key((string) ($settings['orderby'] ?? 'date'));
        if (!in_array($orderby, ['date', 'title', 'price', 'popularity', 'rating', 'rand'], true)) {
            $orderby = 'date';
        }

        $args = [
            'status' => 'publish',
            'limit' => $limit,
            'orderby' => $orderby,
            'order' => $order,
            'return' => 'objects',
        ];

        if (($settings['hide_out_of_stock'] ?? 'yes') === 'yes') {
            $args['stock_status'] = 'instock';
        }

        if ($source === 'manual') {
            $ids = $this->parse_product_ids((string) ($settings['manual_product_ids'] ?? ''));
            if (empty($ids)) {
                return [];
            }

            $args['include'] = $ids;
            $args['orderby'] = 'include';
        } elseif ($source === 'featured') {
            $args['featured'] = true;
        } elseif ($source === 'sale') {
            $args['include'] = array_map('absint', wc_get_product_ids_on_sale());
            if (empty($args['include'])) {
                return [];
            }
        } elseif ($source === 'best_selling') {
            $args['orderby'] = 'popularity';
        } elseif ($source === 'top_rated') {
            $args['orderby'] = 'rating';
        }

        if ($source !== 'manual' && !empty($settings['product_categories']) && is_array($settings['product_categories'])) {
            $categories = array_values(array_filter(array_map('sanitize_title', $settings['product_categories'])));
            if (!empty($categories)) {
                $args['category'] = $categories;
            }
        }

        $products = wc_get_products($args);

        return is_array($products) ? $products : [];
    }

    private function render_product_slide(WC_Product $product, array $settings): void
    {
        $product_id = $product->get_id();
        $permalink = $product->get_permalink();
        $title = $product->get_name();
        $show_category = ($settings['show_category'] ?? 'yes') === 'yes';
        $show_sale_badge = ($settings['show_sale_badge'] ?? 'yes') === 'yes';
        $show_title = ($settings['show_title'] ?? 'yes') === 'yes';
        $show_rating = ($settings['show_rating'] ?? 'yes') === 'yes';
        $show_price = ($settings['show_price'] ?? 'yes') === 'yes';
        $show_excerpt = ($settings['show_excerpt'] ?? '') === 'yes';
        $show_button = ($settings['show_button'] ?? 'yes') === 'yes';
        $is_showcase = ($settings['skin'] ?? '') === 'showcase';
        $show_read_more = (($settings['show_read_more'] ?? '') === 'yes') || $is_showcase;
        $read_more_text = trim((string) ($settings['read_more_text'] ?? esc_html__('View Product', 'calio-addon')));
        $read_more_display = $this->get_valid_product_link_display((string) ($settings['read_more_display'] ?? ($is_showcase ? 'button' : 'icon-top-right')));
        if ($is_showcase && !isset($settings['read_more_display'])) {
            $read_more_display = 'button';
        }
        $show_read_more_button = $show_read_more && $read_more_text !== '' && $read_more_display === 'button';
        $show_read_more_icon = $show_read_more && $read_more_text !== '' && $read_more_display !== 'button';
        $excerpt_length = max(5, min(40, (int) ($settings['excerpt_length'] ?? 14)));
        $title_tag = $this->get_valid_title_tag((string) ($settings['title_html_tag'] ?? 'h2'));
        $image_size = $is_showcase ? 'woocommerce_single' : 'woocommerce_thumbnail';

        echo '<div class="swiper-slide">';
        echo '<article class="calio-woo-slider-card product">';

        echo '<a class="calio-woo-slider-media" href="' . esc_url($permalink) . '" aria-label="' . esc_attr($title) . '">';
        echo wp_kses_post($product->get_image($image_size, ['loading' => 'lazy']));
        if (!$is_showcase && $show_sale_badge && $product->is_on_sale()) {
            echo '<span class="calio-woo-slider-badge">' . esc_html__('Sale!', 'calio-addon') . '</span>';
        }
        echo '</a>';

        if ($is_showcase && $show_sale_badge && $product->is_on_sale()) {
            echo '<span class="calio-woo-slider-badge">' . esc_html__('Sale!', 'calio-addon') . '</span>';
        }

        if ($show_read_more_icon) {
            $this->render_product_link_icon($permalink, $read_more_text, $settings, $read_more_display);
        }

        echo '<div class="calio-woo-slider-content">';
        if ($show_category) {
            $category = $this->get_primary_product_category($product);
            if ($category !== '') {
                echo '<div class="calio-woo-slider-category">' . esc_html($category) . '</div>';
            }
        }

        if ($show_title) {
            echo '<' . esc_attr($title_tag) . ' class="calio-woo-slider-title"><a href="' . esc_url($permalink) . '">' . esc_html($title) . '</a></' . esc_attr($title_tag) . '>';
        }

        if ($show_rating && $product->get_average_rating() > 0) {
            echo '<div class="calio-woo-slider-rating">' . wp_kses_post(wc_get_rating_html($product->get_average_rating(), $product->get_rating_count())) . '</div>';
        }

        if ($show_price) {
            echo '<div class="calio-woo-slider-price">' . wp_kses_post($product->get_price_html()) . '</div>';
        }

        if ($show_excerpt) {
            $description = wp_trim_words(wp_strip_all_tags($product->get_short_description()), $excerpt_length);
            if ($description !== '') {
                echo '<div class="calio-woo-slider-description">' . esc_html($description) . '</div>';
            }
        }

        if ($show_button || $show_read_more_button) {
            echo '<div class="calio-woo-slider-actions">';
            if ($show_button) {
                $this->render_add_to_cart_button($product, $settings);
            }
            if ($show_read_more_button) {
                echo '<a class="calio-woo-slider-button is-secondary" href="' . esc_url($permalink) . '">' . esc_html($read_more_text) . '</a>';
            }
            echo '</div>';
        }

        echo '</div>';
        echo '</article>';
        echo '</div>';
    }

    private function render_add_to_cart_button(WC_Product $product, array $settings): void
    {
        $classes = [
            'calio-woo-slider-button',
            'is-primary',
            'button',
            'product_type_' . sanitize_html_class($product->get_type()),
        ];

        if ($product->supports('ajax_add_to_cart') && $product->is_purchasable() && $product->is_in_stock()) {
            $classes[] = 'add_to_cart_button';
            $classes[] = 'ajax_add_to_cart';
        }

        $button_text = trim((string) ($settings['button_text'] ?? ''));
        if ($button_text === '') {
            $button_text = $product->add_to_cart_text();
        }

        echo '<a href="' . esc_url($product->add_to_cart_url()) . '"'
            . ' class="' . esc_attr(implode(' ', array_filter($classes))) . '"'
            . ' data-quantity="1"'
            . ' data-product_id="' . esc_attr((string) $product->get_id()) . '"'
            . ' data-product_sku="' . esc_attr((string) $product->get_sku()) . '"'
            . ' aria-label="' . esc_attr($product->add_to_cart_description()) . '"'
            . ' rel="nofollow">';

        if (($settings['icon_align'] ?? 'right') === 'left') {
            echo wp_kses($this->get_button_icon_markup($settings), $this->calio_allowed_icon_markup());
        }

        echo '<span class="calio-woo-slider-button-text">' . esc_html($button_text) . '</span>';

        if (($settings['icon_align'] ?? 'right') !== 'left') {
            echo wp_kses($this->get_button_icon_markup($settings), $this->calio_allowed_icon_markup());
        }

        echo '</a>';
    }

    private function render_product_link_icon(string $permalink, string $label, array $settings, string $display): void
    {
        $classes = [
            'calio-woo-slider-product-link-icon',
            'is-' . sanitize_html_class($display),
        ];

        echo '<a class="' . esc_attr(implode(' ', $classes)) . '" href="' . esc_url($permalink) . '"'
            . ' aria-label="' . esc_attr($label) . '"'
            . ' data-tooltip="' . esc_attr($label) . '">';
        echo wp_kses($this->get_product_link_icon_markup($settings), $this->calio_allowed_icon_markup());
        echo '</a>';
    }

    private function render_scroll_to_section(array $settings): void
    {
        if (($settings['scroll_to_section'] ?? '') !== 'yes') {
            return;
        }

        $section_id = trim((string) ($settings['section_id'] ?? ''));
        if ($section_id === '') {
            return;
        }

        $href = '#' . ltrim($section_id, '#');
        echo '<a class="calio-woo-slider-scroll-link" href="' . esc_url($href) . '" aria-label="' . esc_attr__('Scroll to section', 'calio-addon') . '">';
        echo wp_kses($this->get_icon_markup($settings['scroll_to_section_icon'] ?? []), $this->calio_allowed_icon_markup());
        echo '</a>';
    }

    private function get_button_icon_markup(array $settings): string
    {
        $icon = $settings['button_icon'] ?? [];
        if (!is_array($icon) || empty($icon['value'])) {
            return '';
        }

        $align = ($settings['icon_align'] ?? 'right') === 'left' ? 'left' : 'right';

        ob_start();
        Icons_Manager::render_icon($icon, ['aria-hidden' => 'true']);
        $markup = trim((string) ob_get_clean());
        if ($markup === '') {
            return '';
        }

        return '<span class="calio-button-icon-align-' . esc_attr($align) . '">' . wp_kses($markup, $this->calio_allowed_icon_markup()) . '</span>';
    }

    private function get_product_link_icon_markup(array $settings): string
    {
        $icon = $settings['read_more_icon'] ?? [];
        if (is_array($icon) && !empty($icon['value'])) {
            return '<span class="calio-woo-slider-product-link-icon-mark" aria-hidden="true">' . wp_kses($this->get_icon_markup($icon), $this->calio_allowed_icon_markup()) . '</span>';
        }

        return '<span class="calio-woo-slider-product-link-icon-mark" aria-hidden="true">&rarr;</span>';
    }

    private function get_primary_product_category(WC_Product $product): string
    {
        $category_ids = $product->get_category_ids();
        if (empty($category_ids)) {
            return '';
        }

        $term = get_term((int) $category_ids[0], 'product_cat');
        if (!$term || is_wp_error($term)) {
            return '';
        }

        return (string) $term->name;
    }

    /**
     * @return array<string, string>
     */
    private function get_title_tag_options(): array
    {
        return [
            'h1' => 'H1',
            'h2' => 'H2',
            'h3' => 'H3',
            'h4' => 'H4',
            'h5' => 'H5',
            'h6' => 'H6',
            'div' => 'div',
            'span' => 'span',
            'p' => 'p',
        ];
    }

    private function get_valid_title_tag(string $tag): string
    {
        $allowed = array_keys($this->get_title_tag_options());

        return in_array($tag, $allowed, true) ? $tag : 'h2';
    }

    private function get_legacy_navigation_type(array $settings): string
    {
        $show_arrows = ($settings['show_arrows'] ?? 'yes') === 'yes';
        $show_dots = ($settings['show_dots'] ?? 'yes') === 'yes';

        if ($show_arrows && $show_dots) {
            return 'arrows-dots';
        }

        if ($show_arrows) {
            return 'arrows';
        }

        if ($show_dots) {
            return 'dots';
        }

        return 'none';
    }

    private function get_valid_arrow_position(string $position): string
    {
        $allowed = ['top', 'center', 'bottom', 'bottom-right'];

        return in_array($position, $allowed, true) ? $position : 'center';
    }

    private function get_valid_arrow_icon(string $icon): string
    {
        $allowed = ['default', 'arrow', 'chevron', 'angle'];

        return in_array($icon, $allowed, true) ? $icon : 'default';
    }

    private function get_valid_product_link_display(string $display): string
    {
        $allowed = ['button', 'icon-top-right', 'icon-overlay'];

        return in_array($display, $allowed, true) ? $display : 'icon-top-right';
    }

    private function get_image_style_variables(array $settings): string
    {
        $styles = [];
        $fit = $this->get_valid_image_fit((string) ($settings['image_fit'] ?? 'default'));
        $position = $this->get_valid_image_position((string) ($settings['image_position'] ?? 'default'));

        if ($fit !== '') {
            $styles[] = '--calio-woo-slider-image-fit: ' . $fit . ';';
        }

        if ($position !== '') {
            $styles[] = '--calio-woo-slider-image-position: ' . $position . ';';
        }

        return implode(' ', $styles);
    }

    private function get_valid_image_fit(string $fit): string
    {
        $allowed = ['cover', 'contain', 'fill', 'none', 'scale-down'];

        return in_array($fit, $allowed, true) ? $fit : '';
    }

    private function get_valid_image_position(string $position): string
    {
        $allowed = [
            'top left',
            'top center',
            'top right',
            'center left',
            'center center',
            'center right',
            'bottom left',
            'bottom center',
            'bottom right',
        ];

        return in_array($position, $allowed, true) ? $position : '';
    }

    private function get_arrow_icon_markup(string $direction, string $icon): string
    {
        $is_next = $direction === 'next';
        $icons = [
            'default' => $is_next ? '&rarr;' : '&larr;',
            'arrow' => $is_next ? '&rarr;' : '&larr;',
            'chevron' => $is_next ? '&rsaquo;' : '&lsaquo;',
            'angle' => $is_next ? '&raquo;' : '&laquo;',
        ];

        return '<span class="calio-woo-slider-arrow-icon" aria-hidden="true">' . ($icons[$icon] ?? $icons['default']) . '</span>';
    }

    /**
     * @param mixed $icon Elementor icon control value.
     */
    private function get_navigation_arrow_icon_markup($icon, string $direction, string $legacy_icon): string
    {
        if (is_array($icon) && !empty($icon['value'])) {
            return '<span class="calio-woo-slider-arrow-icon" aria-hidden="true">' . wp_kses($this->get_icon_markup($icon), $this->calio_allowed_icon_markup()) . '</span>';
        }

        return $this->get_arrow_icon_markup($direction, $legacy_icon);
    }

    /**
     * @return array<string, string>
     */
    private function get_product_category_options(): array
    {
        if (!taxonomy_exists('product_cat')) {
            return [];
        }

        $terms = get_terms([
            'taxonomy' => 'product_cat',
            'hide_empty' => false,
        ]);

        if (!is_array($terms) || is_wp_error($terms)) {
            return [];
        }

        $options = [];
        foreach ($terms as $term) {
            if (!isset($term->slug, $term->name)) {
                continue;
            }

            $options[(string) $term->slug] = (string) $term->name;
        }

        return $options;
    }

    /**
     * @return array<int, int>
     */
    private function parse_product_ids(string $value): array
    {
        if ($value === '') {
            return [];
        }

        return array_values(array_unique(array_filter(array_map('absint', preg_split('/[\s,]+/', $value)))));
    }

    private function get_slider_control_size(array $settings, string $key, int $fallback): int
    {
        $value = $settings[$key]['size'] ?? $fallback;

        return max(0, (int) $value);
    }

    private function get_slider_dimension_value(array $settings, string $key, string $fallback): string
    {
        $value = $settings[$key] ?? null;
        if (!is_array($value)) {
            return $fallback;
        }

        $size = $value['size'] ?? null;
        if ($size === null || $size === '' || !is_numeric($size)) {
            return $fallback;
        }

        $unit = isset($value['unit']) ? (string) $value['unit'] : 'px';
        if (!in_array($unit, ['px', 'vh'], true)) {
            $unit = 'px';
        }

        return rtrim(rtrim((string) (float) $size, '0'), '.') . $unit;
    }

    private function get_responsive_dimension_styles(array $settings, string $key, string $css_variable, string $fallback): string
    {
        $desktop = $this->get_slider_dimension_value($settings, $key, $fallback);
        $tablet = $this->get_slider_dimension_value($settings, $key . '_tablet', '');
        $mobile = $this->get_slider_dimension_value($settings, $key . '_mobile', '');
        $styles = [$css_variable . '-desktop: ' . $desktop . ';'];

        if ($tablet !== '') {
            $styles[] = $css_variable . '-tablet: ' . $tablet . ';';
        }

        if ($mobile !== '') {
            $styles[] = $css_variable . '-mobile: ' . $mobile . ';';
        }

        return implode(' ', $styles);
    }
}
