<?php
/**
 * Buttons class.
 *
 * @package SaveToWishlist\Classes\Frontend
 * @since 1.0.0
 */
namespace SaveToWishlist\Classes\Frontend;

use SaveToWishlist\Abstracts\Singleton;
use SaveToWishlist\Helpers\Helper_Functions;
use SaveToWishlist\Classes\Frontend\Wishlist;

/**
 * Class Buttons
 *
 * This class serves as the buttons class.
 */
class Buttons extends Singleton {

    /**
     * Helper functions.
     *
     * @var Helper_Functions
     */
    private $helper_functions;

    /**
     * Wishlist.
     *
     * @var Wishlist
     */
    private $wishlist;

    /**
     * Constructor.
     *
     * @since 1.0.0
     * @access public
     */
    public function __construct() {
        $this->helper_functions = Helper_Functions::instance();
        $this->wishlist         = Wishlist::instance();
    }

    /**
     * Display wishlist button on shop page.
     *
     * @since 1.0.0
     * @access public
     */
    public function display_wishlist_button_on_shop_page() {
        // Check if the guest wishlist is disabled.
        $disable_guest_wishlist = get_option( 'stwlite_settings_disable_guest_wishlist', 'no' );
        if ( 'yes' === $disable_guest_wishlist && ! is_user_logged_in() ) {
            return;
        }

        $placement = $this->shop_wishlist_button_placement_option();

        if ( 'top_right' === $placement || 'top_left' === $placement ) {
            add_action( 'woocommerce_before_shop_loop_item', array( $this, 'show_button_on_shop_page' ), 10 );
            add_filter( 'stwlite_before_woocommerce/product-image', array( $this, 'show_button_on_shop_page' ), 10 );
        } elseif ( 'above_title' === $placement ) {
            add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'show_button_on_shop_page' ), 10 );
            add_filter( 'stwlite_after_woocommerce/product-image', array( $this, 'show_button_on_shop_page' ), 10 );
        } elseif ( 'below_title' === $placement ) {
            add_action( 'woocommerce_shop_loop_item_title', array( $this, 'show_button_on_shop_page' ), 10 );
            add_filter( 'stwlite_before_woocommerce/product-price', array( $this, 'show_button_on_shop_page' ), 10 );
        } elseif ( 'above_button' === $placement ) {
            add_action( 'woocommerce_after_shop_loop_item_title', array( $this, 'show_button_on_shop_page' ), 10 );
            add_filter( 'stwlite_before_woocommerce/product-button', array( $this, 'show_button_on_shop_page' ), 10 );
        } elseif ( 'below_button' === $placement ) {
            add_action( 'woocommerce_after_shop_loop_item', array( $this, 'show_button_on_shop_page' ), 10 );
            add_filter( 'stwlite_after_woocommerce/product-button', array( $this, 'show_button_on_shop_page' ), 10 );
        }
    }

    /**
     * Display wishlist button on product page.
     *
     * @since 1.0.0
     * @access public
     */
    public function display_wishlist_button_on_product_page() {
        // Check if the guest wishlist is disabled.
        $disable_guest_wishlist = get_option( 'stwlite_settings_disable_guest_wishlist', 'no' );
        if ( 'yes' === $disable_guest_wishlist && ! is_user_logged_in() ) {
            return;
        }

        $placement = $this->product_wishlist_button_placement_option();

        if ( 'top_image_right' === $placement ) {
            add_action( 'woocommerce_product_thumbnails', array( $this, 'show_button_on_product_page' ), 10 );
        } elseif ( 'top_image_left' === $placement ) {
            add_action( 'woocommerce_before_single_product_summary', array( $this, 'show_button_on_product_page' ), 10 );
        } elseif ( 'top_cart' === $placement ) {
            add_action( 'woocommerce_before_add_to_cart_quantity', array( $this, 'show_button_on_product_page' ), 10 );
            // Fallback for products where the add-to-cart form is suppressed (e.g., price-less or non-purchasable products).
            add_action( 'woocommerce_single_product_summary', array( $this, 'show_button_on_product_page_no_form_fallback' ), 29 );
        } elseif ( 'bottom_cart' === $placement ) {
            add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'show_button_on_product_page' ), 10 );
            // Fallback for products where the add-to-cart form is suppressed (e.g., price-less or non-purchasable products).
            add_action( 'woocommerce_single_product_summary', array( $this, 'show_button_on_product_page_no_form_fallback' ), 31 );
        } elseif ( 'top_title' === $placement ) {
            add_action( 'woocommerce_single_product_summary', array( $this, 'show_button_on_product_page' ), 4 );
        } elseif ( 'bottom_title' === $placement ) {
            add_action( 'woocommerce_single_product_summary', array( $this, 'show_button_on_product_page' ), 5 );
        }
    }

    /**
     * Display wishlist button on cart page.
     *
     * @since 1.0.0
     * @access public
     */
    public function display_wishlist_button_on_cart_page() {
        $add_cart_products_to_wishlist = get_option( 'stwlite_settings_add_cart_products_to_wishlist', 'yes' );
        if ( 'yes' === $add_cart_products_to_wishlist && is_user_logged_in() ) {
            add_action( 'woocommerce_after_cart_table', array( $this, 'show_button_on_cart_page' ), 10 );
            add_action( 'stwlite_after_woocommerce/cart-line-items-block', array( $this, 'show_button_on_cart_page' ), 10 );
        }

        // Show per-product wishlist button below each product name in the cart.
        // Only available for logged-in users — guests cannot save cart items to a wishlist.
        $cart_save_mode = get_option( 'stwlite_settings_cart_save_mode', 'no' );
        if ( 'yes' === $add_cart_products_to_wishlist && 'yes' === $cart_save_mode && is_user_logged_in() ) {
            add_action( 'woocommerce_after_cart_item_name', array( $this, 'show_per_product_button_on_cart_page' ), 10, 2 );
        }
    }

    /**
     * Check if the current page is a shop page.
     *
     * @since 1.0.0
     * @access public
     */
    public function show_button_on_shop_page() {
        global $product;

        if ( ! $product instanceof \WC_Product ) {
            return;
        }

        $placement     = $this->shop_wishlist_button_placement_option();
        $style         = $this->shop_wishlist_button_style_option();
        $color_options = $this->shop_wishlist_button_color_options_option();
        $button_html   = $this->wishlist_button_html( $placement, $style, $color_options );

        // Filter the button html.
        $button_html = apply_filters( 'stwlite_wishlist_button_shop_html', $button_html, $product, $placement, $style, $color_options );

        do_action( 'stwlite_before_wishlist_button_shop_page' );
        echo wp_kses( $button_html, $this->helper_functions->get_allowed_button_html() );
        do_action( 'stwlite_after_wishlist_button_shop_page' );
    }

    /**
     * Show the wishlist button on product page.
     *
     * @since 1.0.0
     * @access public
     */
    public function show_button_on_product_page() {
        global $product;

        $placement     = $this->product_wishlist_button_placement_option();
        $style         = $this->product_wishlist_button_style_option();
        $color_options = $this->product_wishlist_button_color_options_option();
        $button_html   = $this->wishlist_button_html( $placement, $style, $color_options );

        // Filter the button html.
        $button_html = apply_filters( 'stwlite_wishlist_button_product_html', $button_html, $product, $placement, $style, $color_options );

        echo wp_kses( $button_html, $this->helper_functions->get_allowed_button_html() );
    }

    /**
     * Render the wishlist button on the single product page when the
     * configured placement targets the add-to-cart form (top_cart / bottom_cart)
     * but WooCommerce does not render the form — e.g., products without a
     * price, "call for price" / quote-only listings, catalog-mode products,
     * or otherwise non-purchasable / out-of-stock products.
     *
     * The cart placements hook woocommerce_before_add_to_cart_quantity and
     * woocommerce_after_add_to_cart_button, neither of which fires when the
     * form is suppressed. This fallback runs on woocommerce_single_product_summary
     * so the button still appears in roughly the same position.
     *
     * @since 1.0.7
     * @access public
     */
    public function show_button_on_product_page_no_form_fallback() {
        global $product;

        if ( ! $product instanceof \WC_Product ) {
            return;
        }

        // Skip when the add-to-cart form will render; the primary hook covers it.
        if ( $product->is_purchasable() && $product->is_in_stock() ) {
            return;
        }

        $this->show_button_on_product_page();
    }

    /**
     * Show the wishlist button on cart page.
     *
     * @since 1.0.0
     * @access public
     */
    public function show_button_on_cart_page() {
        $label         = $this->cart_wishlist_button_label_option();
        $style         = $this->cart_wishlist_button_style_option();
        $color_options = $this->cart_wishlist_button_color_options_option();
        $button_label  = $this->wishlist_button_label_html( $style, $label, $color_options );

        $default_wishlist = $this->wishlist->get_default_wishlist();
        $button_html      = '';
        $show_button      = true;

        if ( is_user_logged_in() && empty( $default_wishlist ) ) {
            $show_button = false;
        }

        if ( $show_button && ! $this->all_cart_items_in_wishlist() ) {
            $button_html  = '<div class="stw-wishlist-button-container stw-wishlist-button-cart">';
            $button_html .= '<button class="save-to-wishlist-cart-button stw-wishlist-button-style-' . esc_attr( $style ) . '"'
                . ' style="color: ' . esc_attr( $color_options['text_color'] ) . '; background-color: ' . esc_attr( $color_options['background_color'] ) . '; border-color: ' . esc_attr( $color_options['border_color'] ) . '; --hover-text-color: ' . esc_attr( $color_options['hover_text_color'] ) . '; --hover-background-color: ' . esc_attr( $color_options['hover_background_color'] ) . '; --hover-border-color: ' . esc_attr( $color_options['hover_border_color'] ) . '; --border-width: ' . esc_attr( $color_options['border_width'] ) . 'px; --text-size: ' . esc_attr( $color_options['text_size'] ) . 'px; --text-style: ' . esc_attr( $color_options['text_style'] ) . ';">'
                . '<span class="stw-icon-wrapper">' . $button_label . '</span>'
                . '</button>';
            $button_html .= '</div>';
        }

        // Filter the button html.
        $button_html = apply_filters( 'stwlite_wishlist_cart_button_html', $button_html );

        echo wp_kses( $button_html, $this->helper_functions->get_allowed_cart_button_html() );
    }

    /**
     * Build button HTML for every cart item so the block cart can inject them client-side.
     *
     * The WooCommerce cart block renders line items via React, so there is no
     * PHP hook equivalent to woocommerce_after_cart_item_name. Instead we
     * pre-generate the button HTML for each cart item here and expose it as
     * JSON in stwlite_frontend_js_vars.blockCartItems. A MutationObserver in
     * the frontend JS then injects each button after the product name link.
     *
     * @since 1.0.6
     * @access public
     * @return array Array of { product_id, permalink, button_html } maps.
     */
    public function get_block_cart_item_buttons() {
        if ( ! WC()->cart ) {
            return array();
        }

        $add_cart_products_to_wishlist = get_option( 'stwlite_settings_add_cart_products_to_wishlist', 'yes' );
        $cart_save_mode                = get_option( 'stwlite_settings_cart_save_mode', 'no' );
        if ( 'yes' !== $add_cart_products_to_wishlist || 'yes' !== $cart_save_mode ) {
            return array();
        }

        // Per-product cart buttons are only available to logged-in users.
        if ( ! is_user_logged_in() ) {
            return array();
        }

        global $product;
        $original_product = $product;

        $items         = array();
        $style         = $this->product_wishlist_button_style_option();
        $color_options = $this->product_wishlist_button_color_options_option();

        try {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $cart_product = isset( $cart_item['data'] ) ? $cart_item['data'] : null;
                if ( ! $cart_product instanceof \WC_Product ) {
                    continue;
                }

                // Set the global so wishlist_button_html() can read the current product.
                $product     = $cart_product;
                $button_html = $this->wishlist_button_html( 'cart_item_' . $cart_product->get_id(), $style, $color_options );
                $button_html = apply_filters( 'stwlite_wishlist_button_cart_item_html', $button_html, $cart_product, $style, $color_options );

                if ( ! $button_html ) {
                    continue;
                }

                $items[] = array(
                    'product_id'  => $cart_product->get_id(),
                    'permalink'   => get_permalink( $cart_product->get_id() ),
                    'button_html' => wp_kses( $button_html, $this->helper_functions->get_allowed_button_html() ),
                );
            }
        } finally {
            $product = $original_product;
        }

        return $items;
    }

    /**
     * Show the wishlist button below the product name for each item on the cart page.
     *
     * Uses the same style and colour settings as the product page button.
     *
     * @since 1.0.6
     * @access public
     * @param array  $cart_item     The cart item data.
     * @param string $cart_item_key The cart item key.
     */
    public function show_per_product_button_on_cart_page( $cart_item, $cart_item_key ) {
        global $product;

        $product = $cart_item['data'];

        if ( ! $product instanceof \WC_Product ) {
            return;
        }

        $style         = $this->product_wishlist_button_style_option();
        $color_options = $this->product_wishlist_button_color_options_option();
        $button_html   = $this->wishlist_button_html( 'cart_item_' . $product->get_id(), $style, $color_options );

        // Filter the button html.
        $button_html = apply_filters( 'stwlite_wishlist_button_cart_item_html', $button_html, $product, $style, $color_options );

        echo wp_kses( $button_html, $this->helper_functions->get_allowed_button_html() );
    }

    /**
     * Check whether every item currently in the cart is already saved in the
     * user's default wishlist.
     *
     * @since 1.0.5
     * @access private
     * @return bool True when all cart items are already in the wishlist.
     */
    private function all_cart_items_in_wishlist() {
        if ( ! is_user_logged_in() || ! WC()->cart ) {
            return false;
        }

        $cart_items = WC()->cart->get_cart();

        if ( empty( $cart_items ) ) {
            return false;
        }

        $default_wishlist = $this->wishlist->get_default_wishlist();
        $collection_id    = ! empty( $default_wishlist['id'] ) ? (int) $default_wishlist['id'] : 0;

        foreach ( $cart_items as $cart_item ) {
            $product_id   = (int) $cart_item['product_id'];
            $variation_id = ! empty( $cart_item['variation_id'] ) ? (int) $cart_item['variation_id'] : 0;
            $product_obj  = wc_get_product( Helper_Functions::get_effective_product_id( $cart_item ) );

            if ( ! $product_obj ) {
                return false;
            }

            $parent_id = Helper_Functions::get_parent_product_id( $product_obj, $product_id );

            $wishlist_products = $this->wishlist->check_if_product_in_wishlist( $parent_id, $collection_id, false );
            $in_wishlist       = $this->wishlist->product_check_in_wishlist( $product_obj, $wishlist_products, $variation_id, true );

            // If not found by specific variation, also check whether the parent
            // product was saved without a variation (variation_id = 0), which
            // should count as "all cart items in wishlist" for variable products.
            if ( ! $in_wishlist && $variation_id > 0 && ! empty( $wishlist_products ) ) {
                $in_wishlist = $this->wishlist->product_check_in_wishlist( $product_obj, $wishlist_products, 0, true );
            }

            if ( ! $in_wishlist ) {
                return false;
            }
        }

        return true;
    }

    /**
     * Get the wishlist button html.
     *
     * @param string $placement The placement of the button.
     * @param string $style     The style of the button.
     * @param array  $color_options The color options of the button.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function wishlist_button_html( $placement, $style, $color_options = array() ) {
        global $product;

        $default_wishlist = $this->wishlist->get_default_wishlist();
        $button_html      = '';
        $show_button      = true;

        if ( is_user_logged_in() && empty( $default_wishlist ) ) {
            $show_button = false;
        }

        $show_button = apply_filters( 'stwlite_wishlist_button_show_button', $show_button, $product );

        if ( $show_button ) {
            $is_cart_item = 0 === strpos( $placement, 'cart_item_' );

            if ( $is_cart_item && $product->is_type( 'variation' ) ) {
                // Variable product cart items expose a WC_Product_Variation whose get_id()
                // returns the variation ID, not the parent. Wishlist entries are stored
                // against the parent product ID, so we must look up by parent and then
                // match against the specific variation ID.
                $parent_id        = $product->get_parent_id();
                $variation_id     = $product->get_id();
                $exists           = $this->wishlist->check_if_product_in_wishlist( $parent_id, 0, false, true );
                $selected_product = ! empty( $exists ) ? $exists : null;

                // Check if the exact variation is in any wishlist.
                $selected_wishlist = $this->wishlist->product_check_in_wishlist( $product, $selected_product, $variation_id, is_user_logged_in() );

                // Also mark the button active when the parent variable product was saved to any
                // wishlist without a specific variation (variation_id = 0).
                if ( ! $selected_wishlist && ! empty( $selected_product ) ) {
                    $selected_wishlist = $this->wishlist->product_check_in_wishlist( $product, $selected_product, 0, is_user_logged_in() );
                }
            } else {
                // Standard path: simple products and variable products on product/shop pages
                // (where $product is the parent WC_Product_Variable, not a variation).
                $exists               = $this->wishlist->check_if_product_in_wishlist( $product->get_id(), 0, false, true );
                $current_variation_id = $this->wishlist->get_variation_id_from_attributes( $product->get_id() );
                $selected_product     = ! empty( $exists ) ? $exists : null;
                $selected_wishlist    = $this->wishlist->product_check_in_wishlist( $product, $selected_product, $current_variation_id, is_user_logged_in() );
            }

            $button_active_class = $selected_wishlist ? 'stw-active' : '';

            $button_label = $this->wishlist_button_label_html( $style, '', $color_options, '', $selected_wishlist );

            $button_html  = '<div id="stw-button-placement-' . esc_attr( $placement ) . '" class="stw-wishlist-button-container stw-wishlist-button-placement-' . esc_attr( $placement ) . '">';
            $button_html .= '<button class="save-to-wishlist-button save-to-wishlist-product-id-' . esc_attr( $product->get_id() ) . ' stw-wishlist-button-style-' . esc_attr( $style ) . ' ' . esc_attr( $button_active_class ) . '" data-product-id="' . esc_attr( $product->get_id() ) . '" style="color: ' . esc_attr( $color_options['text_color'] ) . '; background-color: ' . esc_attr( $color_options['background_color'] ) . '; border-color: ' . esc_attr( $color_options['border_color'] ) . '; --hover-text-color: ' . esc_attr( $color_options['hover_text_color'] ) . '; --hover-background-color: ' . esc_attr( $color_options['hover_background_color'] ) . '; --hover-border-color: ' . esc_attr( $color_options['hover_border_color'] ) . '; --border-width: ' . esc_attr( $color_options['border_width'] ) . 'px; --text-size: ' . esc_attr( $color_options['text_size'] ) . 'px; --text-style: ' . esc_attr( $color_options['text_style'] ) . ';"><span class="stw-icon-wrapper">' . $button_label . '</span></button>';

            // Check if the multiple lists is enabled.
            $allow_multiple_lists = get_option( 'stwlite_settings_allow_multiple_wishlists', 'yes' );
            $multiple_lists       = apply_filters( 'stwlite_wishlist_button_allow_product_multiple_lists', true, $product );
            if ( $multiple_lists && 'yes' === $allow_multiple_lists && is_user_logged_in() ) {
                $wishlist_options = $this->wishlist->get_wishlists_as_options();

                $button_html .= '<div class="save-to-wishlist-multiple-lists-container">';
                $button_html .= '<span class="save-to-wishlist-multiple-lists-button">';
                $button_html .= esc_html__( 'Choose list:', 'saveto-wishlist-lite-for-woocommerce' );
                $button_html .= '</span>';
                $button_html .= '<div class="save-to-wishlist-multiple-lists-button-content">';
                $button_html .= '<select name="stw-multiple-lists-select" data-product-id="' . esc_attr( $product->get_id() ) . '">';
                $button_html .= '<option value="">' . esc_html__( 'Select a wishlist', 'saveto-wishlist-lite-for-woocommerce' ) . '</option>';
                foreach ( $wishlist_options as $option ) {
                    $button_html .= '<option value="' . esc_attr( $option['id'] . ':' . $option['url_code'] ) . '">' . esc_html( $option['name'] ) . '</option>';
                }
                $button_html .= '</select>';
                $button_html .= '</div>';
                $button_html .= '</div>';
            }

            $button_html .= '</div>';
        }

        // Filter the button html.
        $button_html = apply_filters( 'stwlite_wishlist_button_html', $button_html, $product, $placement, $style, $button_label );

        return $button_html;
    }

    /**
     * Get the shop wishlist button label.
     *
     * @param string $style The style of the button.
     * @param string $label The label of the button.
     * @param array  $color_options The color options of the button.
     * @param string $selected_wishlist_label The label of the selected wishlist.
     * @param bool   $selected_wishlist Whether the product is in the selected wishlist.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function wishlist_button_label_html( $style, $label = '', $color_options = array(), $selected_wishlist_label = '', $selected_wishlist = false ) {
        $label = ! empty( $label ) ? $label : $this->shop_wishlist_button_label_option();

        if ( $selected_wishlist ) {
            $label = ! empty( $selected_wishlist_label ) ? $selected_wishlist_label : $this->selected_wishlist_button_label_option();
        }

        $icon_color_control = isset( $color_options['icon_color'] ) ? $color_options['icon_color'] : '#FF0000';

        $button_label = '';
        $text_label   = '<span class="stw-text-label" data-label="' . esc_attr( $label ) . '">' . esc_html( $label ) . '</span>';
        if ( 'icon_and_text' === $style || 'icon_and_text_pill' === $style || 'icon_only' === $style ) {
            $button_label = '<span class="stw-icon-holder"><svg class="stw-icon stw-icon-heart selected-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17" fill="none"><g clip-path="url(#clip0_1_56)"><path d="M19.6527 3.95588C19.1291 2.4517 18.0663 1.22782 16.7417 0.592197C15.9211 0.197399 15.085 0 14.2644 0C12.8851 0 11.4863 0.552717 10.0014 1.68579C8.51664 0.552717 7.1178 0 5.73849 0C4.35919 0 4.08176 0.201347 3.26121 0.592197C1.93661 1.22782 0.873797 2.4517 0.350208 3.95588C-0.173382 5.44821 -0.106956 7.06688 0.529947 8.39735C1.38566 10.1779 2.92908 11.7531 4.01533 12.7875L4.03096 12.8033C4.91794 13.6403 6.75832 15.3892 8.73155 16.6328C9.11447 16.8737 9.55601 17 10.0054 17C10.4547 17 10.8962 16.8737 11.2713 16.6328C13.2446 15.3892 15.0889 13.6442 15.9719 12.8033L15.9876 12.7875C17.0777 11.7571 18.6211 10.1779 19.4729 8.39735C20.1059 7.06688 20.1724 5.44821 19.6527 3.95588ZM19.1252 8.22364C18.3515 9.83837 16.9761 11.311 15.7258 12.4993C14.8271 13.3521 13.0258 15.0655 11.0721 16.2973C10.4195 16.7079 9.59117 16.7079 8.93864 16.2973C6.98885 15.0655 5.18364 13.3521 4.28494 12.4993C3.03458 11.311 1.65918 9.83442 0.885519 8.22364C-0.364844 5.61008 0.865983 2.17534 3.43313 0.947515C5.86744 -0.217139 8.00087 0.580353 10.0054 2.18323C12.0098 0.580353 14.1433 -0.217139 16.5776 0.947515C19.1447 2.17534 20.3756 5.61008 19.1252 8.22364Z" fill="' . esc_attr( $icon_color_control ) . '"/></g><defs><clipPath id="clip0_1_56"><rect width="20" height="17" fill="white"/></clipPath></defs></svg><svg class="stw-icon stw-icon-heart hover-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17" fill="none"><path d="M10.0054 16.8252C9.55601 16.8252 9.11447 16.7002 8.73155 16.4618C6.75832 15.231 4.91794 13.5 4.03096 12.6716L4.01533 12.656C2.92908 11.6323 1.38566 10.0732 0.529947 8.31101C-0.106956 6.99422 -0.173382 5.39219 0.350208 3.9152C0.873797 2.42649 1.93661 1.2152 3.26121 0.586108C4.08176 0.199277 4.91403 0 5.73849 0C7.1178 0 8.51664 0.547034 10.0014 1.66845C11.4863 0.547034 12.8851 0 14.2644 0C15.6437 0 15.9211 0.195369 16.7417 0.586108C18.0663 1.2152 19.1291 2.42649 19.6527 3.9152C20.1724 5.39219 20.1059 6.99422 19.4729 8.31101C18.6211 10.0732 17.0777 11.6362 15.9876 12.656L15.9719 12.6716C15.0889 13.5039 13.2446 15.231 11.2713 16.4618C10.8962 16.7002 10.4547 16.8252 10.0054 16.8252Z" fill="' . esc_attr( $icon_color_control ) . '"/><path d="M19.1252 8.13908C18.3515 9.7372 16.9761 11.1947 15.7258 12.3708C14.8271 13.2148 13.0258 14.9106 11.0721 16.1297C10.4195 16.5361 9.59117 16.5361 8.93863 16.1297C6.98885 14.9106 5.18364 13.2148 4.28494 12.3708C3.03457 11.1947 1.65917 9.73329 0.885512 8.13908C-0.364851 5.55239 0.865975 2.15297 3.43313 0.93777C5.86743 -0.214909 8.00086 0.574383 10.0053 2.16078C12.0098 0.574383 14.1433 -0.214909 16.5776 0.93777C19.1447 2.15297 20.3755 5.55239 19.1252 8.13908Z" fill="' . esc_attr( $icon_color_control ) . '"/></svg></span>' . ( 'icon_only' === $style ? '' : $text_label );
        } elseif ( 'text_only' === $style ) {
            $button_label = $text_label;
        } elseif ( 'solid_icon' === $style || 'solid_and_text' === $style || 'solid_and_text_pill' === $style ) {
            $button_label = '<span class="stw-icon-holder"><svg class="stw-icon stw-icon-solid selected-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none"><path d="M9.99999 18.0655C9.83333 18.0655 9.66666 18.0655 9.49999 17.9821C9.16666 17.8988 8.91666 17.6488 8.58333 17.3155C6.66666 15.6488 3.33333 12.5655 2.08333 10.9821C0.166659 8.48213 0.499993 5.06546 2.91666 2.98213C4.99999 1.23213 7.91666 1.31546 9.99999 3.1488C12.0833 1.31546 14.9167 1.23213 17.0833 2.98213C19.5 4.98213 19.9167 8.48213 17.9167 10.9821C16.6667 12.5655 13.3333 15.6488 11.4167 17.3155C11.0833 17.6488 10.8333 17.8155 10.5 17.9821C10.3333 17.9821 10.1667 18.0655 9.99999 18.0655ZM6.33333 3.31546C5.49999 3.31546 4.74999 3.56546 3.99999 4.23213C2.24999 5.73213 1.99999 8.1488 3.33333 9.98213C4.58333 11.5655 8.16666 14.8155 9.66666 16.1488C9.83333 16.3155 9.99999 16.3988 9.99999 16.4821C9.99999 16.4821 10.1667 16.3155 10.25 16.1488C11.75 14.8155 15.3333 11.5655 16.5833 9.98213C18 8.1488 17.75 5.73213 16 4.23213C14.1667 2.6488 11.9167 3.31546 10.6667 4.81546C10.3333 5.1488 9.74999 5.1488 9.41666 4.81546C8.58333 3.8988 7.49999 3.31546 6.33333 3.31546Z" fill="' . esc_attr( $icon_color_control ) . '"/></svg><svg class="stw-icon stw-icon-solid hover-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17" fill="none"><g clip-path="url(#clip0_1_44)"><path d="M9.56522 16.745C9.3913 16.745 9.21739 16.745 9.04348 16.66C8.69565 16.575 8.43478 16.32 8.08696 15.98C6.08696 14.28 2.60869 11.135 1.30435 9.52C-0.695654 6.97 -0.347828 3.485 2.17391 1.36C4.34782 -0.424999 7.3913 -0.339999 9.56522 1.53C11.7391 -0.339999 14.6957 -0.424999 16.9565 1.36C19.4783 3.4 19.913 6.97 17.8261 9.52C16.5217 11.135 13.0435 14.28 11.0435 15.98C10.6957 16.32 10.4348 16.49 10.087 16.66C9.91304 16.66 9.73913 16.745 9.56522 16.745ZM5.73913 1.7C4.86956 1.7 4.08696 1.955 3.30435 2.635C1.47826 4.165 1.21739 6.63 2.60869 8.5C3.91304 10.115 7.65217 13.43 9.21739 14.79C9.3913 14.96 9.56522 15.045 9.56522 15.13C9.56522 15.13 9.73913 14.96 9.82609 14.79C11.3913 13.43 15.1304 10.115 16.4348 8.5C17.913 6.63 17.6522 4.165 15.8261 2.635C13.913 1.02 11.5652 1.7 10.2609 3.23C9.91304 3.57 9.30435 3.57 8.95652 3.23C8.08696 2.295 6.95652 1.7 5.73913 1.7ZM8.69565 10.2C8.43478 10.2 8.26087 10.115 8.08696 9.945L6.34782 8.245C6 7.905 6 7.395 6.34782 7.055C6.69565 6.715 7.21739 6.715 7.56522 7.055L8.69565 8.16L12 4.93C12.3478 4.59 12.8696 4.59 13.2174 4.93C13.5652 5.27 13.5652 5.78 13.2174 6.12L9.30435 9.945C9.13043 10.115 8.95652 10.2 8.69565 10.2Z" fill="' . esc_attr( $icon_color_control ) . '"/></g><defs><clipPath id="clip0_1_44"><rect width="20" height="17" fill="white"/></clipPath></defs></svg></span>' . ( 'solid_icon' === $style ? '' : $text_label );
        } elseif ( 'minimal_icon' === $style || 'minimal_and_text' === $style || 'minimal_and_text_pill' === $style ) {
            $button_label = '<span class="stw-icon-holder"><svg class="stw-icon stw-icon-minimal selected-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="24" viewBox="0 0 20 24" fill="none"><g clip-path="url(#clip0_1_48)"><path fill-rule="evenodd" clip-rule="evenodd" d="M9.59078 9.5625C8.4003 8.325 6.46578 8.325 5.2753 9.5625C4.15923 10.725 4.15923 12.6 5.2753 13.8L9.47917 18.15C9.62798 18.3 9.81399 18.375 10 18.375C10.186 18.375 10.4092 18.3 10.5208 18.15L14.7247 13.8C15.8408 12.6375 15.8408 10.7625 14.7247 9.5625C13.5342 8.325 11.5997 8.325 10.4092 9.5625L10 9.975L9.59078 9.5625ZM6.35417 10.575C6.94941 9.9375 7.95387 9.9375 8.54911 10.575L9.47917 11.55C9.62798 11.7 9.81399 11.775 10 11.775C10.186 11.775 10.4092 11.7 10.5208 11.55L11.4509 10.575C12.0461 9.9375 13.0506 9.9375 13.6458 10.575C14.2039 11.175 14.2039 12.1125 13.6458 12.7125L10 16.5375L6.35417 12.7125C5.79614 12.1125 5.79614 11.175 6.35417 10.575Z" fill="' . esc_attr( $icon_color_control ) . '"/><path fill-rule="evenodd" clip-rule="evenodd" d="M19.3006 7.875C19.3006 5.625 17.8869 3.7125 15.9152 2.9625C15.7292 1.725 14.6503 0.75 13.3482 0.75H6.65178C5.3497 0.75 4.27083 1.6875 4.08482 2.9625C2.11309 3.7125 0.699402 5.625 0.699402 7.875V18C0.699402 20.8875 3.04315 23.25 5.90774 23.25H14.0923C16.9568 23.25 19.3006 20.8875 19.3006 18V7.875ZM2.1875 7.875C2.1875 6.375 3.04315 5.1 4.30803 4.5C4.71726 5.4 5.61012 6 6.65178 6H13.3482C14.3899 6 15.2827 5.4 15.692 4.5C16.9568 5.1 17.8125 6.375 17.8125 7.875V18C17.8125 20.0625 16.1384 21.75 14.0923 21.75H5.90774C3.8616 21.75 2.1875 20.0625 2.1875 18V7.875ZM6.65178 2.25C6.01934 2.25 5.53571 2.7375 5.53571 3.375C5.53571 4.0125 6.01934 4.5 6.65178 4.5H13.3482C13.9807 4.5 14.4643 4.0125 14.4643 3.375C14.4643 2.7375 13.9807 2.25 13.3482 2.25H6.65178Z" fill="' . esc_attr( $icon_color_control ) . '"/></g><defs><clipPath id="clip0_1_48"><rect width="20" height="24" fill="white"/></clipPath></defs></svg><svg class="stw-icon stw-icon-minimal hover-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="24" viewBox="0 0 20 24" fill="none"><g clip-path="url(#clip0_1_46)"><path fill-rule="evenodd" clip-rule="evenodd" d="M20 7.6C20 5.2 18.48 3.16 16.36 2.36C16.16 1.04 15 0 13.6 0H6.4C5 0 3.84 1 3.64 2.36C1.52 3.16 0 5.2 0 7.6V18.4C0 21.48 2.52 24 5.6 24H14.4C17.48 24 20 21.48 20 18.4V7.6ZM6.4 1.6C5.72 1.6 5.2 2.12 5.2 2.8C5.2 3.48 5.72 4 6.4 4H13.6C14.28 4 14.8 3.48 14.8 2.8C14.8 2.12 14.28 1.6 13.6 1.6H6.4ZM4.92 10.2C6.2 8.88 8.28 8.88 9.56 10.2L10 10.64L10.44 10.2C11.72 8.88 13.8 8.88 15.08 10.2C16.28 11.44 16.28 13.44 15.08 14.72L10.56 19.36C10.4 19.52 10.2 19.6 10 19.6C9.8 19.6 9.56 19.52 9.44 19.36L4.92 14.72C3.72 13.48 3.72 11.48 4.92 10.2Z" fill="' . esc_attr( $icon_color_control ) . '"/></g><defs><clipPath id="clip0_1_46"><rect width="20" height="24" fill="white"/></clipPath></defs></svg></span>' . ( 'minimal_icon' === $style ? '' : $text_label );
        }

        return $button_label;
    }

    /**
     * Get the shop wishlist button label.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function shop_wishlist_button_label_option() {
        $default_label = __( 'Save to Wishlist', 'saveto-wishlist-lite-for-woocommerce' );
        $label         = $this->helper_functions->get_string_option( 'stwlite_settings_wishlist_button_label', $default_label );

        // Filter the label.
        $label = apply_filters( 'stwlite_wishlist_button_shop_label', $label );

        return $label;
    }

    /**
     * Get the selected wishlist button label.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function selected_wishlist_button_label_option() {
        $default_label = __( 'Remove from Wishlist', 'saveto-wishlist-lite-for-woocommerce' );
        $label         = $this->helper_functions->get_string_option( 'stwlite_settings_wishlist_button_selected_label', $default_label );

        // Filter the label.
        $label = apply_filters( 'stwlite_wishlist_button_selected_shop_label', $label );

        return $label;
    }

    /**
     * Get the shop wishlist button placement.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function shop_wishlist_button_placement_option() {
        $default_placement = 'top_right';
        $placement         = get_option( 'stwlite_settings_wishlist_button_shop_placement', $default_placement );

        // Filter the placement.
        $placement = apply_filters( 'stwlite_wishlist_button_shop_placement', $placement );

        return $placement;
    }

    /**
     * Get the shop wishlist button style.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function shop_wishlist_button_style_option() {
        $default_style = 'solid_and_text';
        $style         = get_option( 'stwlite_settings_wishlist_button_shop_style', $default_style );

        // Filter the style.
        $style = apply_filters( 'stwlite_wishlist_button_shop_style', $style );

        return $style;
    }

    /**
     * Get the shop wishlist button color options.
     *
     * @since 1.0.0
     * @access public
     * @return array
     */
    public function shop_wishlist_button_color_options_option() {

        $text_color             = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_text_color', '#000000' ) );
        $hover_text_color       = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_hover_text_color', '#000000' ) );
        $background_color       = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_background_color', '#FFFFFF' ) );
        $hover_background_color = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_hover_background_color', '#FFFFFF' ) );
        $border_color           = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_border_color', '#000000' ) );
        $hover_border_color     = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_hover_border_color', '#000000' ) );
        $icon_color             = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_shop_icon_color', '#FF0000' ) );
        $border_width           = absint( get_option( 'stwlite_settings_wishlist_button_shop_border_width', 1 ) );
        $text_size              = absint( get_option( 'stwlite_settings_wishlist_button_shop_text_size', 14 ) );
        $text_style             = sanitize_text_field( get_option( 'stwlite_settings_wishlist_button_shop_text_style', 'normal' ) );

        $default_color_options = array(
            'text_color'             => $text_color ?? '#000000',
            'hover_text_color'       => $hover_text_color ?? '#000000',
            'background_color'       => $background_color ?? '#FFFFFF',
            'hover_background_color' => $hover_background_color ?? '#FFFFFF',
            'border_color'           => $border_color ?? '#000000',
            'hover_border_color'     => $hover_border_color ?? '#000000',
            'icon_color'             => $icon_color ?? '#FF0000',
            'border_width'           => $border_width ?? 1,
            'text_size'              => $text_size ?? 14,
            'text_style'             => $text_style ?? 'normal',
        );

        $color_options = get_option( 'stwlite_settings_wishlist_button_shop_color_options', $default_color_options );

        // Sanitize color options array values.
        $color_options = array(
            'text_color'             => sanitize_hex_color( $color_options['text_color'] ?? '#000000' ) ?? '#000000',
            'hover_text_color'       => sanitize_hex_color( $color_options['hover_text_color'] ?? '#000000' ) ?? '#000000',
            'background_color'       => sanitize_hex_color( $color_options['background_color'] ?? '#FFFFFF' ) ?? '#FFFFFF',
            'hover_background_color' => sanitize_hex_color( $color_options['hover_background_color'] ?? '#FFFFFF' ) ?? '#FFFFFF',
            'border_color'           => sanitize_hex_color( $color_options['border_color'] ?? '#000000' ) ?? '#000000',
            'hover_border_color'     => sanitize_hex_color( $color_options['hover_border_color'] ?? '#000000' ) ?? '#000000',
            'icon_color'             => sanitize_hex_color( $color_options['icon_color'] ?? '#FF0000' ) ?? '#FF0000',
            'border_width'           => absint( $color_options['border_width'] ?? 1 ) ?? 1,
            'text_size'              => absint( $color_options['text_size'] ?? 14 ) ?? 14,
            'text_style'             => sanitize_text_field( $color_options['text_style'] ?? 'normal' ) ?? 'normal',
        );

        // Filter the color options.
        $color_options = apply_filters( 'stwlite_wishlist_button_shop_color_options', $color_options );

        return $color_options;
    }

    /**
     * Get the product wishlist button placement.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function product_wishlist_button_placement_option() {
        $default_placement = 'top_image_right';
        $placement         = get_option( 'stwlite_settings_wishlist_button_product_placement', $default_placement );

        // Filter the placement.
        $placement = apply_filters( 'stwlite_wishlist_button_product_placement', $placement );

        return $placement;
    }

    /**
     * Get the product wishlist button style.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function product_wishlist_button_style_option() {
        $default_style = 'icon_and_text';
        $style         = get_option( 'stwlite_settings_wishlist_button_product_style', $default_style );

        // Filter the style.
        $style = apply_filters( 'stwlite_wishlist_button_product_style', $style );

        return $style;
    }

    /**
     * Get the product wishlist button color options.
     *
     * @since 1.0.0
     * @access public
     * @return array
     */
    public function product_wishlist_button_color_options_option() {

        $text_color             = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_text_color', '#000000' ) );
        $hover_text_color       = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_hover_text_color', '#000000' ) );
        $background_color       = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_background_color', '#FFFFFF' ) );
        $hover_background_color = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_hover_background_color', '#FFFFFF' ) );
        $border_color           = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_border_color', '#000000' ) );
        $hover_border_color     = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_hover_border_color', '#000000' ) );
        $icon_color             = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_product_icon_color', '#FF0000' ) );
        $border_width           = absint( get_option( 'stwlite_settings_wishlist_button_product_border_width', 1 ) );
        $text_size              = absint( get_option( 'stwlite_settings_wishlist_button_product_text_size', 14 ) );
        $text_style             = sanitize_text_field( get_option( 'stwlite_settings_wishlist_button_product_text_style', 'normal' ) );

        $default_color_options = array(
            'text_color'             => $text_color ?? '#000000',
            'hover_text_color'       => $hover_text_color ?? '#000000',
            'background_color'       => $background_color ?? '#FFFFFF',
            'hover_background_color' => $hover_background_color ?? '#FFFFFF',
            'border_color'           => $border_color ?? '#000000',
            'hover_border_color'     => $hover_border_color ?? '#000000',
            'icon_color'             => $icon_color ?? '#FF0000',
            'border_width'           => $border_width ?? 1,
            'text_size'              => $text_size ?? 14,
            'text_style'             => $text_style ?? 'normal',
        );

        $color_options = get_option( 'stwlite_settings_wishlist_button_product_color_options', $default_color_options );

        // Sanitize color options array values.
        $color_options = array(
            'text_color'             => sanitize_hex_color( $color_options['text_color'] ?? '#000000' ) ?? '#000000',
            'hover_text_color'       => sanitize_hex_color( $color_options['hover_text_color'] ?? '#000000' ) ?? '#000000',
            'background_color'       => sanitize_hex_color( $color_options['background_color'] ?? '#FFFFFF' ) ?? '#FFFFFF',
            'hover_background_color' => sanitize_hex_color( $color_options['hover_background_color'] ?? '#FFFFFF' ) ?? '#FFFFFF',
            'border_color'           => sanitize_hex_color( $color_options['border_color'] ?? '#000000' ) ?? '#000000',
            'hover_border_color'     => sanitize_hex_color( $color_options['hover_border_color'] ?? '#000000' ) ?? '#000000',
            'icon_color'             => sanitize_hex_color( $color_options['icon_color'] ?? '#FF0000' ) ?? '#FF0000',
            'border_width'           => absint( $color_options['border_width'] ?? 1 ) ?? 1,
            'text_size'              => absint( $color_options['text_size'] ?? 14 ) ?? 14,
            'text_style'             => sanitize_text_field( $color_options['text_style'] ?? 'normal' ) ?? 'normal',
        );

        // Filter the color options.
        $color_options = apply_filters( 'stwlite_wishlist_button_product_color_options', $color_options );

        return $color_options;
    }

    /**
     * Get the cart wishlist button label.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function cart_wishlist_button_label_option() {
        $default_label = __( 'Save Cart to Wishlist', 'saveto-wishlist-lite-for-woocommerce' );
        $label         = $this->helper_functions->get_string_option( 'stwlite_settings_wishlist_button_cart_label', $default_label );

        // Filter the label.
        $label = apply_filters( 'stwlite_wishlist_button_cart_label', $label );

        return $label;
    }

    /**
     * Get the cart wishlist button style.
     *
     * @since 1.0.0
     * @access public
     * @return string
     */
    public function cart_wishlist_button_style_option() {
        $default_style = 'solid_and_text';
        $style         = get_option( 'stwlite_settings_wishlist_button_cart_style', $default_style );

        // Filter the style.
        $style = apply_filters( 'stwlite_wishlist_button_cart_style', $style );

        return $style;
    }

    /**
     * Get the cart wishlist button color options.
     *
     * @since 1.0.0
     * @access public
     * @return array
     */
    public function cart_wishlist_button_color_options_option() {

        $text_color             = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_text_color', '#000000' ) );
        $hover_text_color       = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_hover_text_color', '#000000' ) );
        $background_color       = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_background_color', '#FFFFFF' ) );
        $hover_background_color = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_hover_background_color', '#FFFFFF' ) );
        $border_color           = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_border_color', '#000000' ) );
        $hover_border_color     = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_hover_border_color', '#000000' ) );
        $icon_color             = sanitize_hex_color( get_option( 'stwlite_settings_wishlist_button_cart_icon_color', '#FF0000' ) );
        $border_width           = absint( get_option( 'stwlite_settings_wishlist_button_cart_border_width', 1 ) );
        $text_size              = absint( get_option( 'stwlite_settings_wishlist_button_cart_text_size', 14 ) );
        $text_style             = sanitize_text_field( get_option( 'stwlite_settings_wishlist_button_cart_text_style', 'normal' ) );

        $default_color_options = array(
            'text_color'             => $text_color ?? '#000000',
            'hover_text_color'       => $hover_text_color ?? '#000000',
            'background_color'       => $background_color ?? '#FFFFFF',
            'hover_background_color' => $hover_background_color ?? '#FFFFFF',
            'border_color'           => $border_color ?? '#000000',
            'hover_border_color'     => $hover_border_color ?? '#000000',
            'icon_color'             => $icon_color ?? '#FF0000',
            'border_width'           => $border_width ?? 1,
            'text_size'              => $text_size ?? 14,
            'text_style'             => $text_style ?? 'normal',
        );

        $color_options = get_option( 'stwlite_settings_wishlist_button_cart_color_options', $default_color_options );

        // Sanitize color options array values.
        $color_options = array(
            'text_color'             => sanitize_hex_color( $color_options['text_color'] ?? '#000000' ) ?? '#000000',
            'hover_text_color'       => sanitize_hex_color( $color_options['hover_text_color'] ?? '#000000' ) ?? '#000000',
            'background_color'       => sanitize_hex_color( $color_options['background_color'] ?? '#FFFFFF' ) ?? '#FFFFFF',
            'hover_background_color' => sanitize_hex_color( $color_options['hover_background_color'] ?? '#FFFFFF' ) ?? '#FFFFFF',
            'border_color'           => sanitize_hex_color( $color_options['border_color'] ?? '#000000' ) ?? '#000000',
            'hover_border_color'     => sanitize_hex_color( $color_options['hover_border_color'] ?? '#000000' ) ?? '#000000',
            'icon_color'             => sanitize_hex_color( $color_options['icon_color'] ?? '#FF0000' ) ?? '#FF0000',
            'border_width'           => absint( $color_options['border_width'] ?? 1 ) ?? 1,
            'text_size'              => absint( $color_options['text_size'] ?? 14 ) ?? 14,
            'text_style'             => sanitize_text_field( $color_options['text_style'] ?? 'normal' ) ?? 'normal',
        );

        // Filter the color options.
        $color_options = apply_filters( 'stwlite_wishlist_button_cart_color_options', $color_options );

        return $color_options;
    }
}
