<?php

/**
 * Frontend swatches coordinator.
 *
 * @package B3WVS\VariationSwatches\Frontend
 */
namespace B3WVS\VariationSwatches\Frontend;

defined( 'ABSPATH' ) || exit;
/**
 * Swatches Class
 */
class Swatches {
    /**
     * Instance of this class.
     *
     * @var Swatches
     */
    private static $instance = null;

    /**
     * Swatch data service.
     *
     * @var SwatchDataService
     */
    private $data_service;

    /**
     * Optional runtime controller for additional frontend behaviors.
     *
     * @var object|null
     */
    private $extension_controller;

    /**
     * Get instance of this class.
     *
     * @return Swatches
     */
    public static function get_instance() {
        if ( is_null( self::$instance ) ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor.
     */
    private function __construct() {
        $this->data_service = new SwatchDataService();
        $this->extension_controller = null;
        add_filter(
            'woocommerce_dropdown_variation_attribute_options_html',
            array($this, 'replace_dropdown_with_swatches'),
            10,
            2
        );
    }

    /**
     * Replace dropdown with swatches.
     *
     * @param string $html HTML of the dropdown.
     * @param array  $args Arguments.
     * @return string
     */
    public function replace_dropdown_with_swatches( $html, $args ) {
        $settings = \b3wvs_get_effective_settings();
        $auto_convert = ( !isset( $settings['auto_convert_dropdowns'] ) ? true : !empty( $settings['auto_convert_dropdowns'] ) );
        if ( !$auto_convert ) {
            return $html;
        }
        $product = $args['product'];
        $attribute = $args['attribute'];
        $options = $args['options'];
        if ( empty( $options ) && $product && is_object( $product ) && method_exists( $product, 'get_variation_attributes' ) ) {
            // Some first-request product renders can arrive with an empty options array
            // even though WooCommerce can still build the native dropdown. Recover the
            // options from the product so swatch replacement remains stable.
            $variation_attributes = $product->get_variation_attributes();
            if ( is_array( $variation_attributes ) && isset( $variation_attributes[$attribute] ) && is_array( $variation_attributes[$attribute] ) ) {
                $options = $variation_attributes[$attribute];
            }
        }
        if ( empty( $options ) || !$product ) {
            return $html;
        }
        $attribute_data = $this->data_service->get_attribute_data(
            $product,
            $attribute,
            $options,
            $settings
        );
        $swatch_html = '<div class="b3-wvs-swatches-container" data-attribute="' . esc_attr( $attribute ) . '" data-swatches="' . esc_attr( wp_json_encode( $attribute_data ) ) . '">';
        $swatch_html .= '<div class="b3-wvs-swatches-mount"></div>';
        // Keep the original select visible by default as a no-JS fallback.
        // The frontend script will hide it after the swatches mount successfully.
        $swatch_html .= '<div class="b3-wvs-original-select">' . $html . '</div>';
        $swatch_html .= '</div>';
        return $swatch_html;
    }

}
