<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! function_exists( 'drop_print_checkout_shipping_tiers_assets_enqueue' ) ) {
	function drop_print_checkout_shipping_tiers_assets_enqueue() {
		if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
			return;
		}
		$place_order_blocked_text = '';
		if ( function_exists( 'drop_print_get_responsible_user_for_auto_production' ) && function_exists( 'drop_print_get_user_settings' ) ) {
			$user_id = drop_print_get_responsible_user_for_auto_production();
			if ( $user_id > 0 ) {
				$settings                 = drop_print_get_user_settings( $user_id );
				$place_order_blocked_text = ! empty( $settings['place_order_blocked_text'] ) ? (string) $settings['place_order_blocked_text'] : '';
			}
		}
		$has_drop_print_items = false;
		if ( function_exists( 'WC' ) && WC()->cart && function_exists( 'drop_print_cart_item_is_drop_print_item' ) ) {
			foreach ( WC()->cart->get_cart() as $cart_item ) {
				if ( drop_print_cart_item_is_drop_print_item( $cart_item ) ) {
					$has_drop_print_items = true;
					break;
				}
			}
		}
		$css_rel  = 'assets/css/checkout-shipping-tiers.css';
		$js_rel   = 'assets/js/checkout-shipping-tiers.js';
		$css_path = DROP_PRINT_PLUGIN_PATH . $css_rel;
		$js_path  = DROP_PRINT_PLUGIN_PATH . $js_rel;
		wp_enqueue_style(
			'drop-print-checkout-shipping-tiers',
			DROP_PRINT_PLUGIN_URL . $css_rel,
			array(),
			file_exists( $css_path ) ? (string) filemtime( $css_path ) : DROP_PRINT_VERSION
		);
		wp_enqueue_script(
			'drop-print-checkout-shipping-tiers',
			DROP_PRINT_PLUGIN_URL . $js_rel,
			array( 'jquery' ),
			file_exists( $js_path ) ? (string) filemtime( $js_path ) : DROP_PRINT_VERSION,
			true
		);
		wp_add_inline_script(
			'drop-print-checkout-shipping-tiers',
			'window.dropPrintHasItems = ' . ( $has_drop_print_items ? 'true' : 'false' ) . ';'
			. 'window.dropPrintPlaceOrderBlockedText = ' . wp_json_encode( $place_order_blocked_text ) . ';'
			. 'window.dropPrintWarmTiers = ' . wp_json_encode( array(
				'url'   => admin_url( 'admin-ajax.php' ),
				'nonce' => wp_create_nonce( 'drop_print_warm_shipping_tiers' ),
			) ) . ';',
			'before'
		);
	}
}
add_action( 'wp_enqueue_scripts', 'drop_print_checkout_shipping_tiers_assets_enqueue' );
