<?php
namespace Calio\Widgets;

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

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

/**
 * Builds editorial heading blocks with layered title parts, eyebrow styles and optional links.
 */
class AdvancedHeadingWidget extends BaseWidget {
    public function get_name(): string { return 'calio-advanced-heading'; }
    public function get_title(): string { return esc_html__('Advanced Heading', 'calio-addon'); }
    public function get_icon(): string { return 'eicon-t-letter'; }
    public function get_style_depends(): array { return $this->resolve_widget_style_depends(); }

    /**
     * Registers content hierarchy and style controls for the Advanced Heading widget.
     */
    protected function register_controls(): void {
        $this->start_controls_section('content', ['label' => esc_html__('Content', 'calio-addon')]);
        $this->add_control('subtitle', ['label' => esc_html__('Eyebrow', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Calio Subtitle']);
        $this->add_control('eyebrow_skin', ['label' => esc_html__('Eyebrow Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'line', 'options' => ['plain' => esc_html__('Plain', 'calio-addon'), 'line' => esc_html__('Line', 'calio-addon'), 'badge' => esc_html__('Badge', 'calio-addon')]]);
        $this->add_control('content_width', ['label' => esc_html__('Content Width', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'normal', 'options' => ['narrow' => esc_html__('Narrow', 'calio-addon'), 'normal' => esc_html__('Normal', 'calio-addon'), 'wide' => esc_html__('Wide', 'calio-addon')]]);
        $this->add_control('panel_skin', ['label' => esc_html__('Panel Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'soft' => esc_html__('Soft', 'calio-addon'), 'outline' => esc_html__('Outline', 'calio-addon')]]);
        $this->add_control('show_accent_bar', ['label' => esc_html__('Show Accent Bar', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes']);
        $this->add_control('layout_mode', ['label' => esc_html__('Layout Mode', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'stacked', 'options' => ['stacked' => esc_html__('Stacked', 'calio-addon'), 'split' => esc_html__('Split Section', 'calio-addon')]]);
        $this->add_control('support_layout', ['label' => esc_html__('Support Layout', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'stacked', 'options' => ['stacked' => esc_html__('Stacked', 'calio-addon'), 'inline' => esc_html__('Inline', 'calio-addon')]]);
        $this->add_control('title_prefix', ['label' => esc_html__('Title Prefix', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('show_divider', ['label' => esc_html__('Show Divider', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes']);
        $this->add_control('title', ['label' => esc_html__('Title', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Your Advanced Heading']);
        $this->add_control('highlight_text', ['label' => esc_html__('Highlight Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('title_suffix', ['label' => esc_html__('Title Suffix', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('description', ['label' => esc_html__('Description', 'calio-addon'), 'type' => Controls_Manager::TEXTAREA, 'default' => '']);
        $this->add_control('background_title', ['label' => esc_html__('Background Title', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('meta_note', ['label' => esc_html__('Meta Note', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('support_badge', ['label' => esc_html__('Support Badge', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('support_link_text', ['label' => esc_html__('Support Link Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('support_link', ['label' => esc_html__('Support Link', 'calio-addon'), 'type' => Controls_Manager::URL, 'default' => ['url' => '']]);
        $this->add_control('link', ['label' => esc_html__('Link', 'calio-addon'), 'type' => Controls_Manager::URL, 'default' => ['url' => '']]);
        $this->add_control('tag', ['label' => esc_html__('HTML Tag', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'h2', 'options' => ['h1' => 'H1', 'h2' => 'H2', 'h3' => 'H3', 'h4' => 'H4', 'div' => 'DIV', 'p' => 'P']]);
        $this->add_control('align', ['label' => esc_html__('Alignment', 'calio-addon'), 'type' => Controls_Manager::CHOOSE, 'default' => 'left', 'options' => ['left' => ['title' => esc_html__('Left', 'calio-addon'), 'icon' => 'eicon-text-align-left'], 'center' => ['title' => esc_html__('Center', 'calio-addon'), 'icon' => 'eicon-text-align-center'], 'right' => ['title' => esc_html__('Right', 'calio-addon'), 'icon' => 'eicon-text-align-right']], 'selectors' => ['{{WRAPPER}} .calio-advanced-heading' => 'text-align: {{VALUE}};']]);
        $this->end_controls_section();

        $this->start_controls_section('style_subtitle', ['label' => esc_html__('Eyebrow', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'subtitle_typography', 'selector' => '{{WRAPPER}} .calio-advanced-heading-subtitle']);
        $this->add_style_heading('heading_subtitle_surface_heading', esc_html__('Surface', 'calio-addon'));
        $this->add_text_paint_controls('heading_subtitle', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-subtitle');
        $this->add_background_type_control('heading_subtitle_bg_group', '{{WRAPPER}} .calio-advanced-heading.eyebrow-skin-badge .calio-advanced-heading-subtitle');
        $this->add_style_heading('heading_subtitle_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'heading_subtitle_border', 'selector' => '{{WRAPPER}} .calio-advanced-heading.eyebrow-skin-badge .calio-advanced-heading-subtitle']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'heading_subtitle_shadow', 'selector' => '{{WRAPPER}} .calio-advanced-heading.eyebrow-skin-badge .calio-advanced-heading-subtitle']);
        $this->add_style_heading('heading_subtitle_spacing_heading', esc_html__('Spacing', 'calio-addon'));
        $this->add_dimensions_control('subtitle_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-subtitle', 'margin');
        $this->end_controls_section();

        $this->start_controls_section('style_panel', ['label' => esc_html__('Panel', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('heading_panel_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('heading_panel_state_tabs');
        $this->start_controls_tab('heading_panel_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_background_type_control('heading_panel_normal_group', '{{WRAPPER}} .calio-advanced-heading');
        $this->add_border_paint_controls('heading_panel_border_normal', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading');
        $this->end_controls_tab();
        $this->start_controls_tab('heading_panel_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_background_type_control('heading_panel_hover_group', '{{WRAPPER}} .calio-advanced-heading:hover');
        $this->add_border_paint_controls('heading_panel_border_hover', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover');
        $this->end_controls_tab();
        $this->start_controls_tab('heading_panel_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_background_type_control('heading_panel_active_group', '{{WRAPPER}} .calio-advanced-heading:focus-within');
        $this->add_border_paint_controls('heading_panel_border_active', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('heading_panel_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'heading_panel_border_group', 'selector' => '{{WRAPPER}} .calio-advanced-heading']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'heading_panel_shadow', 'selector' => '{{WRAPPER}} .calio-advanced-heading']);
        $this->add_style_heading('heading_panel_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('panel_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_dimensions_control('panel_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading', 'padding');
        $this->add_dimensions_control('panel_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading', 'margin');
        $this->end_controls_section();

        $this->start_controls_section('style_title', ['label' => esc_html__('Title', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'title_typography', 'selector' => '{{WRAPPER}} .calio-advanced-heading-title']);
        $this->add_style_heading('heading_title_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('heading_title_state_tabs');
        $this->start_controls_tab('heading_title_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('heading_title_normal', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-title');
        $this->add_text_paint_controls('heading_highlight_normal', esc_html__('Highlight Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-highlight');
        $this->add_background_paint_controls('heading_accent_normal', esc_html__('Accent Bar Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-bar', ['condition' => ['show_accent_bar' => 'yes']]);
        $this->end_controls_tab();
        $this->start_controls_tab('heading_title_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('heading_title_hover', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-title');
        $this->add_text_paint_controls('heading_highlight_hover', esc_html__('Highlight Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-highlight');
        $this->add_background_paint_controls('heading_accent_hover', esc_html__('Accent Bar Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-bar', ['condition' => ['show_accent_bar' => 'yes']]);
        $this->end_controls_tab();
        $this->start_controls_tab('heading_title_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('heading_title_active', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-title');
        $this->add_text_paint_controls('heading_highlight_active', esc_html__('Highlight Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-highlight');
        $this->add_background_paint_controls('heading_accent_active', esc_html__('Accent Bar Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-bar', ['condition' => ['show_accent_bar' => 'yes']]);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('heading_title_spacing_heading', esc_html__('Spacing', 'calio-addon'));
        $this->add_dimensions_control('title_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-title', 'margin');
        $this->end_controls_section();

        $this->start_controls_section('style_prefix', ['label' => esc_html__('Prefix / Suffix', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'prefix_typography', 'selector' => '{{WRAPPER}} .calio-advanced-heading-prefix, {{WRAPPER}} .calio-advanced-heading-suffix']);
        $this->add_text_paint_controls('heading_prefix', esc_html__('Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-heading-prefix', '{{WRAPPER}} .calio-advanced-heading-suffix']);
        $this->end_controls_section();
        $this->start_controls_section('style_background_title', ['label' => esc_html__('Background Title', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['background_title!' => '']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'bg_title_typography', 'selector' => '{{WRAPPER}} .calio-advanced-heading-bg']);
        $this->add_text_paint_controls('heading_bg_title', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-bg');
        $this->add_control('bg_title_opacity', ['label' => esc_html__('Opacity', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0.05, 'max' => 1, 'step' => 0.05]], 'selectors' => ['{{WRAPPER}} .calio-advanced-heading-bg' => 'opacity: {{SIZE}};']]);
        $this->add_control('bg_title_offset_y', ['label' => esc_html__('Offset Y', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => -120, 'max' => 120]], 'selectors' => ['{{WRAPPER}} .calio-advanced-heading-bg' => 'transform: translateY({{SIZE}}{{UNIT}});']]);
        $this->end_controls_section();
        $this->start_controls_section('style_description', ['label' => esc_html__('Description', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'desc_typography', 'selector' => '{{WRAPPER}} .calio-advanced-heading-desc']);
        $this->add_text_paint_controls('heading_desc', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-desc');
        $this->end_controls_section();
        $this->start_controls_section('style_meta_note', ['label' => esc_html__('Meta Note', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['meta_note!' => '']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'meta_note_typography', 'selector' => '{{WRAPPER}} .calio-advanced-heading-meta-note']);
        $this->add_style_heading('heading_support_surface_heading', esc_html__('Support States', 'calio-addon'));
        $this->start_controls_tabs('heading_support_state_tabs');
        $this->start_controls_tab('heading_support_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('heading_meta_note_normal', esc_html__('Meta Note Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-meta-note');
        $this->add_text_paint_controls('heading_support_badge_text_normal', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-support-badge');
        $this->add_background_type_control('heading_support_badge_bg_normal_group', '{{WRAPPER}} .calio-advanced-heading-support-badge');
        $this->add_text_paint_controls('heading_support_link_normal', esc_html__('Support Link Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-support-link');
        $this->add_background_paint_controls('heading_divider_normal', esc_html__('Divider Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading-divider');
        $this->end_controls_tab();
        $this->start_controls_tab('heading_support_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('heading_meta_note_hover', esc_html__('Meta Note Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-meta-note');
        $this->add_text_paint_controls('heading_support_badge_text_hover', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-support-badge');
        $this->add_background_type_control('heading_support_badge_bg_hover_group', '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-support-badge');
        $this->add_text_paint_controls('heading_support_link_hover', esc_html__('Support Link Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-support-link');
        $this->add_background_paint_controls('heading_divider_hover', esc_html__('Divider Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:hover .calio-advanced-heading-divider');
        $this->end_controls_tab();
        $this->start_controls_tab('heading_support_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('heading_meta_note_active', esc_html__('Meta Note Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-meta-note');
        $this->add_text_paint_controls('heading_support_badge_text_active', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-support-badge');
        $this->add_background_type_control('heading_support_badge_bg_active_group', '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-support-badge');
        $this->add_text_paint_controls('heading_support_link_active', esc_html__('Support Link Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-support-link');
        $this->add_background_paint_controls('heading_divider_active', esc_html__('Divider Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-heading:focus-within .calio-advanced-heading-divider');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('heading_support_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'heading_support_badge_border', 'selector' => '{{WRAPPER}} .calio-advanced-heading-support-badge']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'heading_support_badge_shadow', 'selector' => '{{WRAPPER}} .calio-advanced-heading-support-badge']);
        $this->end_controls_section();
    }

    /**
     * Outputs escaped heading markup while allowing a limited highlighted span inside the title.
     */
    protected function render(): void {
        $s = $this->get_settings_for_display();
        $tag = (string) ($s['tag'] ?? 'h2');
        if (!in_array($tag, ['h1', 'h2', 'h3', 'h4', 'div', 'p'], true)) { $tag = 'h2'; }
        $title = (string) ($s['title'] ?? '');
        $highlight = (string) ($s['highlight_text'] ?? '');
        $rendered_title = esc_html($title);
        if ($highlight !== '' && stripos($title, $highlight) !== false) {
            $pattern = '/' . preg_quote($highlight, '/') . '/i';
            $rendered_title = preg_replace($pattern, '<span class="calio-advanced-heading-highlight">$0</span>', esc_html($title), 1) ?: esc_html($title);
        }
        $title_html = '';
        if (!empty($s['title_prefix'])) { $title_html .= '<span class="calio-advanced-heading-prefix">' . esc_html((string) $s['title_prefix']) . '</span> '; }
        $title_html .= $rendered_title;
        if (!empty($s['title_suffix'])) { $title_html .= ' <span class="calio-advanced-heading-suffix">' . esc_html((string) $s['title_suffix']) . '</span>'; }
        if (!empty($s['link']['url'])) {
            $rel = [];
            if (!empty($s['link']['nofollow'])) {
                $rel[] = 'nofollow';
            }
            if (!empty($s['link']['is_external'])) {
                $rel[] = 'noopener';
                $rel[] = 'noreferrer';
            }
            $linked_title = '<a class="calio-advanced-heading-link" href="' . esc_url((string) ($s['link']['url'])) . '"';
            if (!empty($s['link']['is_external'])) {
                $linked_title .= ' target="' . esc_attr('_blank') . '"';
            }
            if (!empty($rel)) {
                $linked_title .= ' rel="' . esc_attr(implode(' ', array_unique($rel))) . '"';
            }
            $title_html = $linked_title . '>' . $title_html . '</a>';
        }
        $classes = ['calio-advanced-heading', 'eyebrow-skin-' . sanitize_html_class((string) ($s['eyebrow_skin'] ?? 'line')), 'width-' . sanitize_html_class((string) ($s['content_width'] ?? 'normal')), 'panel-' . sanitize_html_class((string) ($s['panel_skin'] ?? 'none')), 'layout-' . sanitize_html_class((string) ($s['layout_mode'] ?? 'stacked')), 'support-layout-' . sanitize_html_class((string) ($s['support_layout'] ?? 'stacked'))];
        echo '<div class="' . esc_attr(implode(' ', $classes)) . '">';
        if (!empty($s['background_title'])) {
            echo '<div class="calio-advanced-heading-bg" aria-hidden="true">' . esc_html((string) $s['background_title']) . '</div>';
        }
        echo '<div class="calio-advanced-heading-main">';
        echo '<div class="calio-advanced-heading-copy">';
        if (!empty($s['subtitle'])) {
            echo '<span class="calio-advanced-heading-subtitle">' . esc_html((string) $s['subtitle']) . '</span>';
        }
        echo '<' . tag_escape($tag) . ' class="calio-advanced-heading-title">' . wp_kses_post($title_html) . '</' . tag_escape($tag) . '>';
        if (($s['show_divider'] ?? '') === 'yes') {
            echo '<span class="calio-advanced-heading-divider" aria-hidden="true"></span>';
        }
        echo '</div>';
        if (!empty($s['description']) || !empty($s['meta_note']) || !empty($s['support_badge']) || (!empty($s['support_link_text']) && !empty($s['support_link']['url']))) {
            echo '<div class="calio-advanced-heading-support">';
            if (!empty($s['support_badge']) || !empty($s['meta_note'])) {
                echo '<div class="calio-advanced-heading-support-top">';
                if (!empty($s['support_badge'])) {
                    echo '<span class="calio-advanced-heading-support-badge">' . esc_html((string) $s['support_badge']) . '</span>';
                }
                if (!empty($s['meta_note'])) {
                    echo '<div class="calio-advanced-heading-meta-note">' . esc_html((string) $s['meta_note']) . '</div>';
                }
                echo '</div>';
            }
            if (!empty($s['description'])) {
                echo '<div class="calio-advanced-heading-desc">' . esc_html((string) $s['description']) . '</div>';
            }
            if (!empty($s['support_link_text']) && !empty($s['support_link']['url'])) {
                $support_rel = [];
                if (!empty($s['support_link']['nofollow'])) { $support_rel[] = 'nofollow'; }
                if (!empty($s['support_link']['is_external'])) { $support_rel[] = 'noopener'; $support_rel[] = 'noreferrer'; }
                echo '<a class="calio-advanced-heading-support-link" href="' . esc_url((string) $s['support_link']['url']) . '"';
                if (!empty($s['support_link']['is_external'])) {
                    echo ' target="' . esc_attr('_blank') . '"';
                }
                if (!empty($support_rel)) {
                    echo ' rel="' . esc_attr(implode(' ', array_unique($support_rel))) . '"';
                }
                echo '>' . esc_html((string) $s['support_link_text']) . '</a>';
            }
            echo '</div>';
        }
        echo '</div>';
        echo '</div>';
    }
}








