<?php

namespace Webnestors\Wsn\Snippets;

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

    public function title(): string {
        return esc_html__("Remove Shipping Form on Local Pickup", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Remove the ship to a different address section when local pick up is selected.",
            "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 Remove Shipping Form on Local Pickup module enhances the checkout experience by automatically removing the "Ship to a different address" section when customers choose local pickup as their shipping option. This feature streamlines the checkout process by eliminating unnecessary fields when they are not required, improving usability and reducing customer confusion.</p>
        <p>This module is particularly beneficial for stores offering local pickup as an option, where customers don't need to provide a shipping address. By hiding the irrelevant shipping form, you simplify the checkout process and create a more efficient shopping experience.</p>
        <h3>Settings</h3>
        <p>Just enable it and you are good to go</p>
        <h3>Appearance</h3>
        <p>There are no appearence options for this module</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action("woocommerce_after_checkout_form", [$this, "wn_remove_ship_to_section"]);
    }

    function wn_remove_ship_to_section() {
        $chosen_methods = WC()->session->get("chosen_shipping_methods");
        $chosen_shipping = $chosen_methods[0];

        if (0 === strpos($chosen_shipping, "local_pickup")) {
            wp_enqueue_script(
                "wsn_checkout_remove_address_on_local_pickup_1",
                \Webnestors\Wsn\Plugin::instance()->get_url(
                    "assets/snippets/scripts/RemoveShippingAddressOnLocalPickup1.js"
                )
            );
        }

        wp_enqueue_script(
            "wsn_checkout_remove_address_on_local_pickup_2",
            \Webnestors\Wsn\Plugin::instance()->get_url(
                "assets/snippets/scripts/RemoveShippingAddressOnLocalPickup2.js"
            )
        );
    }
}
