<?php
/**
 * Storzen v1.1.3 — New Widgets
 *
 * Two new widgets added for v1.1.3:
 *
 *  • Storzen_Checkout_Trust_Badges_Widget
 *      Category:  storzen-cart  (Checkout section)
 *      Toggle:    checkout_trust_badges
 *      Purpose:   SSL, payment, and returns trust badges to boost checkout
 *                 conversion. Presets + custom icon/image upload. Horizontal
 *                 or grid layout. Fully styled from the Elementor Style tab.
 *
 *  • Storzen_Product_Breadcrumbs_Widget
 *      Category:  storzen-product-details
 *      Toggle:    product_breadcrumbs
 *      Purpose:   WooCommerce breadcrumb trail for Elementor product page
 *                 templates. Uses woocommerce_breadcrumb() so theme overrides
 *                 and SEO plugins (Yoast, RankMath) apply automatically.
 *
 * INSTALLATION
 *   Copy to: storzen/elementor/widgets/class-storzen-style-v113-widgets.php
 *   Then apply the patch file (see patch instructions).
 *
 * @package storzen
 * @since   1.1.3
 */

namespace Elementor;

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

// ─────────────────────────────────────────────────────────────────────────────
// Widget 1: Checkout Trust Badges
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Trust badges widget for the checkout page.
 *
 * Renders a row or grid of trust icons (SSL lock, secure payment, money-back,
 * payment logos, etc.) to reduce checkout anxiety and increase purchase confidence.
 *
 * Built-in presets use inline SVG so they work without Font Awesome and respect
 * the user's chosen icon color from the Elementor panel. Custom items support
 * either a Dashicon / Font Awesome icon class or a custom image upload.
 *
 * Works on both the classic WooCommerce checkout page and Elementor-built
 * checkout templates. Also safe to place on cart pages, product pages, or
 * anywhere else trust signals are useful.
 *
 * Category:   storzen-cart
 * Toggle key: checkout_trust_badges
 *
 * @since 1.1.3
 */
class Storzen_Checkout_Trust_Badges_Widget extends Widget_Base {

	use Storzen_Cart_Widget_Helpers;

	// ── Identity ──────────────────────────────────────────────────────────────

	public function get_name() {
		return 'storzen-checkout-trust-badges';
	}

