<?php
/**
 * WooCommerce product tab.
 *
 * @package flo-sync
 */

namespace NMFS\Products\Product_Fields;

use NMFS\Shared;
use WC_Product_Factory;

defined( 'ABSPATH' ) || exit;

/**
 * Class Wc_Product_Tab
 */
class Wc_Product_Tab {

	public const PRODUCT_TYPE_VARIABLE = 'variable';

	/**
	 * Construct class.
	 */
	public function __construct() {
		add_filter( 'woocommerce_product_data_tabs', array( &$this, 'register_tabs' ) );
		add_action( 'admin_head', array( &$this, 'mf_styles' ) );
	}

	/**
	 * Register new tab WooCommerce product data tab.
	 *
	 * @param array $tabs Default tabs.
	 *
	 * @return array
	 */
	public function register_tabs( array $tabs ): array {
		global $post;
		$product_type = WC_Product_Factory::get_product_type( $post->ID );

		if ( self::PRODUCT_TYPE_VARIABLE !== $product_type ) {
			$custom_product_tab['mf_tab'] = array(
				'label'  => __( 'Northmill Sync', 'flo-sync' ),
				'target' => 'mf_tab',
				'class'  => array(),
			);

			$insert_at_position = 7;
			$updated_tabs       = array_slice( $tabs, 0, $insert_at_position, true );
			$updated_tabs       = array_merge( $updated_tabs, $custom_product_tab );
			$updated_tabs       = array_merge( $updated_tabs, array_slice( $tabs, $insert_at_position, null, true ) );

			$tabs = $updated_tabs;
		}

		$tabs['inventory']['class'][] = 'show_if_booking';

		return $tabs;
	}

	/**
	 * Add styles to admin head.
	 */
	public function mf_styles(): void {
		?>
		<style>
			#woocommerce-product-data ul.wc-tabs li.mf_tab_options.mf_tab_tab a:before {
				background: url(<?php echo esc_url( Shared::instance()->assets->get_shared_img_url( 'nm-logo.png' ) ); ?>) center center no-repeat;
				content: " " !important;
				background-size: 100%;
				width: 13px;
				height: 13px;
				display: inline-block;
				line-height: 1;
			}
		</style>
		<?php
	}
}
