<?php
if ( ! defined( 'ABSPATH' ) ) {
	// Exit if accessed directly.
	exit;
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_image_type' ) ) {
	function qode_badges_for_woocommerce_badge_image_type( $options ) {
		$options['image'] = esc_html__( 'Image', 'qode-badges-for-woocommerce' );

		return $options;
	}

	add_filter( 'qode_badges_for_woocommerce_filter_badge_types', 'qode_badges_for_woocommerce_badge_image_type' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_predefined_images' ) ) {
	function qode_badges_for_woocommerce_predefined_images() {
		$images = array();

		foreach ( glob( QODE_BADGES_FOR_WOOCOMMERCE_CPT_PATH . '/badge/layouts/image/assets/img/*.svg' ) as $image_path ) {
			$image_name = strstr( $image_path, '/img' );
			$image_url  = QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/layouts/image/assets' . $image_name;

			$images[ $image_url ] = array(
				'image' => $image_url,
				'label' => $image_name,
			);
		}

		return apply_filters( 'qode_badges_for_woocommerce_filter_predefined_images_array', $images );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_image_layout_default_value' ) ) {
	function qode_badges_for_woocommerce_image_layout_default_value( $default_value, $field_type ) {
		if ( 'predefined_image' === $field_type ) {
			$image_name    = '/img/1.svg';
			$default_value = QODE_BADGES_FOR_WOOCOMMERCE_CPT_URL_PATH . '/badge/layouts/image/assets' . $image_name;
		}

		return $default_value;
	}

	add_filter( 'qode_badges_for_woocommerce_filter_default_value', 'qode_badges_for_woocommerce_image_layout_default_value', 10, 2 );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_image_add_dependency_options' ) ) {
	function qode_badges_for_woocommerce_badge_image_add_dependency_options( $types ) {
		$types[] = 'image';

		return $types;
	}

	add_filter( 'qode_badges_for_woocommerce_filter_colors_badge_types', 'qode_badges_for_woocommerce_badge_image_add_dependency_options' );
	add_filter( 'qode_badges_for_woocommerce_filter_border_badge_types', 'qode_badges_for_woocommerce_badge_image_add_dependency_options' );
	add_filter( 'qode_badges_for_woocommerce_filter_hide_height_types', 'qode_badges_for_woocommerce_badge_image_add_dependency_options' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_image_attribute_preview' ) ) {
	function qode_badges_for_woocommerce_image_attribute_preview( $attr ) {

		if ( is_admin() ) {
			$current_screen = get_current_screen();

			if ( 'qbfw_badge' === $current_screen->post_type ) {
				$attr['srcset'] = '';
			}
		}

		return $attr;
	}

	add_filter( 'wp_get_attachment_image_attributes', 'qode_badges_for_woocommerce_image_attribute_preview' );
}