	public function get_title() {
		return esc_html__( 'Trust Badges', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-shield';
	}

	public function get_categories() {
		return array( 'storzen-cart' );
	}

	public function get_keywords() {
		return array( 'trust', 'badges', 'secure', 'ssl', 'payment', 'checkout', 'storzen' );
	}

	// ── Controls ──────────────────────────────────────────────────────────────

	protected function register_controls() {

		// ── Content tab: Badge Presets ────────────────────────────────────────

		$this->start_controls_section(
			'section_presets',
			array(
				'label' => esc_html__( 'Badges', 'storzen' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'source',
			array(
				'label'   => esc_html__( 'Badge Source', 'storzen' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'presets',
				'options' => array(
					'presets' => esc_html__( 'Built-in Presets', 'storzen' ),
					'custom'  => esc_html__( 'Custom Badges', 'storzen' ),
					'both'    => esc_html__( 'Presets + Custom', 'storzen' ),
				),
			)
		);

		// ── Preset toggles ─────────────────────────────────────────────────

		$this->add_control(
			'preset_ssl',
			array(
				'label'        => esc_html__( 'SSL Secure', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'label_on'     => esc_html__( 'Show', 'storzen' ),
				'label_off'    => esc_html__( 'Hide', 'storzen' ),
				'return_value' => 'yes',
				'condition'    => array( 'source' => array( 'presets', 'both' ) ),
			)
		);

		$this->add_control(
			'preset_ssl_label',
			array(
				'label'     => esc_html__( 'SSL Label', 'storzen' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'SSL Secure', 'storzen' ),
				'condition' => array(
					'source'     => array( 'presets', 'both' ),
					'preset_ssl' => 'yes',
				),
			)
		);

		$this->add_control(
			'preset_money_back',
			array(
				'label'        => esc_html__( 'Money-Back Guarantee', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'label_on'     => esc_html__( 'Show', 'storzen' ),
				'label_off'    => esc_html__( 'Hide', 'storzen' ),
				'return_value' => 'yes',
				'condition'    => array( 'source' => array( 'presets', 'both' ) ),
			)
		);

		$this->add_control(
			'preset_money_back_label',
			array(
				'label'     => esc_html__( 'Money-Back Label', 'storzen' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( '30-Day Returns', 'storzen' ),
				'condition' => array(
					'source'             => array( 'presets', 'both' ),
					'preset_money_back'  => 'yes',
				),
			)
		);

		$this->add_control(
			'preset_secure_payment',
			array(
				'label'        => esc_html__( 'Secure Payment', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'label_on'     => esc_html__( 'Show', 'storzen' ),
				'label_off'    => esc_html__( 'Hide', 'storzen' ),
				'return_value' => 'yes',
				'condition'    => array( 'source' => array( 'presets', 'both' ) ),
			)
		);

		$this->add_control(
			'preset_secure_payment_label',
			array(
				'label'     => esc_html__( 'Secure Payment Label', 'storzen' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Secure Payment', 'storzen' ),
				'condition' => array(
					'source'                 => array( 'presets', 'both' ),
					'preset_secure_payment'  => 'yes',
				),
			)
		);

		$this->add_control(
			'preset_free_shipping',
			array(
				'label'        => esc_html__( 'Free Shipping', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'no',
				'label_on'     => esc_html__( 'Show', 'storzen' ),
				'label_off'    => esc_html__( 'Hide', 'storzen' ),
				'return_value' => 'yes',
				'condition'    => array( 'source' => array( 'presets', 'both' ) ),
			)
		);

		$this->add_control(
			'preset_free_shipping_label',
			array(
				'label'     => esc_html__( 'Shipping Label', 'storzen' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Free Shipping', 'storzen' ),
				'condition' => array(
					'source'               => array( 'presets', 'both' ),
					'preset_free_shipping' => 'yes',
				),
			)
		);

		$this->add_control(
			'preset_support',
			array(
				'label'        => esc_html__( '24/7 Support', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'no',
				'label_on'     => esc_html__( 'Show', 'storzen' ),
				'label_off'    => esc_html__( 'Hide', 'storzen' ),
				'return_value' => 'yes',
				'condition'    => array( 'source' => array( 'presets', 'both' ) ),
			)
		);

		$this->add_control(
			'preset_support_label',
			array(
				'label'     => esc_html__( 'Support Label', 'storzen' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( '24/7 Support', 'storzen' ),
				'condition' => array(
					'source'         => array( 'presets', 'both' ),
					'preset_support' => 'yes',
				),
			)
		);

		// ── Custom badge repeater ───────────────────────────────────────────

		$repeater = new Repeater();

		$repeater->add_control(
			'badge_type',
			array(
				'label'   => esc_html__( 'Badge Type', 'storzen' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'icon',
				'options' => array(
					'icon'  => esc_html__( 'Icon (Font Awesome)', 'storzen' ),
					'image' => esc_html__( 'Custom Image', 'storzen' ),
				),
			)
		);

		$repeater->add_control(
			'badge_icon',
			array(
				'label'     => esc_html__( 'Icon', 'storzen' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array( 'value' => 'fas fa-lock', 'library' => 'fa-solid' ),
				'condition' => array( 'badge_type' => 'icon' ),
			)
		);

		$repeater->add_control(
			'badge_image',
			array(
				'label'     => esc_html__( 'Image', 'storzen' ),
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'badge_type' => 'image' ),
			)
		);

		$repeater->add_control(
			'badge_label',
			array(
				'label'       => esc_html__( 'Label', 'storzen' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Secure', 'storzen' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'custom_badges',
			array(
				'label'       => esc_html__( 'Custom Badges', 'storzen' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'badge_type'  => 'icon',
						'badge_icon'  => array( 'value' => 'fas fa-lock', 'library' => 'fa-solid' ),
						'badge_label' => esc_html__( 'SSL Secure', 'storzen' ),
					),
				),
				'title_field' => '{{{ badge_label }}}',
				'condition'   => array( 'source' => array( 'custom', 'both' ) ),
			)
		);

		$this->end_controls_section();

		// ── Content tab: Layout ───────────────────────────────────────────────

		$this->start_controls_section(
			'section_layout',
			array(
				'label' => esc_html__( 'Layout', 'storzen' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => esc_html__( 'Layout', 'storzen' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'row',
				'options' => array(
					'row'  => esc_html__( 'Horizontal Row', 'storzen' ),
					'grid' => esc_html__( 'Grid', 'storzen' ),
				),
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'          => esc_html__( 'Columns', 'storzen' ),
				'type'           => Controls_Manager::SELECT,
				'default'        => '3',
				'tablet_default' => '2',
				'mobile_default' => '2',
				'options'        => array(
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
				),
				'selectors'      => array(
					'{{WRAPPER}} .sz-trust-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);',
				),
				'condition'      => array( 'layout' => 'grid' ),
			)
		);

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

		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'storzen' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array( 'title' => esc_html__( 'Left', 'storzen' ),   'icon' => 'eicon-text-align-left' ),
					'center'     => array( 'title' => esc_html__( 'Center', 'storzen' ), 'icon' => 'eicon-text-align-center' ),
					'flex-end'   => array( 'title' => esc_html__( 'Right', 'storzen' ),  'icon' => 'eicon-text-align-right' ),
				),
				'default'   => 'center',
				'selectors' => array(
					'{{WRAPPER}} .sz-trust-badges-row' => 'justify-content: {{VALUE}};',
					'{{WRAPPER}} .sz-trust-grid'       => 'justify-items: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'gap',
			array(
				'label'      => esc_html__( 'Badge Gap', 'storzen' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 4, 'max' => 60 ) ),
				'default'    => array( 'unit' => 'px', 'size' => 20 ),
				'selectors'  => array(
					'{{WRAPPER}} .sz-trust-badges-row' => 'gap: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sz-trust-grid'       => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// ── Style tab: Icon ───────────────────────────────────────────────────

		$this->start_controls_section(
			'section_style_icon',
			array(
				'label' => esc_html__( 'Icon / Image', 'storzen' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#374151',
				'selectors' => array(
					'{{WRAPPER}} .sz-trust-badge__icon i'   => 'color: {{VALUE}};',
					'{{WRAPPER}} .sz-trust-badge__icon svg' => 'fill: {{VALUE}};',
					'{{WRAPPER}} .sz-trust-badge__svg path,
					 {{WRAPPER}} .sz-trust-badge__svg rect,
					 {{WRAPPER}} .sz-trust-badge__svg circle' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'      => esc_html__( 'Icon Size', 'storzen' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 14, 'max' => 80 ) ),
				'default'    => array( 'unit' => 'px', 'size' => 28 ),
				'selectors'  => array(
					'{{WRAPPER}} .sz-trust-badge__icon i'   => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sz-trust-badge__icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sz-trust-badge__svg'      => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sz-trust-badge__img'      => 'width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// ── Style tab: Label ──────────────────────────────────────────────────

		$this->start_controls_section(
			'section_style_label',
			array(
				'label'     => esc_html__( 'Label', 'storzen' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_label' => 'yes' ),
			)
		);

		$this->add_control(
			'label_color',
			array(
				'label'     => esc_html__( 'Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#6b7280',
				'selectors' => array(
					'{{WRAPPER}} .sz-trust-badge__label' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'label_typography',
				'selector' => '{{WRAPPER}} .sz-trust-badge__label',
			)
		);

		$this->add_responsive_control(
			'label_margin_top',
			array(
				'label'      => esc_html__( 'Gap to Icon', 'storzen' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 20 ) ),
				'default'    => array( 'unit' => 'px', 'size' => 6 ),
				'selectors'  => array(
					'{{WRAPPER}} .sz-trust-badge__label' => 'margin-top: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// ── Style tab: Badge item ─────────────────────────────────────────────

		$this->start_controls_section(
			'section_style_item',
			array(
				'label' => esc_html__( 'Badge Item', 'storzen' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'item_bg',
			array(
				'label'     => esc_html__( 'Background', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sz-trust-badge' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'item_border',
				'selector' => '{{WRAPPER}} .sz-trust-badge',
			)
		);

		$this->add_control(
			'item_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'storzen' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 30 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .sz-trust-badge' => 'border-radius: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => esc_html__( 'Padding', 'storzen' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sz-trust-badge' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	// ── Render ────────────────────────────────────────────────────────────────

	protected function render() {
		$s          = $this->get_settings_for_display();
		$source     = $s['source']      ?? 'presets';
		$layout     = $s['layout']      ?? 'row';
		$show_label = 'yes' === ( $s['show_label'] ?? 'yes' );

		$badges = array();

		// ── Preset badges ────────────────────────────────────────────────
		if ( in_array( $source, array( 'presets', 'both' ), true ) ) {

			if ( 'yes' === ( $s['preset_ssl'] ?? 'yes' ) ) {
				$badges[] = array(
					'type'  => 'svg_preset',
					'key'   => 'ssl',
					'label' => $s['preset_ssl_label'] ?? esc_html__( 'SSL Secure', 'storzen' ),
				);
			}
			if ( 'yes' === ( $s['preset_money_back'] ?? 'yes' ) ) {
				$badges[] = array(
					'type'  => 'svg_preset',
					'key'   => 'money_back',
					'label' => $s['preset_money_back_label'] ?? esc_html__( '30-Day Returns', 'storzen' ),
				);
			}
			if ( 'yes' === ( $s['preset_secure_payment'] ?? 'yes' ) ) {
				$badges[] = array(
					'type'  => 'svg_preset',
					'key'   => 'secure_payment',
					'label' => $s['preset_secure_payment_label'] ?? esc_html__( 'Secure Payment', 'storzen' ),
				);
			}
			if ( 'yes' === ( $s['preset_free_shipping'] ?? 'no' ) ) {
				$badges[] = array(
					'type'  => 'svg_preset',
					'key'   => 'free_shipping',
					'label' => $s['preset_free_shipping_label'] ?? esc_html__( 'Free Shipping', 'storzen' ),
				);
			}
			if ( 'yes' === ( $s['preset_support'] ?? 'no' ) ) {
				$badges[] = array(
					'type'  => 'svg_preset',
					'key'   => 'support',
					'label' => $s['preset_support_label'] ?? esc_html__( '24/7 Support', 'storzen' ),
				);
			}
		}

		// ── Custom badges ────────────────────────────────────────────────
		if ( in_array( $source, array( 'custom', 'both' ), true ) && ! empty( $s['custom_badges'] ) ) {
			foreach ( $s['custom_badges'] as $item ) {
				$badges[] = array(
					'type'  => $item['badge_type'] ?? 'icon',
					'icon'  => $item['badge_icon']  ?? array(),
					'image' => $item['badge_image'] ?? array(),
					'label' => $item['badge_label'] ?? '',
				);
			}
		}

		if ( empty( $badges ) ) {
			return;
		}

		$wrapper_class = 'grid' === $layout
			? 'sz-trust-badges-wrap sz-trust-grid'
			: 'sz-trust-badges-wrap sz-trust-badges-row';

		echo '<div class="' . esc_attr( $wrapper_class ) . '">';

		foreach ( $badges as $badge ) {
			echo '<div class="sz-trust-badge">';
			echo '<div class="sz-trust-badge__icon" aria-hidden="true">';

			if ( 'svg_preset' === $badge['type'] ) {
				echo $this->get_preset_svg( $badge['key'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG hardcoded below
			} elseif ( 'icon' === $badge['type'] && ! empty( $badge['icon']['value'] ) ) {
				Icons_Manager::render_icon( $badge['icon'], array( 'aria-hidden' => 'true' ) );
			} elseif ( 'image' === $badge['type'] && ! empty( $badge['image']['id'] ) ) {
				echo wp_get_attachment_image(
					(int) $badge['image']['id'],
					'thumbnail',
					false,
					array( 'class' => 'sz-trust-badge__img', 'loading' => 'lazy' )
				);
			}

			echo '</div>'; // .sz-trust-badge__icon

			if ( $show_label && '' !== trim( (string) ( $badge['label'] ?? '' ) ) ) {
				printf(
					'<span class="sz-trust-badge__label">%s</span>',
					esc_html( $badge['label'] )
				);
			}

			echo '</div>'; // .sz-trust-badge
		}

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

	// ── Preset SVGs ───────────────────────────────────────────────────────────

	/**
	 * Returns a hardcoded, safe inline SVG for each preset badge type.
	 * Colors are controlled via CSS fill on .sz-trust-badge__svg path.
	 *
	 * @param string $key Preset key: ssl | money_back | secure_payment | free_shipping | support
	 * @return string Safe SVG markup.
	 */
	private function get_preset_svg( string $key ): string {
		$svgs = array(

			'ssl' => '<svg class="sz-trust-badge__svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2L4 5v6c0 5.25 3.5 10.15 8 11.5C16.5 21.15 20 16.25 20 11V5l-8-3z"/><path fill="#fff" d="M10 12.5l-2-2-1.4 1.4L10 15.3l7-7L15.6 6.9z"/></svg>',

			'money_back' => '<svg class="sz-trust-badge__svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2a10 10 0 1 0 0 20A10 10 0 0 0 12 2zm0 18a8 8 0 1 1 0-16 8 8 0 0 1 0 16zm.5-13H11v6l5.25 3.15.75-1.23-4.5-2.67V7z"/><path d="M8 6l-4 4 4 4V11h8V9H8V6z"/></svg>',

			'secure_payment' => '<svg class="sz-trust-badge__svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="2" y="5" width="20" height="14" rx="2"/><rect x="2" y="8" width="20" height="3" fill="#fff" opacity=".4"/><rect x="5" y="14" width="4" height="2" rx="1" fill="#fff"/><rect x="11" y="14" width="4" height="2" rx="1" fill="#fff"/></svg>',

			'free_shipping' => '<svg class="sz-trust-badge__svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M20 8h-3V4H3c-1.1 0-2 .9-2 2v11h2a3 3 0 0 0 6 0h6a3 3 0 0 0 6 0h2v-5l-3-4zM6 18.5A1.5 1.5 0 1 1 7.5 17 1.5 1.5 0 0 1 6 18.5zm13.5-9 1.96 2.5H17V9.5h2.5zM18 18.5A1.5 1.5 0 1 1 19.5 17 1.5 1.5 0 0 1 18 18.5z"/></svg>',

			'support' => '<svg class="sz-trust-badge__svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 2C6.48 2 2 6.48 2 12v5a3 3 0 0 0 3 3h1v-8H4v-2a8 8 0 1 1 16 0v2h-2v8h1a3 3 0 0 0 3-3v-5c0-5.52-4.48-10-10-10z"/></svg>',
		);

		return $svgs[ $key ] ?? '';
	}
}


// ─────────────────────────────────────────────────────────────────────────────
// Widget 2: Product Breadcrumbs
// ─────────────────────────────────────────────────────────────────────────────

/**
 * WooCommerce breadcrumb trail widget for Elementor product page templates.
 *
 * Uses woocommerce_breadcrumb() so:
 *  - Theme overrides (woocommerce/global/breadcrumb.php) apply automatically
 *  - Yoast SEO, RankMath, and SEOPress override the breadcrumb output if enabled
 *  - All WooCommerce breadcrumb filters work (woocommerce_breadcrumb_defaults,
 *    woocommerce_breadcrumb_home_url, woocommerce_get_breadcrumb, etc.)
 *
 * Category:   storzen-product-details
 * Toggle key: product_breadcrumbs
 *
 * @since 1.1.3
 */
class Storzen_Product_Breadcrumbs_Widget extends Widget_Base {

	use Storzen_Product_Part_Helpers;

	// ── Identity ──────────────────────────────────────────────────────────────

	public function get_name() {
		return 'storzen-product-breadcrumbs';
	}

	public function get_title() {
		return esc_html__( 'Product Breadcrumbs', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-navigation-horizontal';
	}

	public function get_categories() {
		return array( 'storzen-product-details' );
	}

	public function get_keywords() {
		return array( 'breadcrumb', 'breadcrumbs', 'navigation', 'path', 'product', 'storzen' );
	}

	// ── Controls ──────────────────────────────────────────────────────────────

	protected function register_controls() {

		// ── Content tab ───────────────────────────────────────────────────────

		$this->start_controls_section(
			'section_breadcrumb_content',
			array(
				'label' => esc_html__( 'Breadcrumbs', 'storzen' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'delimiter',
			array(
				'label'       => esc_html__( 'Separator', 'storzen' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '/',
				'placeholder' => '/',
				'description' => esc_html__( 'Character shown between breadcrumb items. Examples: / › → •', 'storzen' ),
			)
		);

		$this->add_control(
			'show_home',
			array(
				'label'        => esc_html__( 'Show Home Link', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'label_on'     => esc_html__( 'Show', 'storzen' ),
				'label_off'    => esc_html__( 'Hide', 'storzen' ),
				'return_value' => 'yes',
			)
		);

		$this->add_control(
			'home_label',
			array(
				'label'     => esc_html__( 'Home Label', 'storzen' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Home', 'storzen' ),
				'condition' => array( 'show_home' => 'yes' ),
			)
		);

		$this->add_responsive_control(
			'align',
			array(
				'label'     => esc_html__( 'Alignment', 'storzen' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array( 'title' => esc_html__( 'Left', 'storzen' ),   'icon' => 'eicon-text-align-left' ),
					'center' => array( 'title' => esc_html__( 'Center', 'storzen' ), 'icon' => 'eicon-text-align-center' ),
					'right'  => array( 'title' => esc_html__( 'Right', 'storzen' ),  'icon' => 'eicon-text-align-right' ),
				),
				'default'   => 'left',
				'selectors' => array(
					'{{WRAPPER}} .sz-breadcrumbs-wrap,
					 {{WRAPPER}} .sz-breadcrumbs-wrap .woocommerce-breadcrumb' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();

		// ── Style tab ─────────────────────────────────────────────────────────

		$this->start_controls_section(
			'section_breadcrumb_style',
			array(
				'label' => esc_html__( 'Style', 'storzen' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'breadcrumb_typography',
				'selector' => '{{WRAPPER}} .woocommerce-breadcrumb,
				               {{WRAPPER}} .woocommerce-breadcrumb a,
				               {{WRAPPER}} .sz-breadcrumbs-wrap',
			)
		);

		$this->add_control(
			'link_color',
			array(
				'label'     => esc_html__( 'Link Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#6b7280',
				'selectors' => array(
					'{{WRAPPER}} .woocommerce-breadcrumb a' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'link_hover_color',
			array(
				'label'     => esc_html__( 'Link Hover Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111827',
				'selectors' => array(
					'{{WRAPPER}} .woocommerce-breadcrumb a:hover' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'current_color',
			array(
				'label'     => esc_html__( 'Current Item Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111827',
				'selectors' => array(
					'{{WRAPPER}} .woocommerce-breadcrumb' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'separator_color',
			array(
				'label'       => esc_html__( 'Separator Color', 'storzen' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '#d1d5db',
				'description' => esc_html__( 'Note: applies to the wrapper; native WC breadcrumb renders separators as plain text, so this colors the entire breadcrumb text and you can adjust link and current colors separately above.', 'storzen' ),
				'selectors'   => array(
					'{{WRAPPER}} .woocommerce-breadcrumb' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'margin',
			array(
				'label'      => esc_html__( 'Margin', 'storzen' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sz-breadcrumbs-wrap' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	// ── Render ────────────────────────────────────────────────────────────────

	protected function render() {

		if ( ! function_exists( 'woocommerce_breadcrumb' ) ) {
			if ( $this->szp_maybe_placeholder( esc_html__( 'Product Breadcrumbs — requires WooCommerce.', 'storzen' ) ) ) {
				return;
			}
			return;
		}

		$s          = $this->get_settings_for_display();
		$delimiter  = isset( $s['delimiter'] ) && '' !== $s['delimiter']
			? $s['delimiter'] : '/';
		$show_home  = 'yes' === ( $s['show_home'] ?? 'yes' );
		$home_label = isset( $s['home_label'] ) && '' !== $s['home_label']
			? $s['home_label']
			: esc_html__( 'Home', 'storzen' );

		// Elementor editor placeholder when no product context.
		$is_editor = $this->szp_is_editor_or_preview();
		$product   = $this->szp_get_loop_product();

		if ( ! $product && $is_editor ) {
			// Build the placeholder breadcrumb string entirely before output
			// so each part is escaped at construction — avoids unescaped-variable
			// warnings from PHPCS's printf() argument scanner.
			$sep        = esc_html( ' ' . trim( $delimiter ) . ' ' );
			$aria       = esc_attr__( 'Breadcrumb', 'storzen' );
			$home_text  = esc_html( $home_label );
			$shop_text  = esc_html__( 'Shop', 'storzen' );
			$prod_text  = esc_html__( 'Product Name', 'storzen' );
			$html       = '<nav class="sz-breadcrumbs-wrap woocommerce-breadcrumb" aria-label="'
				. $aria . '">' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $aria escaped via esc_attr__() above
				. '<a href="#">' . $home_text . '</a>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $home_text escaped via esc_html() above
				. $sep  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $sep escaped via esc_html() above
				. '<a href="#">' . $shop_text . '</a>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $shop_text escaped via esc_html__() above
				. $sep  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $sep escaped via esc_html() above
				. $prod_text // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $prod_text escaped via esc_html__() above
				. '</nav>';
			echo wp_kses(
				$html,
				array(
					'nav' => array( 'class' => true, 'aria-label' => true ),
					'a'   => array( 'href'  => true ),
				)
			);
			return;
		}

		// Build WooCommerce breadcrumb args.
		$args = array(
			'delimiter'   => ' ' . esc_html( trim( $delimiter ) ) . ' ',
			'wrap_before' => '<nav class="sz-breadcrumbs-wrap woocommerce-breadcrumb" aria-label="' . esc_attr__( 'Breadcrumb', 'storzen' ) . '">',
			'wrap_after'  => '</nav>',
			'before'      => '',
			'after'       => '',
			'home'        => $show_home ? esc_html( $home_label ) : false,
		);

		/**
		 * Allow third-party plugins and themes to further modify the args
		 * before Storzen passes them to woocommerce_breadcrumb().
		 *
		 * @since 1.1.3
		 * @param array $args Breadcrumb arguments.
		 */
		$args = apply_filters( 'storzen_breadcrumb_args', $args );

		woocommerce_breadcrumb( $args );
	}

	/**
	 * True in Elementor editor or preview iframe.
	 *
	 * @return bool
	 */
	private function szp_is_editor_or_preview(): bool {
		$pl = \Elementor\Plugin::$instance;
		if ( ! empty( $pl->editor )
			&& method_exists( $pl->editor, 'is_edit_mode' )
			&& $pl->editor->is_edit_mode() ) {
			return true;
		}
		if ( ! empty( $pl->preview )
			&& method_exists( $pl->preview, 'is_preview_mode' )
			&& $pl->preview->is_preview_mode() ) {
			return true;
		}
		return false;
	}
}
