<?php
/**
 * Magento 2 Google Analytics 4 (GA4) GTM
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Anowave license that is
 * available through the world-wide-web at this URL:
 * https://www.anowave.com/license-agreement/
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category 	Anowave
 * @package 	Anowave_Ec
 * @copyright 	Copyright (c) 2026 Anowave (https://www.anowave.com/)
 * @license  	https://www.anowave.com/license-agreement/
 */

namespace Anowave\Ec\Plugin\Tab;

use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Product;

class Front
{
	/**
	 * @var AttributeFactory
	 */
	protected AttributeRepositoryInterface $attributeRepositoryInterface;

	protected RequestInterface $requestInterface;
	
	/**
	 * Construstor 
	 * 
	 * @param AttributeRepositoryInterface $attributeFactory
	 */
	public function __construct
	(
		AttributeRepositoryInterface $attributeRepositoryInterface,
		RequestInterface $requestInterface
	) 
	{
		$this->attributeRepositoryInterface = $attributeRepositoryInterface;
		$this->requestInterface = $requestInterface;
	}
	
	/**
	 * Get form HTML 
	 * 
	 * @param \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject
	 * @param \Closure $proceed
	 * @return unknown
	 */
	public function aroundGetFormHtml
	(
		\Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
		\Closure $proceed
	)
	{
		$attribute_id = (int) $this->requestInterface->getParam('attribute_id');

		if (!$attribute_id)
		{
			return $proceed();
		}

		$attribute = $this->attributeRepositoryInterface->get(Product::ENTITY, $attribute_id);

		$fieldset = $subject->getForm()->getElement('front_fieldset');

		$events = [\Anowave\Ec\Helper\Constants::EVENT_ADD_TO_CART];
		
		$fieldset->addField
		(
			'datalayer', 'select',
			[
				'name' 		=> 'datalayer',
				'label' 	=> __('Add in dataLayer[]'),
				'title' 	=> __('Add in dataLayer[]'),
				'note' 		=> __('Add this attribute to dataLayer[] object in events such as ' . join(chr(44), $events)),
				'values' 	=> 
				[
					0 	=> __('No'),
					1 	=> __('Yes')
				],
				'value' => (int) $attribute->getData('datalayer')
			]
		);

		return $proceed();
	}
}