<?php

namespace Webnestors\Wsn\Snippets;

class GeneralHideShippingWhenFree extends \Webnestors\Wsn\Snippet {
    public function tab(): string {
        return "general";
    }

    public function title(): string {
        return esc_html__(
            "Disable all Shipping Methods when Free Shipping is Enabled and Keep Local Pickup",
            "enhancements-for-woocommerce"
        );
    }

    public function description(): string {
        return esc_html__(
            "Disable all shipping rates except Local pickup when free shipping is available. Used at the cart and checkout pages.",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Disable All Shipping Methods When Free Shipping is Enabled and Keep Local Pickup module offers a convenient solution for WooCommerce stores that want to streamline the checkout process by simplifying shipping options when free shipping is available. When this feature is enabled, all shipping methods (such as flat rate, express, or international shipping) will automatically be disabled at the cart and checkout pages if free shipping is an available option. However, the Local Pickup option remains active for customers who prefer to pick up their orders in person.</p>
        <p>This module is especially useful for stores that offer free shipping under certain conditions (e.g., on orders above a certain amount) and want to ensure that customers are incentivized to choose the most cost-effective shipping method—either free shipping or local pickup. By removing unnecessary shipping options, the module reduces customer confusion and ensures a smooth, simplified checkout experience. It’s also ideal for businesses that operate physical stores or warehouses where customers can collect their orders directly.</p>
        <h3>Settings</h3>
        <p>There are no setting for this module</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_package_rates", [$this, "wn__hide_if_free"], 100);
    }

    function wn__hide_if_free($rates) {
        $free = $local = [];
        foreach ($rates as $rate_id => $rate) {
            if ("free_shipping" === $rate->method_id) {
                $free[$rate_id] = $rate;
            } elseif ("local_pickup" === $rate->method_id) {
                $local[$rate_id] = $rate;
            }
        }
        return !empty($free) ? array_merge($free, $local) : $rates;
    }
}
