<?php
/**
 * Product Tags Widget — render functions
 * All functions are standalone, prefixed ggmc_product_tags_.
 * The $widget parameter replaces $this throughout.
 */

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

/**
 * Main render entry point.
 *
 * @param \GGM_Commerce_Base_Widget $widget The widget instance.
 */
function ggmc_product_tags_render( $widget ) {
    $settings = $widget->get_settings_for_display();
    $product_data = $widget->get_product_data();

    if (!$product_data) {
        if (ggmc_is_editor_mode()) {
            echo '<div class="ggmc-product-tags"><span class="ggmc-tags-label">Tags:</span> <span class="ggmc-tag-link">Sample Tag</span></div>';
        }
        return;
    }

    // Get tags for this product
    $taxonomy = GGM_Commerce_Taxonomy::get_instance();
    $tags = $taxonomy->get_product_tags($product_data->id);

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

    $layout    = $settings['display_layout'];
    $show_label = $settings['show_label'] === 'yes';
    $label_text = $settings['label_text'];
    $separator  = $settings['separator'];
    $show_icon  = $settings['show_icon'] === 'yes' && $layout === 'badges';

    echo '<div class="ggmc-product-tags ggmc-tags-' . esc_attr($layout) . '">';

    if ($show_label && !empty($label_text)) {
        echo '<span class="ggmc-tags-label">' . esc_html($label_text) . '</span>';
    }

    if ($layout === 'badges') {
        echo '<span class="ggmc-tags-badges">';
    } elseif ($layout === 'list') {
        echo '<ul class="ggmc-tags-list">';
    }

    $tag_links = [];
    foreach ($tags as $tag) {
        $tag_links[] = ggmc_product_tags_build_tag_link( $tag, $layout, $show_icon );
    }

    if ($layout === 'inline') {
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $tag_links contains pre-escaped HTML link elements
        echo implode(esc_html($separator), $tag_links);
    } else {
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $tag_links contains pre-escaped HTML link elements
        echo implode('', $tag_links);
    }

    if ($layout === 'badges') {
        echo '</span>';
    } elseif ($layout === 'list') {
        echo '</ul>';
    }

    echo '</div>';
}

/**
 * Build the HTML for a single tag link.
 *
 * @param object $tag       The tag object.
 * @param string $layout    The current display layout ('inline', 'list', 'badges').
 * @param bool   $show_icon Whether to render the icon span.
 * @return string           Pre-escaped HTML for the tag link.
 */
function ggmc_product_tags_build_tag_link( $tag, $layout, $show_icon ) {
    $url = GGM_Commerce_Helpers::get_tag_url($tag);

    $link_html = '';

    if ($layout === 'list') {
        $link_html .= '<li class="ggmc-tag-item">';
    }

    $link_html .= '<a href="' . esc_url($url) . '" class="ggmc-tag-link">';

    if ($show_icon) {
        $link_html .= '<span class="ggmc-tag-icon">#</span>';
    }

    $link_html .= esc_html($tag->name);
    $link_html .= '</a>';

    if ($layout === 'list') {
        $link_html .= '</li>';
    }

    return $link_html;
}
