<?php

namespace Webnestors\Wsn\Snippets;

class SingleChangeRelatedProductsHeading extends \Webnestors\Wsn\Snippet {
    public $heading_text = "";
    public $subheading_text = "";

    public function tab(): string {
        return "product";
    }

    public function title(): string {
        return esc_html__("Change Related Products Heading", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__("Change the related products heading and also create a subheading.", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Heading", "enhancements-for-woocommerce"),
                "name" => "heading_text",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Subheading", "enhancements-for-woocommerce"),
                "name" => "subheading_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        
        <h3>Description</h3>
        <p>The Change Related Products Heading module allows you to customize the heading text of the "Related Products" section on WooCommerce product pages. You can replace the default "Related Products" text with a more engaging or brand-specific title, such as "You May Also Like" or "Recommended for You." Additionally, this module lets you add an optional subheading, providing space for a brief message or description that can further entice customers to explore similar items.</p>
        <p>This customization helps create a more cohesive and branded shopping experience, making the related products section more relevant and appealing to customers.</p>
        <h3>Settings</h3>
        <p>Write the heading text and the subheading text you want to display.</p>
        <h3>Appearance</h3>
        <p>There are no customization options for this module.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_filter("woocommerce_product_related_products_heading", [$this, "wn_rename_related_products"]);
        add_filter("woocommerce_product_loop_start", [$this, "wn_related_products_subheading"]);
    }

    function wn_rename_related_products() {
        return $this->heading_text;
    }

    function wn_related_products_subheading($html) {
        if (wc_get_loop_prop("name", "related")) {
            $html = "<p>" . esc_html($this->subheading_text) . "</p>" . $html;
        }

        return $html;
    }
}
