<?php

/**
 * Frontend functionality with Pro feature checks.
 * 
 * @author Nikunj Hatkar
 * @link https://github.com/nikunj8866
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Prevent direct access
}
class Broken_Image_Fallback_Frontend {
    /**
     * Plugin options
     */
    private $options;

    /**
     * Constructor
     */
    public function __construct() {
        $this->options = Broken_Image_Fallback::get_options();
        add_action( 'wp_enqueue_scripts', array($this, 'enqueue_scripts') );
    }

    /**
     * Enqueue frontend scripts and styles.
     */
    public function enqueue_scripts() {
        // Main JavaScript file
        wp_enqueue_script(
            'broken-image-fallback',
            BROKIMFA_PLUGIN_URL . 'assets/broken-image-fallback.js',
            array(),
            BROKIMFA_VERSION,
            true
        );
        // CSS file
        wp_enqueue_style(
            'broken-image-fallback',
            BROKIMFA_PLUGIN_URL . 'assets/broken-image-fallback.css',
            array(),
            BROKIMFA_VERSION
        );
        // Prepare options for JavaScript
        $js_options = array(
            'fallbackMethod'              => 'default_image',
            'defaultImageUrl'             => ( isset( $this->options['default_image_url'] ) ? $this->options['default_image_url'] : '' ),
            'enableFade'                  => false,
            'fadeDuration'                => 300,
            'lazyLoadingCompat'           => ( isset( $this->options['lazy_loading_compat'] ) ? $this->options['lazy_loading_compat'] : true ),
            'hideCompletely'              => false,
            'enableFeaturedImageFallback' => false,
            'enableGutenbergSupport'      => false,
            'enableTaxonomySupport'       => false,
            'isPro'                       => brokimfa_fs()->is_premium(),
        );
        // Pass options to JavaScript
        wp_localize_script( 'broken-image-fallback', 'brokimfaOptions', $js_options );
    }

    /**
     * Get image size dimensions (PRO)
     */
    private function get_image_size_dimensions( $size ) {
        global $_wp_additional_image_sizes;
        $dimensions = array(
            'width'  => '',
            'height' => '',
        );
        if ( in_array( $size, array(
            'thumbnail',
            'medium',
            'medium_large',
            'large'
        ) ) ) {
            $dimensions['width'] = get_option( $size . '_size_w' );
            $dimensions['height'] = get_option( $size . '_size_h' );
        } elseif ( isset( $_wp_additional_image_sizes[$size] ) ) {
            $dimensions['width'] = $_wp_additional_image_sizes[$size]['width'];
            $dimensions['height'] = $_wp_additional_image_sizes[$size]['height'];
        }
        return $dimensions;
    }

}
