<?php

	if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly



	use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;

	const Irem_VERSION = '0.1.0';

	/**
	 * Class for integrating with WooCommerce Blocks
	 */
	class Korkmaz_Blocks_Integration implements IntegrationInterface {

		/**
		 * The name of the integration.
		 *
		 * @return string
		 */
		public function get_name() {
			return 'irem';
		}

		/**
		 * When called invokes any initialization/setup for the integration.
		 */
		public function initialize() {
			$this->register_newsletter_block_frontend_scripts();
			$this->register_newsletter_block_editor_scripts();
			$this->register_newsletter_block_editor_styles();
			$this->register_main_integration();
		}

		/**
		 * Register the FRONTEND script for the block (irem-block-frontend.js).
		 */
		public function register_newsletter_block_frontend_scripts() {
			// Bir üst dizine çıkıp build klasöründeki JS dosyasına giden URL:
			$script_url = plugins_url( '../build/irem-block-frontend.js', __FILE__ );
			// PHP tarafında asset bilgisi için path:
			$script_asset_path = plugin_dir_path( __FILE__ ) . '../build/irem-block-frontend.asset.php';

			$script_asset = file_exists( $script_asset_path )
				? require $script_asset_path
				: array(
					'dependencies' => array(),
					'version'      => $this->get_file_version( $script_asset_path ),
				);

			wp_register_script(
				'irem-block-frontend',
				$script_url,
				$script_asset['dependencies'],
				$script_asset['version'],
				true
			);

			wp_set_script_translations(
				'irem-block-frontend',
				'wc-korkmaz-contract',
				plugin_dir_path( dirname( __FILE__ ) ) . 'languages'
			);
		}

		/**
		 * Get the file modified time as a cache buster if we're in dev mode.
		 *
		 * @param string $file Local path to the file.
		 * @return string The cache buster value to use for the given file.
		 */
		protected function get_file_version( $file ) {
			if ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG && file_exists( $file ) ) {
				return filemtime( $file );
			}
			return Irem_VERSION;
		}

		/**
		 * Register the EDITOR script for the block (irem-block.js).
		 */
		public function register_newsletter_block_editor_scripts() {
			$script_url = plugins_url( '../build/irem-block.js', __FILE__ );
			$script_asset_path = plugin_dir_path( __FILE__ ) . '../build/irem-block.asset.php';

			$script_asset = file_exists( $script_asset_path )
				? require $script_asset_path
				: array(
					'dependencies' => array(),
					'version'      => $this->get_file_version( $script_asset_path ),
				);

			wp_register_script(
				'irem-block-editor',
				$script_url,
				$script_asset['dependencies'],
				$script_asset['version'],
				true
			);

			wp_set_script_translations(
				'irem-block-editor',
				'wc-korkmaz-contract',
				plugin_dir_path( dirname( __FILE__ ) ) . 'languages'
			);
		}

		/**
		 * Register editor styles (style-irem-block.css).
		 */
		public function register_newsletter_block_editor_styles() {
			$style_url = plugins_url( '../build/style-irem-block.css', __FILE__ );
			$style_path = plugin_dir_path( __FILE__ ) . '../build/style-irem-block.css';

			wp_register_style(
				'irem-block',
				$style_url,
				array(),
				$this->get_file_version( $style_path )
			);
		}

		/**
		 * Registers the main JS file required to add filters and Slot/Fills (index.js).
		 */
		public function register_main_integration() {
			// URL: index.js
			$script_url = plugins_url( '../build/index.js', __FILE__ );
			// Path: index.asset.php
			$script_asset_path = plugin_dir_path( __FILE__ ) . '../build/index.asset.php';

			$script_asset = file_exists( $script_asset_path )
				? require $script_asset_path
				: array(
					'dependencies' => array(),
					'version'      => '1.0.0',
				);

			wp_register_script(
				'irem-blocks-integration',
				$script_url,
				$script_asset['dependencies'],
				$script_asset['version'],
				true
			);

			wp_set_script_translations(
				'irem-blocks-integration',
				'wc-korkmaz-contract',
				plugin_dir_path( dirname( __FILE__ ) ) . 'languages'
			);
		}

		/**
		 * Returns an array of script handles to enqueue in the frontend context.
		 *
		 * @return string[]
		 */
		public function get_script_handles() {
			// Hem 'index.js' (irem-blocks-integration)
			// hem de 'irem-block-frontend.js' scriptlerini front-end'e ekleyeceğiz.
			return array( 'irem-blocks-integration', 'irem-block-frontend' );
		}

		/**
		 * Returns an array of script handles to enqueue in the editor context.
		 *
		 * @return string[]
		 */
		public function get_editor_script_handles() {
			// Editor'de 'irem-block-editor' ve 'irem-blocks-integration' lazım.
			return array( 'irem-blocks-integration', 'irem-block-editor' );
		}

		/**
		 * An array of key-value pairs of data made available to the block on the client side.
		 */
		public function get_script_data() {
			$example_data       = did_action( 'init' ) ? __( 'This is some example data from the server', 'wc-korkmaz-contract' ) : 'This is some example data from the server';
			$opt_in_default_text = did_action( 'init' ) ? __( 'I want to receive updates about products and promotions.', 'wc-korkmaz-contract' ) : 'I want to receive updates about products and promotions.';

			$data = array(
				'irem-active' => true,
				'example-data' => $example_data,
				'optInDefaultText' => $opt_in_default_text,
				'gsozlesmebir' => wp_kses_post( stripslashes( get_option( 'birinci_sozlesme_metni', '' ) ) ),
				'gsozlesmeiki' => wp_kses_post( stripslashes( get_option( 'ikinci_sozlesme_metni', '' ) ) ),
				'firmaadi' => sanitize_text_field( get_option( 'firmaadi', '' ) ),
				'firmaadresi' => sanitize_text_field( get_option( 'firmaadresi', '' ) ),
				'firmatelno' => sanitize_text_field( get_option( 'firmatelno', '' ) ),
				'firmaverdaire' => sanitize_text_field( get_option( 'firmaverdaire', '' ) ),
				'firmaverno' => sanitize_text_field( get_option( 'firmaverno', '' ) ),
				'birinci_sozlesme_link_ismi' => sanitize_text_field( get_option( 'birinci_sozlesme_link_ismi', '' ) ),
				've_metin' => sanitize_text_field( get_option( 've_metin', '' ) ),
				'ikinci_sozlesme_link_ismi' => sanitize_text_field( get_option( 'ikinci_sozlesme_link_ismi', '' ) ),
				'son_metin' => sanitize_text_field( get_option( 'son_metin', '' ) ),
				'sozlesme_ozellik_3' => (int) get_option( 'sozlesme_ozellik_3', 0 ),
				'sozlesme_ozellik_1' => (int) get_option( 'sozlesme_ozellik_1', 0 ),
				'sozlesme_ozellik_2' => (int) get_option( 'sozlesme_ozellik_2', 0 ),
				'sozlesme_ozellik_5' => (int) get_option( 'sozlesme_ozellik_5', 0 ),
				'sozlesme_ozellik_6' => (int) get_option( 'sozlesme_ozellik_6', 0 ),
				'sozlesme_ozellik_4' => (int) get_option( 'sozlesme_ozellik_4', 0 ),
			);

			return $data;
		}
	}
