<?php
/**
 * Widget Name: Site Logo
 * Description: Displays the WordPress site logo with full 2026 customization.
 *              Supports image, SVG inline, dark-mode logo, hover effects,
 *              sticky scroll logo, retina, lazy-load, SEO attributes.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Css_Filter;

/**
 * Class Captwiki_Site_Logo
 *
 * @since 1.0.0
 */
class Captwiki_Site_Logo extends Widget_Base {

	/**
	 * Get Widget Name.
	 *
	 * @since 1.0.0
	 */
	public function get_name(): string {
		return 'captwiki-site-logo';
	}

	/**
	 * Get Widget Title.
	 *
	 * @since 1.0.0
	 */
	public function get_title(): string {
		return esc_html__( 'Site Logo', 'captain-widgets-kit' );
	}

	/**
	 * Get Widget Icon.
	 *
	 * @since 1.0.0
	 */
	public function get_icon(): string {
		return 'eicon-site-logo captwiki-el-logo';
	}

	/**
	 * Get Widget Categories.
	 *
	 * @since 1.0.0
	 */
	public function get_categories(): array {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get Widget Keywords.
	 *
	 * @since 1.0.0
	 */
	public function get_keywords(): array {
		return array( 'site', 'logo', 'branding', 'svg', 'image', 'dark mode', 'sticky', 'retina' );
	}

	/**
	 * Get Widget Styles.
	 *
	 * @since 1.0.0
	 */
	public function get_style_depends(): array {
		return array( 'captwiki-site-logo' );
	}

	/**
	 * Get Widget Scripts.
	 *
	 * @since 1.0.0
	 */
	public function get_script_depends(): array {
		return array( 'captwiki-site-logo' );
	}

	/**
	 * Check if the widget has an inner wrapper.
	 *
	 * @since 1.0.0
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Register widget controls.
	 *
	 * @since 1.0.0
	 */
	protected function register_controls(): void {

		$this->start_controls_section(
			'section_logo',
			array( 'label' => esc_html__( 'Logo Source', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'logo_source',
			array(
				'label'   => esc_html__( 'Logo Source', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'site',
				'options' => array(
					'site'   => esc_html__( 'WordPress Site Logo', 'captain-widgets-kit' ),
					'custom' => esc_html__( 'Custom Image / SVG Upload', 'captain-widgets-kit' ),
					'url'    => esc_html__( 'External Image URL', 'captain-widgets-kit' ),
					'text'   => esc_html__( 'Site Name (Text Logo)', 'captain-widgets-kit' ),
				),
			)
		);

		/* Custom Media Upload */
		$this->add_control(
			'custom_logo',
			array(
				'label'      => esc_html__( 'Upload Logo (Image / SVG)', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::MEDIA,
				'media_type' => array( 'image', 'svg+xml' ),
				'dynamic'    => array( 'active' => true ),
				'condition'  => array( 'logo_source' => 'custom' ),
			)
		);

		/* SVG Inline Option */
		$this->add_control(
			'svg_inline',
			array(
				'label'       => esc_html__( 'Render SVG Inline', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => '',
				'description' => esc_html__( 'Inlines SVG code for CSS color/animation control. Only works for SVG files.', 'captain-widgets-kit' ),
				'condition'   => array( 'logo_source' => 'custom' ),
			)
		);

		/* External URL */
		$this->add_control(
			'logo_url',
			array(
				'label'       => esc_html__( 'External Image URL', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => 'https://example.com/logo.png',
				'dynamic'     => array( 'active' => true ),
				'condition'   => array( 'logo_source' => 'url' ),
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'logo_size',
				'default'   => 'full',
				'condition' => array( 'logo_source' => array( 'site', 'custom' ) ),
			)
		);

		$this->end_controls_section();

		/* ── SECTION: Dark Mode / Sticky Logo ─────────────── */
		$this->start_controls_section(
			'section_dark_sticky',
			array( 'label' => esc_html__( 'Dark Mode & Sticky Logo', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'dark_logo_info',
			array(
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => '<small style="color:#888">Upload an alternate logo shown when <code>prefers-color-scheme: dark</code> is active <strong>or</strong> when a parent has <code>.dark-mode</code> class.</small>',
				'content_classes' => 'elementor-descriptor',
			)
		);

		$this->add_control(
			'dark_logo',
			array(
				'label'      => esc_html__( 'Dark Mode Logo', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::MEDIA,
				'media_type' => array( 'image', 'svg+xml' ),
				'dynamic'    => array( 'active' => true ),
			)
		);

		$this->add_control(
			'sticky_logo',
			array(
				'label'       => esc_html__( 'Sticky Scroll Logo', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::MEDIA,
				'media_type'  => array( 'image', 'svg+xml' ),
				'dynamic'     => array( 'active' => true ),
				'description' => esc_html__( 'Shown when the page is scrolled (requires sticky header). Uses JS IntersectionObserver — zero jQuery dependency.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();

		/* ── SECTION: Link & SEO ──────────────────────────── */
		$this->start_controls_section(
			'section_link_seo',
			array( 'label' => esc_html__( 'Link & SEO', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'link_to',
			array(
				'label'   => esc_html__( 'Link To', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'home',
				'options' => array(
					'home'   => esc_html__( 'Homepage', 'captain-widgets-kit' ),
					'custom' => esc_html__( 'Custom URL', 'captain-widgets-kit' ),
					'none'   => esc_html__( 'No Link', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'custom_link',
			array(
				'label'     => esc_html__( 'Custom URL', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::URL,
				'dynamic'   => array( 'active' => true ),
				'condition' => array( 'link_to' => 'custom' ),
			)
		);

		$this->add_control(
			'link_target',
			array(
				'label'     => esc_html__( 'Open In', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '_self',
				'options'   => array(
					'_self'  => esc_html__( 'Same Tab', 'captain-widgets-kit' ),
					'_blank' => esc_html__( 'New Tab', 'captain-widgets-kit' ),
				),
				'condition' => array( 'link_to!' => 'none' ),
			)
		);

		$this->add_control(
			'link_rel',
			array(
				'label'     => esc_html__( 'Link Rel', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '',
				'options'   => array(
					''                    => esc_html__( 'None', 'captain-widgets-kit' ),
					'nofollow'            => 'nofollow',
					'noopener noreferrer' => 'noopener noreferrer',
					'nofollow noopener'   => 'nofollow noopener',
					'sponsored'           => 'sponsored',
				),
				'condition' => array( 'link_to!' => 'none' ),
			)
		);

		$this->add_control(
			'custom_alt',
			array(
				'label'       => esc_html__( 'Custom Alt Text (SEO)', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Brand Name — Tagline', 'captain-widgets-kit' ),
				'description' => esc_html__( 'Leave empty to use WordPress site name automatically.', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'title_attr',
			array(
				'label'       => esc_html__( 'Image Title Attribute', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Optional tooltip on hover', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'use_figure',
			array(
				'label'       => esc_html__( 'Wrap in <figure> Tag', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => '',
				'description' => esc_html__( 'Semantic HTML5 — improves structured data parsing.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();

		/* ── SECTION: Performance ─────────────────────────── */
		$this->start_controls_section(
			'section_performance',
			array( 'label' => esc_html__( 'Performance & Loading', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'loading',
			array(
				'label'   => esc_html__( 'Loading Strategy', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'eager',
				'options' => array(
					'eager' => esc_html__( 'Eager (Above fold — default)', 'captain-widgets-kit' ),
					'lazy'  => esc_html__( 'Lazy (Below fold)', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'fetch_priority',
			array(
				'label'   => esc_html__( 'Fetch Priority', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'high',
				'options' => array(
					'high' => esc_html__( 'High (LCP — recommended for logos)', 'captain-widgets-kit' ),
					'low'  => esc_html__( 'Low', 'captain-widgets-kit' ),
					'auto' => esc_html__( 'Auto', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'decoding',
			array(
				'label'   => esc_html__( 'Image Decoding', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'async',
				'options' => array(
					'async' => 'async (non-blocking)',
					'sync'  => 'sync',
					'auto'  => 'auto',
				),
			)
		);

		$this->add_control(
			'retina_logo',
			array(
				'label'       => esc_html__( 'Retina / 2× Logo', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::MEDIA,
				'media_type'  => array( 'image' ),
				'dynamic'     => array( 'active' => true ),
				'description' => esc_html__( 'Optional 2× version served to HiDPI screens via srcset.', 'captain-widgets-kit' ),
				'condition'   => array( 'logo_source' => array( 'site', 'custom', 'url' ) ),
			)
		);

		$this->end_controls_section();

		/* ── SECTION: Style → Size & Spacing ─────────────── */
		$this->start_controls_section(
			'section_style_size',
			array(
				'label' => esc_html__( 'Size & Spacing', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'logo_width',
			array(
				'label'      => esc_html__( 'Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'vw', 'rem' ),
				'range'      => array(
					'px' => array(
						'min' => 20,
						'max' => 600,
					),
					'%'  => array(
						'min' => 5,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-site-logo img,
					 {{WRAPPER}} .captwiki-site-logo svg' => 'width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'logo_max_height',
			array(
				'label'      => esc_html__( 'Max Height', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'rem' ),
				'range'      => array(
					'px' => array(
						'min' => 10,
						'max' => 300,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-site-logo img,
					 {{WRAPPER}} .captwiki-site-logo svg' => 'max-height: {{SIZE}}{{UNIT}}; height: auto;',
				),
			)
		);

		$this->add_responsive_control(
			'logo_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',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo' => 'justify-content: {{VALUE}};',
				),
				'default'   => 'flex-start',
			)
		);

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

		$this->end_controls_section();

		/* ── SECTION: Style → Image Effects ──────────────── */
		$this->start_controls_section(
			'section_style_effects',
			array(
				'label' => esc_html__( 'Image Effects', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'opacity',
			array(
				'label'     => esc_html__( 'Opacity', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0.1,
						'max'  => 1,
						'step' => 0.05,
					),
				),
				'default'   => array( 'size' => 1 ),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo img,
					 {{WRAPPER}} .captwiki-site-logo svg' => 'opacity: {{SIZE}};',
				),
			)
		);

		$this->add_control(
			'mix_blend_mode',
			array(
				'label'     => esc_html__( 'Mix Blend Mode', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'normal',
				'options'   => array(
					'normal'      => 'Normal',
					'multiply'    => 'Multiply',
					'screen'      => 'Screen',
					'overlay'     => 'Overlay',
					'darken'      => 'Darken',
					'lighten'     => 'Lighten',
					'color-dodge' => 'Color Dodge',
					'luminosity'  => 'Luminosity',
				),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo img,
					 {{WRAPPER}} .captwiki-site-logo svg' => 'mix-blend-mode: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'filter_heading_normal',
			array(
				'label'     => esc_html__( 'CSS Filter — Normal', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'css_filter',
				'selector' => '{{WRAPPER}} .captwiki-site-logo img, {{WRAPPER}} .captwiki-site-logo svg',
			)
		);

		$this->add_control(
			'filter_heading_hover',
			array(
				'label'     => esc_html__( 'CSS Filter — Hover', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'css_filter_hover',
				'selector' => '{{WRAPPER}} .captwiki-site-logo:hover img, {{WRAPPER}} .captwiki-site-logo:hover svg',
			)
		);

		$this->end_controls_section();

		/* ── SECTION: Style → Hover Animation ────────────── */
		$this->start_controls_section(
			'section_style_hover',
			array(
				'label' => esc_html__( 'Hover Animation', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'hover_animation',
			array(
				'label'   => esc_html__( 'Animation Type', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'none',
				'options' => array(
					'none'      => esc_html__( 'None', 'captain-widgets-kit' ),
					'zoom-in'   => esc_html__( 'Zoom In', 'captain-widgets-kit' ),
					'zoom-out'  => esc_html__( 'Zoom Out', 'captain-widgets-kit' ),
					'lift'      => esc_html__( 'Lift Up (shadow)', 'captain-widgets-kit' ),
					'rotate-cw' => esc_html__( 'Slight Rotate CW', 'captain-widgets-kit' ),
					'pulse'     => esc_html__( 'Pulse', 'captain-widgets-kit' ),
					'shake'     => esc_html__( 'Shake', 'captain-widgets-kit' ),
					'flip-h'    => esc_html__( 'Flip Horizontal', 'captain-widgets-kit' ),
					'glow'      => esc_html__( 'Glow', 'captain-widgets-kit' ),
					'fade-dim'  => esc_html__( 'Fade Dim', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'hover_transition',
			array(
				'label'     => esc_html__( 'Transition Duration (ms)', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 100,
						'max'  => 1200,
						'step' => 50,
					),
				),
				'default'   => array( 'size' => 300 ),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo img,
					 {{WRAPPER}} .captwiki-site-logo svg,
					 {{WRAPPER}} .captwiki-site-logo a' => 'transition-duration: {{SIZE}}ms;',
				),
			)
		);

		$this->add_control(
			'glow_color',
			array(
				'label'     => esc_html__( 'Glow Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#0073aa',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo.cwk-anim-glow:hover img,
					 {{WRAPPER}} .captwiki-site-logo.cwk-anim-glow:hover svg' =>
						'filter: drop-shadow(0 0 10px {{VALUE}});',
				),
				'condition' => array( 'hover_animation' => 'glow' ),
			)
		);

		$this->end_controls_section();

		/* ── SECTION: Style → Border & Shadow ────────────── */
		$this->start_controls_section(
			'section_style_border',
			array(
				'label' => esc_html__( 'Border & Shadow', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'border',
				'selector' => '{{WRAPPER}} .captwiki-site-logo img, {{WRAPPER}} .captwiki-site-logo svg',
			)
		);

		$this->add_responsive_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-site-logo img,
					 {{WRAPPER}} .captwiki-site-logo svg' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'box_shadow',
				'selector' => '{{WRAPPER}} .captwiki-site-logo img, {{WRAPPER}} .captwiki-site-logo svg',
			)
		);

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

		$this->end_controls_section();

		/* ── SECTION: Style → Text Logo ───────────────────── */
		$this->start_controls_section(
			'section_style_text',
			array(
				'label'     => esc_html__( 'Text Logo Style', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'logo_source' => 'text' ),
			)
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'      => 'text_typography',
				'selector'  => '{{WRAPPER}} .captwiki-site-logo .cwk-text-logo',
				'condition' => array( 'logo_source' => 'text' ),
			)
		);

		$this->add_control(
			'text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo .cwk-text-logo' => 'color: {{VALUE}};',
				),
				'condition' => array( 'logo_source' => 'text' ),
			)
		);

		$this->add_control(
			'text_hover_color',
			array(
				'label'     => esc_html__( 'Text Hover Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo:hover .cwk-text-logo' => 'color: {{VALUE}};',
				),
				'condition' => array( 'logo_source' => 'text' ),
			)
		);

		$this->add_control(
			'text_gradient',
			array(
				'label'     => esc_html__( 'Gradient Text', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => '',
				'condition' => array( 'logo_source' => 'text' ),
			)
		);

		$this->add_control(
			'text_gradient_start',
			array(
				'label'     => esc_html__( 'Gradient Start Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#0073aa',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo .cwk-text-logo' =>
						'background: linear-gradient(90deg, {{VALUE}}, var(--cwk-grad-end, #00c6ff)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;',
				),
				'condition' => array(
					'logo_source'   => 'text',
					'text_gradient' => 'yes',
				),
			)
		);

		$this->add_control(
			'text_gradient_end',
			array(
				'label'     => esc_html__( 'Gradient End Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#00c6ff',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-site-logo .cwk-text-logo' => '--cwk-grad-end: {{VALUE}};',
				),
				'condition' => array(
					'logo_source'   => 'text',
					'text_gradient' => 'yes',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Safely inline an SVG file from the uploads directory.
	 *
	 * @param int $attachment_id attachment id.
	 * @return string|false SVG content or false if failed.
	 */
	private function get_inline_svg( int $attachment_id ) {
		$file = get_attached_file( $attachment_id );
		if ( ! $file || 'svg' !== strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ) ) {
			return false;
		}
		// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		$svg = file_get_contents( $file );
		if ( false === $svg ) {
			return false;
		}
		$svg = preg_replace( '/<\?xml[^>]+>/i', '', $svg );
		$svg = preg_replace( '/<!DOCTYPE[^>]+>/i', '', $svg );
		if ( false === strpos( $svg, 'aria-' ) ) {
			$svg = str_replace( '<svg', '<svg aria-hidden="true" focusable="false"', $svg );
		}
		return trim( $svg );
	}

	/**
	 * Build the hover animation CSS class string.
	 *
	 * @param string $animation CSS animation.
	 * @return string CSS class for animation.
	 */
	private function animation_class( string $animation ): string {
		if ( 'none' === $animation || '' === $animation ) {
			return '';
		}
		return ' cwk-anim-' . sanitize_html_class( $animation );
	}

	/**
	 * Build the link target / rel attributes string.
	 *
	 * @param array $settings Settings array.
	 * @return string Link target / rel attributes.
	 */
	private function link_attrs( array $settings ): string {
		$target = ! empty( $settings['link_target'] ) ? $settings['link_target'] : '_self';
		$rel    = ! empty( $settings['link_rel'] ) ? $settings['link_rel'] : '';
		$out    = ' target="' . esc_attr( $target ) . '"';
		if ( $rel ) {
			$out .= ' rel="' . esc_attr( $rel ) . '"';
		}
		return $out;
	}

	/**
	 * Render the widget HTML output.
	 *
	 * NOTE: The `content_template()` method has been intentionally removed.
	 * Elementor editor preview is handled exclusively via `render()`.
	 * This avoids duplicate JS template maintenance and preventseditor JavaScript errors.
	 */
	protected function render(): void {
		$s = $this->get_settings_for_display();

		$logo_src    = isset( $s['logo_source'] ) ? $s['logo_source'] : 'site';
		$img_url     = '';
		$img_id      = 0;
		$inline_svg  = false;
		$is_text     = ( 'text' === $logo_src );
		$svg_content = '';

		switch ( $logo_src ) {

			case 'custom':
				$img_id = ! empty( $s['custom_logo']['id'] ) ? (int) $s['custom_logo']['id'] : 0;

				if ( $img_id ) {
					$img_url = Group_Control_Image_Size::get_attachment_image_src(
						$img_id,     // attachment ID.
						'logo_size', // control name prefix.
						$s           // full settings array.
					);
				}

				// Fallback to URL if get_attachment_image_src returns empty.
				if ( empty( $img_url ) && ! empty( $s['custom_logo']['url'] ) ) {
					$img_url = $s['custom_logo']['url'];
				}

				// Inline SVG.
				if ( $img_id && ! empty( $s['svg_inline'] ) && 'yes' === $s['svg_inline'] ) {
					$svg_raw = $this->get_inline_svg( $img_id );
					if ( $svg_raw ) {
						$inline_svg  = true;
						$svg_content = $svg_raw;
					}
				}
				break;

			case 'url':
				$img_url = ! empty( $s['logo_url']['url'] ) ? $s['logo_url']['url'] : '';
				break;

			case 'site':
			default:
				if ( has_custom_logo() ) {
					$logo_id = (int) get_theme_mod( 'custom_logo' );
					if ( $logo_id ) {
						$img_id  = $logo_id;
						$img_url = Group_Control_Image_Size::get_attachment_image_src(
							$logo_id,
							'logo_size',
							$s
						);
						// Fallback: if size variant doesn't exist (e.g. SVG), use full.
						if ( empty( $img_url ) ) {
							$img_url = wp_get_attachment_image_url( $logo_id, 'full' );
						}
					}
				}
				break;
		}

		/* Bail early if nothing to display */
		if ( ! $is_text && ! $inline_svg && empty( $img_url ) ) {
			echo '<div style="padding:12px;color:#aaa;font-size:13px;border:1px dashed #ddd;border-radius:4px;">' . esc_html__( 'No logo found. Upload one or set a WordPress site logo.', 'captain-widgets-kit' ) . '</div>';
			return;
		}

		$alt = ! empty( $s['custom_alt'] ) ? $s['custom_alt'] : get_bloginfo( 'name' );

		$link_to  = isset( $s['link_to'] ) ? $s['link_to'] : 'home';
		$link_url = '';
		if ( 'home' === $link_to ) {
			$link_url = home_url( '/' );
		} elseif ( 'custom' === $link_to && ! empty( $s['custom_link']['url'] ) ) {
			$link_url = $s['custom_link']['url'];
		}
		$has_link   = '' !== $link_url;
		$link_attrs = $has_link ? $this->link_attrs( $s ) : '';
		$hover_cls  = $this->animation_class( isset( $s['hover_animation'] ) ? $s['hover_animation'] : 'none' );

		/* ── Dark / Sticky logo URLs ─────────────────────── */
		$dark_url   = ! empty( $s['dark_logo']['url'] ) ? $s['dark_logo']['url'] : '';
		$sticky_url = ! empty( $s['sticky_logo']['url'] ) ? $s['sticky_logo']['url'] : '';

		/* ── Retina srcset ───────────────────────────────── */
		$retina_url = ! empty( $s['retina_logo']['url'] ) ? $s['retina_logo']['url'] : '';

		$loading        = ! empty( $s['loading'] ) ? $s['loading'] : 'eager';
		$fetch_priority = ! empty( $s['fetch_priority'] ) ? $s['fetch_priority'] : 'high';
		$decoding       = ! empty( $s['decoding'] ) ? $s['decoding'] : 'async';

		$use_figure = ! empty( $s['use_figure'] ) && 'yes' === $s['use_figure'];
		$wrap_tag   = $use_figure ? 'figure' : 'div';

		$widget_uid = 'cwkl-' . $this->get_id();

		/* data-* attrs for JS dark/sticky switching */
		$data_attrs = ' id="' . esc_attr( $widget_uid ) . '"';
		if ( $dark_url ) {
			$data_attrs .= ' data-dark-logo="' . esc_attr( $dark_url ) . '"';
		}
		if ( $sticky_url ) {
			$data_attrs .= ' data-sticky-logo="' . esc_attr( $sticky_url ) . '"';
		}

		echo '<' . esc_attr( $wrap_tag ) . ' class="captwiki-site-logo' . esc_attr( $hover_cls ) . '"' . $data_attrs . '>'; // phpcs:ignore

		if ( $has_link ) {
			echo '<a href="' . esc_url( $link_url ) . '"' . $link_attrs; // phpcs:ignore
			if ( ! $is_text ) {
				echo ' aria-label="' . esc_attr( $alt ) . '"';
			}
			echo '>';
		}

		if ( $is_text ) {
			echo '<span class="cwk-text-logo">' . esc_html( get_bloginfo( 'name' ) ) . '</span>';

		} elseif ( $inline_svg ) {
			$allowed_svg = array(
				'svg'    => array( 'xmlns' => true, 'width' => true, 'height' => true, 'viewbox' => true, 'viewBox' => true, 'class' => true, 'aria-hidden' => true, 'focusable' => true, 'role' => true, 'fill' => true, 'stroke' => true ),
				'path'   => array( 'd' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'fill-rule' => true, 'clip-rule' => true ),
				'circle' => array( 'cx' => true, 'cy' => true, 'r' => true, 'fill' => true ),
				'rect'   => array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true, 'fill' => true ),
				'g'      => array( 'fill' => true, 'transform' => true ),
				'title'  => array(),
				'desc'   => array(),
			);
			echo wp_kses( $svg_content, $allowed_svg );

		} else {
			$title_attr  = ! empty( $s['title_attr'] ) ? ' title="' . esc_attr( $s['title_attr'] ) . '"' : '';
			$srcset_attr = '';

			if ( $retina_url ) {
				$srcset_attr = ' srcset="' . esc_attr( $img_url ) . ' 1x, ' . esc_attr( $retina_url ) . ' 2x"';
			} elseif ( $img_id ) {

				$size_name = ! empty( $s['logo_size_size'] ) ? $s['logo_size_size'] : 'full';
				$srcset    = wp_get_attachment_image_srcset( $img_id, $size_name );
				$sizes     = wp_get_attachment_image_sizes( $img_id, $size_name );
				if ( $srcset && $sizes ) {
					$srcset_attr = ' srcset="' . esc_attr( $srcset ) . '" sizes="' . esc_attr( $sizes ) . '"';
				}
			}

			echo '<img'
				. ' src="' . esc_url( $img_url ) . '"'
				. $srcset_attr // phpcs:ignore
				. ' alt="' . esc_attr( $alt ) . '"'
				. $title_attr // phpcs:ignore
				. ' loading="' . esc_attr( $loading ) . '"'
				. ' decoding="' . esc_attr( $decoding ) . '"'
				. ' fetchpriority="' . esc_attr( $fetch_priority ) . '"'
				. ' />';
		}

		if ( $has_link ) {
			echo '</a>';
		}

		echo '</' . esc_attr( $wrap_tag ) . '><!-- .captwiki-site-logo -->';
	}
}
