<?php
/**
 * Widget Name: Mini Cart
 * Description: A live WooCommerce mini cart toggle with item count + subtotal badge
 *              that opens a dropdown or slide-in drawer. Auto-refreshes via cart
 *              fragments (add / remove without a page reload), can auto-open when a
 *              product is added, includes an optional "free shipping" progress bar
 *              and ships with multiple attractive styles.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.3.0
 */

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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;

/**
 * Class Captwiki_Woo_Mini_Cart
 *
 * @since 1.3.0
 */
class Captwiki_Woo_Mini_Cart extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-mini-cart';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Mini Cart', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-cart-medium captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-woo-commerce' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'mini cart', 'cart', 'basket', 'drawer', 'menu cart', 'side cart', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.3.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Available visual styles.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Minimal', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Solid Pill', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Outline', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Gradient', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Floating Bubble', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register controls.
	 *
	 * @since 1.3.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_free_shipping_controls();
		$this->register_toggle_style_controls();
		$this->register_badge_style_controls();
		$this->register_panel_style_controls();
		$this->register_items_style_controls();
		$this->register_buttons_style_controls();
	}

	/**
	 * Content tab controls.
	 *
	 * @since 1.3.0
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Mini Cart', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => esc_html__( 'Open As', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'offcanvas-right',
				'options' => array(
					'dropdown'        => esc_html__( 'Dropdown', 'captain-widgets-kit' ),
					'offcanvas-right' => esc_html__( 'Slide-in Drawer (Right)', 'captain-widgets-kit' ),
					'offcanvas-left'  => esc_html__( 'Slide-in Drawer (Left)', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'cart_icon',
			array(
				'label'            => esc_html__( 'Cart Icon', 'captain-widgets-kit' ),
				'type'             => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon',
				'default'          => array(
					'value'   => 'fas fa-shopping-cart',
					'library' => 'fa-solid',
				),
			)
		);

		$this->add_control(
			'show_count',
			array(
				'label'        => esc_html__( 'Show Item Count', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'hide_count_when_empty',
			array(
				'label'        => esc_html__( 'Hide Count When Empty', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => array( 'show_count' => 'yes' ),
			)
		);

		$this->add_control(
			'show_amount',
			array(
				'label'        => esc_html__( 'Show Subtotal', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->add_control(
			'show_label',
			array(
				'label'        => esc_html__( 'Show Label', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->add_control(
			'label_text',
			array(
				'label'       => esc_html__( 'Label', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Cart', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_label' => 'yes' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'panel_title',
			array(
				'label'       => esc_html__( 'Panel Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Shopping Cart', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'open_on_add',
			array(
				'label'        => esc_html__( 'Auto Open on Add', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'description'  => esc_html__( 'Open the cart automatically when a product is added to the cart.', 'captain-widgets-kit' ),
			)
		);

		$this->add_responsive_control(
			'toggle_align',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart' => 'justify-content: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Free shipping bar controls.
	 *
	 * @since 1.3.0
	 */
	private function register_free_shipping_controls(): void {

		$this->start_controls_section(
			'section_free_shipping',
			array(
				'label' => esc_html__( 'Free Shipping Bar', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'show_free_shipping',
			array(
				'label'        => esc_html__( 'Enable Progress Bar', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->add_control(
			'free_shipping_goal',
			array(
				'label'       => esc_html__( 'Goal Amount', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'step'        => 1,
				'default'     => 0,
				'description' => esc_html__( 'Leave 0 to auto-detect from your WooCommerce free shipping method.', 'captain-widgets-kit' ),
				'condition'   => array( 'show_free_shipping' => 'yes' ),
			)
		);

		$this->add_control(
			'free_shipping_progress_text',
			array(
				'label'       => esc_html__( 'In-progress Message', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Add {amount} more to get FREE shipping!', 'captain-widgets-kit' ),
				'label_block' => true,
				'description' => esc_html__( 'Use {amount} for the remaining total.', 'captain-widgets-kit' ),
				'condition'   => array( 'show_free_shipping' => 'yes' ),
			)
		);

		$this->add_control(
			'free_shipping_success_text',
			array(
				'label'       => esc_html__( 'Success Message', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( '🎉 You have unlocked FREE shipping!', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_free_shipping' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Toggle (icon + label) style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_toggle_style_controls(): void {

		$toggle_selector = '{{WRAPPER}} .captwiki-mini-cart__toggle';
		$icon_selector   = '{{WRAPPER}} .captwiki-mini-cart__icon';

		$this->start_controls_section(
			'section_style_toggle',
			array(
				'label' => esc_html__( 'Toggle Button', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'      => esc_html__( 'Icon Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array( 'px' => array( 'min' => 10, 'max' => 80, 'step' => 1 ) ),
				'selectors'  => array(
					$icon_selector . ' svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					$icon_selector          => 'font-size: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'toggle_gap',
			array(
				'label'      => esc_html__( 'Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 40, 'step' => 1 ) ),
				'selectors'  => array(
					$toggle_selector => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'toggle_typography',
				'selector' => $toggle_selector,
			)
		);

		$this->start_controls_tabs( 'tabs_toggle_state' );

			$this->start_controls_tab(
				'tab_toggle_normal',
				array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'toggle_color',
				array(
					'label'     => esc_html__( 'Text / Icon Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$toggle_selector            => 'color: {{VALUE}};',
						$icon_selector . ' svg'     => 'fill: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'toggle_background',
					'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
					'types'    => array( 'classic', 'gradient' ),
					'selector' => $toggle_selector,
					'exclude'  => array( 'image' ),
				)
			);

			$this->end_controls_tab();

			$this->start_controls_tab(
				'tab_toggle_hover',
				array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'toggle_color_hover',
				array(
					'label'     => esc_html__( 'Text / Icon Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$toggle_selector . ':hover'             => 'color: {{VALUE}};',
						$toggle_selector . ':hover .captwiki-mini-cart__icon svg' => 'fill: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'toggle_background_hover',
					'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
					'types'    => array( 'classic', 'gradient' ),
					'selector' => $toggle_selector . ':hover',
					'exclude'  => array( 'image' ),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_toggle_box', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'toggle_border',
				'selector' => $toggle_selector,
			)
		);

		$this->add_responsive_control(
			'toggle_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$toggle_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'toggle_box_shadow',
				'selector' => $toggle_selector,
			)
		);

		$this->add_responsive_control(
			'toggle_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					$toggle_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Count badge style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_badge_style_controls(): void {

		$count_selector  = '{{WRAPPER}} .captwiki-mini-cart__count';
		$amount_selector = '{{WRAPPER}} .captwiki-mini-cart__amount';

		$this->start_controls_section(
			'section_style_badge',
			array(
				'label' => esc_html__( 'Count & Subtotal', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'count_heading',
			array(
				'label' => esc_html__( 'Item Count', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_control(
			'count_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$count_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'count_bg',
			array(
				'label'     => esc_html__( 'Background Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$count_selector => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'count_typography',
				'selector' => $count_selector,
			)
		);

		$this->add_responsive_control(
			'count_size',
			array(
				'label'      => esc_html__( 'Badge Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 14, 'max' => 40, 'step' => 1 ) ),
				'selectors'  => array(
					$count_selector => 'min-width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'amount_heading',
			array(
				'label'     => esc_html__( 'Subtotal', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'amount_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$amount_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'amount_typography',
				'selector' => $amount_selector,
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Panel / drawer style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_panel_style_controls(): void {

		$panel_selector  = '{{WRAPPER}} .captwiki-mini-cart__panel';
		$header_selector = '{{WRAPPER}} .captwiki-mini-cart__title';

		$this->start_controls_section(
			'section_style_panel',
			array(
				'label' => esc_html__( 'Panel', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'panel_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => $panel_selector,
				'exclude'  => array( 'image' ),
			)
		);

		$this->add_responsive_control(
			'panel_width',
			array(
				'label'      => esc_html__( 'Panel Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vw' ),
				'range'      => array(
					'px' => array( 'min' => 260, 'max' => 600, 'step' => 1 ),
					'vw' => array( 'min' => 30, 'max' => 100, 'step' => 1 ),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mini-cart[data-layout^="offcanvas"] .captwiki-mini-cart__panel' => 'width: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .captwiki-mini-cart[data-layout="dropdown"] .captwiki-mini-cart__panel'   => 'width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'panel_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$panel_selector . ', ' . $panel_selector . ' a, ' . $panel_selector . ' .woocommerce-mini-cart__total' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'panel_border',
				'selector' => $panel_selector,
			)
		);

		$this->add_responsive_control(
			'panel_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$panel_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'panel_box_shadow',
				'selector' => $panel_selector,
			)
		);

		$this->add_responsive_control(
			'panel_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$panel_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'panel_title_heading',
			array(
				'label'     => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'panel_title_color',
			array(
				'label'     => esc_html__( 'Title Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$header_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'panel_title_typography',
				'selector' => $header_selector,
			)
		);

		$this->add_control(
			'overlay_color',
			array(
				'label'     => esc_html__( 'Overlay Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart__overlay' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Cart item list style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_items_style_controls(): void {

		$list_selector = '{{WRAPPER}} .captwiki-mini-cart__panel .woocommerce-mini-cart';

		$this->start_controls_section(
			'section_style_items',
			array(
				'label' => esc_html__( 'Cart Items', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'item_name_color',
			array(
				'label'     => esc_html__( 'Product Name Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$list_selector . ' .woocommerce-mini-cart-item a:not(.remove)' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'item_price_color',
			array(
				'label'     => esc_html__( 'Price Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$list_selector . ' .quantity, ' . $list_selector . ' .amount' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'item_divider_color',
			array(
				'label'     => esc_html__( 'Divider Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$list_selector . ' li' => 'border-bottom-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'remove_heading',
			array(
				'label'     => esc_html__( 'Remove Button', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'remove_color',
			array(
				'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart__panel a.remove' => 'color: {{VALUE}}; border-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'remove_color_hover',
			array(
				'label'     => esc_html__( 'Hover Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart__panel a.remove:hover' => 'color: {{VALUE}}; border-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'fs_heading',
			array(
				'label'     => esc_html__( 'Free Shipping Bar', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'fs_track_color',
			array(
				'label'     => esc_html__( 'Track Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart__fs-track' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'fs_fill_color',
			array(
				'label'     => esc_html__( 'Fill Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart__fs-fill' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'fs_text_color',
			array(
				'label'     => esc_html__( 'Message Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mini-cart__fs-msg' => 'color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * View cart / checkout button style controls.
	 *
	 * @since 1.3.0
	 */
	private function register_buttons_style_controls(): void {

		$buttons_selector  = '{{WRAPPER}} .captwiki-mini-cart__panel .woocommerce-mini-cart__buttons a.button';
		$checkout_selector = '{{WRAPPER}} .captwiki-mini-cart__panel .woocommerce-mini-cart__buttons a.checkout';

		$this->start_controls_section(
			'section_style_buttons',
			array(
				'label' => esc_html__( 'Buttons', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'buttons_typography',
				'selector' => $buttons_selector,
			)
		);

		$this->add_control(
			'view_cart_heading',
			array(
				'label' => esc_html__( 'View Cart', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_control(
			'view_cart_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$buttons_selector . ':not(.checkout)' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'view_cart_bg',
			array(
				'label'     => esc_html__( 'Background Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$buttons_selector . ':not(.checkout)' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'checkout_heading',
			array(
				'label'     => esc_html__( 'Checkout', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'checkout_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$checkout_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'checkout_bg',
			array(
				'label'     => esc_html__( 'Background Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$checkout_selector => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'buttons_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'separator'  => 'before',
				'selectors'  => array(
					$buttons_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'buttons_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$buttons_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Detect the free shipping goal from WooCommerce shipping methods.
	 *
	 * @since 1.3.0
	 * @return float Smallest configured free-shipping minimum amount, or 0 if none.
	 */
	private function captwiki_auto_free_shipping_goal(): float {

		$goal = 0.0;

		if ( ! class_exists( 'WC_Shipping_Zones' ) ) {
			return (float) apply_filters( 'captwiki_mini_cart_free_shipping_goal', $goal );
		}

		$zones   = WC_Shipping_Zones::get_zones();
		$zones[] = array( 'zone_id' => 0 ); // "Rest of the World" zone.

		foreach ( $zones as $zone_data ) {

			$zone    = WC_Shipping_Zones::get_zone( isset( $zone_data['zone_id'] ) ? (int) $zone_data['zone_id'] : (int) $zone_data['id'] );
			$methods = $zone ? $zone->get_shipping_methods( true ) : array();

			foreach ( $methods as $method ) {

				if ( 'free_shipping' !== $method->id ) {
					continue;
				}

				$min = isset( $method->min_amount ) ? (float) $method->min_amount : 0.0;

				if ( $min > 0 && ( 0.0 === $goal || $min < $goal ) ) {
					$goal = $min;
				}
			}
		}

		return (float) apply_filters( 'captwiki_mini_cart_free_shipping_goal', $goal );
	}

	/**
	 * Render the free shipping progress bar markup.
	 *
	 * The fill width + message are recalculated client-side after every cart
	 * change; this is the initial server-rendered state.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 */
	private function captwiki_render_free_shipping_bar( array $settings ): void {

		if ( empty( $settings['show_free_shipping'] ) || 'yes' !== $settings['show_free_shipping'] ) {
			return;
		}

		$goal = ! empty( $settings['free_shipping_goal'] ) ? (float) $settings['free_shipping_goal'] : $this->captwiki_auto_free_shipping_goal();

		if ( $goal <= 0 ) {
			return;
		}

		$subtotal = ( function_exists( 'WC' ) && WC()->cart ) ? (float) WC()->cart->get_subtotal() : 0.0;
		$remaining = max( 0, $goal - $subtotal );
		$percent   = $goal > 0 ? min( 100, ( $subtotal / $goal ) * 100 ) : 0;
		$complete  = $remaining <= 0;

		$progress_tpl = ! empty( $settings['free_shipping_progress_text'] ) ? $settings['free_shipping_progress_text'] : esc_html__( 'Add {amount} more to get FREE shipping!', 'captain-widgets-kit' );
		$success_tpl  = ! empty( $settings['free_shipping_success_text'] ) ? $settings['free_shipping_success_text'] : esc_html__( 'You have unlocked FREE shipping!', 'captain-widgets-kit' );

		$message = $complete
			? $success_tpl
			: str_replace( '{amount}', wp_strip_all_tags( wc_price( $remaining ) ), $progress_tpl );

		printf(
			'<div class="captwiki-mini-cart__fs%1$s" data-goal="%2$s" data-progress-text="%3$s" data-success-text="%4$s">',
			$complete ? ' is-complete' : '',
			esc_attr( (string) $goal ),
			esc_attr( $progress_tpl ),
			esc_attr( $success_tpl )
		);
		echo '<p class="captwiki-mini-cart__fs-msg">' . esc_html( $message ) . '</p>';
		echo '<div class="captwiki-mini-cart__fs-track"><span class="captwiki-mini-cart__fs-fill" style="width:' . esc_attr( (string) $percent ) . '%"></span></div>';
		echo '</div>';
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.3.0
	 * @param string $message Notice text.
	 */
	private function captwiki_render_editor_notice( string $message ): void {

		if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		echo '<div class="captwiki-mini-cart captwiki-mini-cart--notice"><p>' . esc_html( $message ) . '</p></div>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.3.0
	 */
	protected function render(): void {

		if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'WC' ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'WooCommerce is required for the Mini Cart widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings = $this->get_settings_for_display();

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = ! empty( $settings['widget_style'] ) && in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		$allowed_layouts = array( 'dropdown', 'offcanvas-right', 'offcanvas-left' );
		$layout          = ! empty( $settings['layout'] ) && in_array( $settings['layout'], $allowed_layouts, true ) ? $settings['layout'] : 'offcanvas-right';

		// Scripts: WooCommerce fragments + add-to-cart (handles remove links), then ours.
		wp_enqueue_script( 'wc-cart-fragments' );
		wp_enqueue_script( 'wc-add-to-cart' );
		wp_enqueue_script( 'captwiki-woo-mini-cart' );

		wp_localize_script(
			'captwiki-woo-mini-cart',
			'captwikiMiniCart',
			array(
				'currencySymbol' => html_entity_decode( get_woocommerce_currency_symbol() ),
				'decimals'       => wc_get_price_decimals(),
				'decimalSep'     => wc_get_price_decimal_separator(),
				'thousandSep'    => wc_get_price_thousand_separator(),
				'priceFormat'    => html_entity_decode( get_woocommerce_price_format() ),
			)
		);

		// Load only the active style stylesheet, depending on the common one.
		wp_enqueue_style(
			'captwiki-woo-mini-cart-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-mini-cart/captwiki-woo-mini-cart-' . $style . '.css',
			array( 'captwiki-woo-mini-cart' ),
			CAPTWIKI_VERSION
		);

		$show_count = ! empty( $settings['show_count'] ) && 'yes' === $settings['show_count'];
		$show_amt   = ! empty( $settings['show_amount'] ) && 'yes' === $settings['show_amount'];
		$show_label = ! empty( $settings['show_label'] ) && 'yes' === $settings['show_label'];
		$hide_empty = ! empty( $settings['hide_count_when_empty'] ) && 'yes' === $settings['hide_count_when_empty'];
		$open_add   = ! empty( $settings['open_on_add'] ) && 'yes' === $settings['open_on_add'];

		$panel_title = ! empty( $settings['panel_title'] ) ? $settings['panel_title'] : esc_html__( 'Shopping Cart', 'captain-widgets-kit' );
		$label_text  = ! empty( $settings['label_text'] ) ? $settings['label_text'] : esc_html__( 'Cart', 'captain-widgets-kit' );

		$loader = Captwiki_Widgets_Load::get_instance();

		// Pre-render the cart icon (trusted Icons_Manager SVG markup).
		$icon_html = '';
		if ( ! empty( $settings['cart_icon']['value'] ) ) {
			ob_start();
			Icons_Manager::render_icon( $settings['cart_icon'], array( 'aria-hidden' => 'true' ) );
			$icon_html = ob_get_clean();
		}

		$wrapper_classes = array(
			'captwiki-mini-cart',
			'captwiki-' . $style,
			'captwiki-widget-' . $this->get_id(),
		);

		if ( $hide_empty ) {
			$wrapper_classes[] = 'captwiki-mini-cart--hide-empty';
		}

		$cart_url = wc_get_cart_url();

		?>
		<div class="<?php echo esc_attr( implode( ' ', $wrapper_classes ) ); ?>"
			data-layout="<?php echo esc_attr( $layout ); ?>"
			data-open-on-add="<?php echo $open_add ? '1' : '0'; ?>">

			<a href="<?php echo esc_url( $cart_url ); ?>" class="captwiki-mini-cart__toggle" aria-haspopup="true" aria-expanded="false" aria-label="<?php esc_attr_e( 'View your shopping cart', 'captain-widgets-kit' ); ?>">
				<span class="captwiki-mini-cart__icon" aria-hidden="true">
					<?php echo $icon_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				</span>
				<?php if ( $show_label ) : ?>
					<span class="captwiki-mini-cart__label"><?php echo esc_html( $label_text ); ?></span>
				<?php endif; ?>
				<?php if ( $show_amt ) : ?>
					<?php echo $loader->captwiki_mini_cart_amount_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<?php endif; ?>
				<?php if ( $show_count ) : ?>
					<?php echo $loader->captwiki_mini_cart_count_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<?php endif; ?>
			</a>

			<div class="captwiki-mini-cart__overlay" aria-hidden="true"></div>

			<div class="captwiki-mini-cart__panel" role="dialog" aria-label="<?php echo esc_attr( $panel_title ); ?>" aria-modal="true">
				<div class="captwiki-mini-cart__header">
					<span class="captwiki-mini-cart__title"><?php echo esc_html( $panel_title ); ?></span>
					<button type="button" class="captwiki-mini-cart__close" aria-label="<?php esc_attr_e( 'Close cart', 'captain-widgets-kit' ); ?>">
						<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6L6 18M6 6l12 12"></path></svg>
					</button>
				</div>

				<div class="captwiki-mini-cart__body">
					<?php $this->captwiki_render_free_shipping_bar( $settings ); ?>
					<div class="widget_shopping_cart_content">
						<?php woocommerce_mini_cart(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
