<?php

namespace Inspire_Labs\Alsendo_Connect;

use WC_Admin_Settings;
use WC_Settings_Page;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Global_Settings_Integration
 */
class Global_Settings_Integration extends WC_Settings_Page {

	/**
	 * Static array to prevent duplicate processing of items.
	 *
	 * @var array
	 */
	static $prevent_duplicate = array();

	/**
	 * Static flag indicating if creation process has been completed.
	 *
	 * @var bool
	 */
	static $created = false;

	/**
	 * @var Global_Settings
	 */
	private $config;

	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct();
		$this->id     = 'alsendo_connect_settings_general';
		$this->label  = esc_html__( 'Alsendo', 'alsendo-connect' );
		$this->config = new Global_Settings();

		if ( ! empty( $_POST['alsendo_connect_remove_parcel_template'] ) || ! empty( $_POST['alsendo_connect_remove_sender_template'] ) ) {
			check_ajax_referer( 'alsendo_connect_nonce_param', 'alsendo_connect_settings_nonce' );
			$this->remove_sender_template();
			$this->remove_parcel_template();
		}

		if ( ! self::$created ) {
			add_filter(
				'woocommerce_settings_tabs_array',
				array( $this, 'add_settings_page' ),
				20
			);
			add_action(
				'woocommerce_settings_' . $this->id,
				array( $this, 'output' )
			);
			add_action(
				'woocommerce_settings_save_' . $this->id,
				array( $this, 'save' )
			);
		}
	}


	/**
	 * Save
	 */
	public function save() {

		if ( ! isset( $_POST['alsendo_connect_settings_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_nonce'] ) ), 'alsendo_connect_nonce_param' ) ) {
			wp_die( 'Alsendo Connect: bad nonce' );
		}

		$settings = $this->get_settings();

		$this->create_templates( $settings );

		$api_type = alsendo_connect()->get_main_settings_api();
		if ( 'apaczka' === $api_type ) {
			// refresh Apaczka Services on save settings action.
			$services = ( new Alsendo_API() )->get_service_structure_apaczka( true );
		}
	}

	/**
	 * Create templates
	 *
	 * @param $settings $settings.
	 *
	 * @return void
	 */
	private function create_templates( $settings ) {

		$should_create_package_template = ! empty( $_POST['alsendo_connect_settings_general_create_package_template'] ) &&
										'yes' === sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_create_package_template'] ) );

		$should_create_sender_template = ! empty( $_POST['alsendo_connect_settings_general_create_sender_template'] ) &&
											'yes' === sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_create_sender_template'] ) );

		$detect_sender_section    = Plugin::APP_PREFIX . '_settings_general_sender_';
		$submitted_sender_values  = array();
		$submitted_package_values = array();

		$new_sender_template_name  = null;
		$new_package_template_name = null;

		$package_ids = array(
			$this->config->get_setting_id( 'dispath_point_ups' ),
			$this->config->get_setting_id( 'dispath_point_kurier48' ),
			$this->config->get_setting_id( 'dispath_point_inpost' ),
			$this->config->get_setting_id( 'dispath_point_dpd' ),
			$this->config->get_setting_id( 'dispath_point_dhl' ),
			$this->config->get_setting_id( 'dispath_point_gls' ),
			$this->config->get_setting_id( 'dispath_point_cargus' ),
			$this->config->get_setting_id( 'dispath_point_fan_courier' ),
			$this->config->get_setting_id( 'dispath_point_sameday' ),
			$this->config->get_setting_id( 'pickup_hour_to' ),
			$this->config->get_setting_id( 'pickup_hour_from' ),
			$this->config->get_setting_id( 'shipping_method' ),
			$this->config->get_setting_id( 'declared_content' ),
			$this->config->get_setting_id( 'declared_content_auto' ),
			$this->config->get_setting_id( 'set_order_status_completed' ),
			$this->config->get_setting_id( 'package_contents' ),
			$this->config->get_setting_id( 'custom_text_content' ),
			$this->config->get_setting_id( 'package_weight' ),
			$this->config->get_setting_id( 'package_height' ),
			$this->config->get_setting_id( 'package_length' ),
			$this->config->get_setting_id( 'package_width' ),
			$this->config->get_setting_id( 'is_nstd' ),
			$this->config->get_setting_id( 'parcel_type' ),
			$this->config->get_setting_id( 'service' ),
			$this->config->get_setting_id( 'cod' ),
		);

		foreach ( $settings as $k => $option ) {
			if ( isset( $option['id'] )
				&& $option['id'] === $this->config->get_setting_id( 'create_sender_template' ) ) {
				unset( $settings[ $k ] );
			}

			if ( isset( $option['id'] )
				&& $option['id'] === $this->config->get_setting_id( 'create_package_template' ) ) {
				unset( $settings[ $k ] );
			}
		}

		add_filter(
			'woocommerce_admin_settings_sanitize_option',
			function (
				$value,
				$option,
				$raw_value
			) use (
				&$submitted_sender_values,
				&$submitted_package_values,
				$detect_sender_section,
				&$new_sender_template_name,
				&$new_package_template_name,
				$package_ids
			) {
				if ( strpos(
					$option['id'],
					$detect_sender_section
				) === 0 ) {
					$submitted_sender_values[ $option['id'] ] = $value;
				}

				if ( ! $new_sender_template_name
					&& $option['id'] === $this->config->get_setting_id( 'new_sender_template_name' ) ) {
					$new_sender_template_name = $value;
				}

				if ( in_array( $option['id'], $package_ids ) ) {
					$submitted_package_values[ $option['id'] ] = $value;
				}

				if ( ! $new_package_template_name
					&& $option['id'] === $this->config->get_setting_id( 'new_package_template_name' ) ) {
					$new_package_template_name = $value;
				}

				return $value;
			},
			3,
			100
		);

		WC_Admin_Settings::save_fields( $settings );

		if ( $should_create_sender_template ) {
			if ( $new_sender_template_name ) {
				( new Sender_Settings_Templates_Helper() )->create(
					$submitted_sender_values,
					$new_sender_template_name
				);
				if ( ! empty( $new_sender_template_name ) ) {
					update_option(
						'alsendo_connect_settings_general_select_sender_template',
						sanitize_title( $new_sender_template_name )
					);
				}
			}
		} else {
			$this->maybe_update_sender_template_name( $submitted_sender_values );
		}

		if ( $should_create_package_template ) {
			if ( $new_package_template_name ) {
				( new Gateway_Settings_Templates_Helper() )->create(
					$submitted_package_values,
					$new_package_template_name
				);

				if ( ! empty( $new_package_template_name ) ) {
					update_option(
						'alsendo_connect_settings_general_select_package_template',
						sanitize_title( $new_package_template_name )
					);
				}
			}
		} else {
			// only update template values and maybe name of the template.
			$this->maybe_update_package_template_name( $submitted_package_values );
		}
	}


	/**
	 * Get sections.
	 *
	 * @return array
	 */
	public function get_sections() {
		$sections = array(
			''                  => esc_html__( 'API Settings', 'alsendo-connect' ),
			'sender_templates'  => esc_html__( 'Sender templates', 'alsendo-connect' ),
			'package_templates' => esc_html__( 'Package templates', 'alsendo-connect' ),
		);

		return apply_filters(
			'woocommerce_get_sections_' . $this->id,
			$sections
		);
	}

	/**
	 * Output
	 */
	public function output() {

		static $settings_generated = false;

		global $current_section;

		if ( ! $settings_generated ) {

			if ( 'sender_templates' === $current_section ) {
				include_once ALSENDO_CONNECT_PLUGIN_PATH . '/templates/settings/sender_templates.php';
			} elseif ( 'package_templates' === $current_section ) {
				include_once ALSENDO_CONNECT_PLUGIN_PATH . '/templates/settings/package_templates.php';
			} else {
				$settings = $this->get_settings();
				WC_Admin_Settings::output_fields( $settings );
			}

			$settings_generated = true;
		}
	}




	/**
	 * Get option
	 *
	 * @param string $key $key.
	 *
	 * @return false|mixed|void
	 */
	public function get_option( string $key ) {
		return get_option( $this->config->get_setting_id( $key ) );
	}

	public function get_settings(): array {

		$return = $this->config->get_api_settings();

		if ( $this->api_keys_exists() ) {

			$api_type = alsendo_connect()->get_main_settings_api();

			if ( 'apaczka' === $api_type ) {

				$services = ( new Alsendo_API() )->get_service_structure_apaczka();

				if ( ! empty( $services ) ) {
					$return = array_merge(
						$return,
						$this->config->get_sender_settings()
					);
					//$return[] = $this->sender_template_support();

					$return = array_merge(
						$return,
						$this->config->get_parcel_settings( $services )
					);
					//$return[] = $this->get_manage_package_templates_btn();
				}
			}

			if ( 'ecolet' === $api_type ) {

				$ecolet_services = array();
				$ecolet          = new Ecolet();
				$ecolet_services = $ecolet->get_service_structure_ecolet();


				if ( ! empty( $ecolet_services ) && is_array( $ecolet_services ) ) {
					$return = array_merge(
						$return,
						$this->config->get_sender_settings()
					);

					$return = array_merge(
						$return,
						$this->config->get_parcel_settings( $ecolet_services )
					);
				}
			}

			if ( 'zaslat' === $api_type ) {

				if ( ! empty( $_POST['alsendo_connect_settings_general_global_api_type'] )
					&& 'zaslat' === sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_global_api_type'] ) )
						&& ! empty( $_POST['save'] )
				) {
					$zaslat = new Zaslat();
					$res    = $zaslat->get_address_book( 1 );
				}

				/*$zaslat_address_book = array();
				if ( ! empty( $res['data'] ) && is_array( $res['data'] ) ) {
					$zaslat_address_book = $res['data'];
				}*/

				$zaslat_services = array(
					''           => esc_html__( 'Select operator', 'alsendo-connect' ),
					'BALIKOVNA'  => 'BALIKOVNA',
					'ZASILKOVNA' => 'ZASILKOVNA',
					'GLS'        => 'GLS',
					'DPD'        => 'DPD',
					'PPL'        => 'PPL',
					'ONE'        => 'WEDO/One by Allegro',
					'TOPTRANS'   => 'TOPTRANS',
					'UPS'        => 'UPS',
					'FEDEX'      => 'FEDEX',
				);

				/*$return = array_merge(
					$return,
					$this->config->get_zaslat_sender_settings( $zaslat_address_book )
				);*/

				$return[] = array(
					'title'       => esc_html__( 'Other settings', 'alsendo-connect' ),
					'id'          => 'other_zaslat_settings',
					'type'        => 'title',
					'description' => '',
				);

				$return[] = array(
					'id'                       => $this->config->get_setting_id( 'declared_content_auto' ),
					'title'                    => esc_html__(
						'Automatically complete the "Declaration of value" with the value of the order',
						'alsendo-connect'
					),
					'type'                     => 'checkbox',
					'label'                    => '',
					'default'                  => 'yes',
					'visible_on_order_details' => true,
				);

				$return[] = array(
					'id'                       => $this->config->get_setting_id( 'set_order_status_completed' ),
					'title'                    => esc_html__(
						'Change order status to Completed after shipment created?',
						'alsendo-connect'
					),
					'type'                     => 'checkbox',
					'label'                    => '',
					'default'                  => 'yes',
					'visible_on_order_details' => true,
					'custom_attributes'        => array(
						'checked' => 'checked',
					),
				);

				$return[] = array(
					'id'                       => $this->config->get_setting_id( 'map_debug_mode' ),
					'title'                    => esc_html__( 'An alternative way to show the map button', 'alsendo-connect' ),
					'type'                     => 'checkbox',
					'label'                    => '',
					'default'                  => 'no',
					'visible_on_order_details' => false,
				);

				$return[] = array(
					'id'   => 'other_zaslat_settings',
					'type' => 'sectionend',
				);

				$return = array_merge(
					$return,
					$this->config->get_parcel_settings( $zaslat_services )
				);

				//$return[] = $this->package_template_support();

			}
		} else {

			update_option(
				'alsendo_connect_apaczka_service_structure_timestamp',
				0
			);
			delete_option( 'alsendo_connect_apaczka_service_structure' );
		}

		$return = array_merge(
			$return,
			$this->config->get_debug_settings()
		);

		self::$created = true;

		return $return;
	}


	/**
	 * Check if api keys exist
	 *
	 * @return mixed
	 */
	private function api_keys_exists() {

		$apaczka_app_id     = get_option( $this->config->get_setting_id( 'app_id' ) );
		$apaczka_app_secret = get_option( $this->config->get_setting_id( 'app_secret' ) );

		if ( ! empty( $apaczka_app_id ) && ! empty( $apaczka_app_secret ) ) {
			return true;
		}

		$ecolet_email         = get_option( 'alsendo_connect_settings_general_ecolet_email' );
		$ecolet_password      = get_option( 'alsendo_connect_settings_general_ecolet_password' );
		$ecolet_client_id     = get_option( 'alsendo_connect_settings_general_ecolet_client_id' );
		$ecolet_client_secret = get_option( 'alsendo_connect_settings_general_ecolet_app_secret' );

		if ( ! empty( $ecolet_email ) && ! empty( $ecolet_password ) && ! empty( $ecolet_client_id ) && ! empty( $ecolet_client_secret ) ) {

			return true;

		}

		$zaslat_api_key = get_option( 'alsendo_connect_settings_general_zaslat_api_key' );
		if ( ! empty( $zaslat_api_key ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Sender template support
	 *
	 * @return array[]
	 */
	private function sender_template_support(): array {

		$custom_button_id = $this->config->get_setting_id( 'load_from_sender_template_btn_fieldset' );

		add_action(
			'woocommerce_admin_field_' . $custom_button_id,
			function ( array $data ) use ( $custom_button_id ) {

				$type = $data['type'];
				if ( isset( self::$prevent_duplicate[ $type ] )
					&& self::$prevent_duplicate[ $type ]
				) {
					return;
				}
				if ( $custom_button_id === $type ) {
					?>
					<button id="alsendo_connect_load_sender_settings_btn"
							class='alsendo_connect_load_sender_settings_btn'>
						<?php
						esc_html_e(
							'Get sender data from selected template',
							'alsendo-connect'
						)
						?>
							</button>

					<button
							id="alsendo_connect_remove_sender_template_btn"
							class="button-primary woocommerce-save-button">
							<?php
							esc_html_e(
								'Remove selected sender template',
								'alsendo-connect'
							)
							?>
					</button>

					<input name="alsendo_connect_remove_sender_template_slug"
							type="hidden"
							id="alsendo_connect_remove_sender_template_slug">

					<input name="alsendo_connect_remove_sender_template"
							type="hidden"
							id="alsendo_connect_remove_sender_template">

					<?php
				}
				self::$prevent_duplicate[ $type ] = true;
			},
			10
		);

		return array(

			'title'       => esc_html__(
				'Load from sender template',
				'alsendo-connect'
			),
			'type'        => $custom_button_id,
			'id'          => $this->config->get_setting_id( 'load_from_sender_template' ),
			'description' => '',
			'default'     => '',

		);
	}

	/**
	 * Package template support
	 *
	 * @return array[]
	 */
	private function package_template_support(): array {

		$custom_button_id = $this->config->get_setting_id( 'load_from_package_template_btn_fieldset' );

		add_action(
			'woocommerce_admin_field_' . $custom_button_id,
			function ( array $data ) use ( $custom_button_id ) {
				$type = $data['type'];
				if ( isset( self::$prevent_duplicate[ $type ] )
					&& self::$prevent_duplicate[ $type ]
				) {
					return;
				}
				if ( $custom_button_id === $type ) {
					?>
					<div class="tamplate_buttons-wrapper">
						<label class='alsendo_connect_load_package_settings_label'
								for="alsendo_connect_load_package_settings_btn"></label>
						<button id="alsendo_connect_load_package_settings_btn"
								class='alsendo_connect_load_package_settings_btn'>
							<?php
							esc_html_e(
								'Get package data from selected template',
								'alsendo-connect'
							)
							?>
						</button>

						<button name="alsendo_connect_remove_parcel_template"
								id="alsendo_connect_remove_parcel_template_btn"
								class="alsendo_connect_remove_parcel_template_btn">
							<?php
								esc_html_e(
									'Remove selected parcel template',
									'alsendo-connect'
								)
							?>
						</button>
						<input name="alsendo_connect_remove_parcel_template_slug"
								type="hidden"
								id="alsendo_connect_remove_parcel_template_slug">
						<input name="alsendo_connect_remove_parcel_template"
								type="hidden"
								id="alsendo_connect_remove_parcel_template">

					</div>
					<?php
				}
				self::$prevent_duplicate[ $type ] = true;
			},
			10
		);

		return array(

			'title'       => esc_html__(
				'Load from package template',
				'alsendo-connect'
			),
			'type'        => $custom_button_id,
			'id'          => $this->config->get_setting_id( 'load_from_package_template' ),
			'description' => '',
			'default'     => '',

		);
	}

	/**
	 * Remove parcel template
	 *
	 * @return void
	 */
	private function remove_parcel_template() {

		check_ajax_referer( 'alsendo_connect_nonce_param', 'alsendo_connect_settings_nonce' );

		if ( ! empty( $_POST['alsendo_connect_remove_parcel_template'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['alsendo_connect_remove_parcel_template'] ) ) ) {

			if ( ! empty( $_POST['alsendo_connect_remove_parcel_template_slug'] ) ) {
				$template_slug = sanitize_text_field( wp_unslash( $_POST['alsendo_connect_remove_parcel_template_slug'] ) );

				( new Gateway_Settings_Templates_Helper() )->remove_by_slug( $template_slug );
				( new Alerts() )->add_notice( esc_html__( 'Alsendo Connect: Template was deleted', 'alsendo-connect' ) );
			}
		}
	}

	/**
	 * Remove sender template
	 *
	 * @return void
	 */
	private function remove_sender_template() {

		check_ajax_referer( 'alsendo_connect_nonce_param', 'alsendo_connect_settings_nonce' );

		if ( ! empty( $_POST['alsendo_connect_remove_sender_template'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['alsendo_connect_remove_sender_template'] ) ) ) {

			if ( ! empty( $_POST['alsendo_connect_remove_sender_template_slug'] ) ) {
				$template_slug = sanitize_text_field( wp_unslash( $_POST['alsendo_connect_remove_sender_template_slug'] ) );

				( new Sender_Settings_Templates_Helper() )
					->remove_by_slug(
						$template_slug
					);
				( new Alerts() )->add_notice(
					esc_html__(
						'Template was deleted',
						'alsendo-connect'
					)
				);
			}
		}
	}


	/**
	 * Updates package template name if a new name is provided via AJAX request.
	 *
	 * Validates nonce, sanitizes input data, and updates template name and option
	 * if the new name differs from current name.
	 *
	 * @param array $submitted_package_values $submitted_package_values.
	 * @uses check_ajax_referer()
	 * @uses Gateway_Settings_Templates_Helper
	 * @uses update_option()
	 *
	 * @return void
	 *
	 * @access private
	 * @since 1.0.0
	 */
	private function maybe_update_package_template_name( $submitted_package_values ) {

		check_ajax_referer( 'alsendo_connect_nonce_param', 'alsendo_connect_settings_nonce' );

		$current_package_template_slug = ! empty( $_POST['alsendo_connect_settings_general_select_package_template'] )
			? sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_select_package_template'] ) )
			: null;

		$maybe_new_package_template_name = ! empty( $_POST['alsendo_connect_settings_general_new_package_template_name'] )
			? sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_new_package_template_name'] ) )
			: null;

		if ( ! empty( $current_package_template_slug ) && ! empty( $maybe_new_package_template_name ) ) {

			$current_package_template_name = ( new Gateway_Settings_Templates_Helper() )->get_template_name_by_template_slug( $current_package_template_slug );
			if ( ! empty( $current_package_template_name ) ) {

				$current_package_template_name   = trim( $current_package_template_name );
				$maybe_new_package_template_name = trim( $maybe_new_package_template_name );

				if ( $current_package_template_name !== $maybe_new_package_template_name ) {

					$new_slug = ( new Gateway_Settings_Templates_Helper() )->rename_template(
						$current_package_template_slug,
						$maybe_new_package_template_name
					);

					if ( ! empty( $new_slug ) ) {
						( new Gateway_Settings_Templates_Helper() )->update_template(
							$submitted_package_values,
							$new_slug
						);
					}

					update_option(
						'alsendo_connect_settings_general_select_package_template',
						sanitize_title( $maybe_new_package_template_name )
					);

				} else {
					if ( ! empty( $current_package_template_slug ) ) {
						( new Gateway_Settings_Templates_Helper() )->update_template(
							$submitted_package_values,
							$current_package_template_slug
						);
					}
				}
			}
		}
	}



	/**
	 * Updates sender template name if a new name is provided via AJAX request.
	 *
	 * Validates nonce, sanitizes input data, and updates template name and option
	 * if the new name differs from current name.
	 *
	 * @param array $submitted_sender_values $submitted_sender_values.
	 * @uses check_ajax_referer()
	 * @uses Sender_Settings_Templates_Helper
	 * @uses update_option()
	 *
	 * @return void
	 *
	 * @access private
	 * @since 1.0.0
	 */
	private function maybe_update_sender_template_name( $submitted_sender_values ) {

		check_ajax_referer( 'alsendo_connect_nonce_param', 'alsendo_connect_settings_nonce' );

		$current_sender_template_slug = ! empty( $_POST['alsendo_connect_settings_general_select_sender_template'] )
			? sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_select_sender_template'] ) )
			: null;

		$maybe_new_sender_template_name = ! empty( $_POST['alsendo_connect_settings_general_new_sender_template_name'] )
			? sanitize_text_field( wp_unslash( $_POST['alsendo_connect_settings_general_new_sender_template_name'] ) )
			: null;

		if ( ! empty( $current_sender_template_slug ) && ! empty( $maybe_new_sender_template_name ) ) {
			$current_sender_template_name = ( new Sender_Settings_Templates_Helper() )->get_template_name_by_template_slug( $current_sender_template_slug );
			if ( ! empty( $current_sender_template_name ) ) {
				$current_sender_template_name   = trim( $current_sender_template_name );
				$maybe_new_sender_template_name = trim( $maybe_new_sender_template_name );

				if ( $current_sender_template_name !== $maybe_new_sender_template_name ) {
					$new_slug = ( new Sender_Settings_Templates_Helper() )->rename_template(
						$current_sender_template_slug,
						$maybe_new_sender_template_name
					);

					if ( ! empty( $new_slug ) ) {
						( new Sender_Settings_Templates_Helper() )->update_template(
							$submitted_sender_values,
							$new_slug
						);
					}

					update_option(
						'alsendo_connect_settings_general_select_sender_template',
						sanitize_title( $maybe_new_sender_template_name )
					);
				} else {
					if ( ! empty( $current_sender_template_slug ) ) {
						( new Sender_Settings_Templates_Helper() )->update_template(
							$submitted_sender_values,
							$current_sender_template_slug
						);
					}
				}
			}
		}
	}



	/**
	 * Package template support
	 *
	 * @return array[]
	 */
	public function get_manage_package_templates_btn(): array {

		$api_type = alsendo_connect()->get_main_settings_api();

		if ( empty( $api_type ) /*|| 'zaslat' === $api_type*/ ) {
			return array();
		}

		$custom_button_id = $this->config->get_setting_id( 'redirect_to_package_templates_management' );

		add_action(
			'woocommerce_admin_field_' . $custom_button_id,
			function ( array $data ) use ( $custom_button_id, $api_type ) {
				$type = $data['type'];
				if ( isset( self::$prevent_duplicate[ $type ] )
					&& self::$prevent_duplicate[ $type ]
				) {
					return;
				}
				if ( $custom_button_id === $type ) {
					$manage_package_templates_link = admin_url( 'admin.php?page=wc-settings&tab=alsendo_connect_settings_general&section=package_templates' );
					$manage_sender_templates_link  = admin_url( 'admin.php?page=wc-settings&tab=alsendo_connect_settings_general&section=sender_templates' );
					?>
						<div class="template_buttons-wrapper">
							<a id="alsendo_connect_package_templates_management_btn" href="<?php echo esc_url( $manage_package_templates_link ); ?>"
									class='alsendo_connect_package_templates_management_btn button-secondary'>
								<?php
								esc_html_e(
									'Manage package templates',
									'alsendo-connect'
								)
								?>
							</a>
							<?php if ( 'zaslat' !== $api_type ) { ?>
								<a id="alsendo_connect_sender_templates_management_btn" href="<?php echo esc_url( $manage_sender_templates_link ); ?>"
									class='alsendo_connect_sender_templates_management_btn button-secondary'>
									<?php
									esc_html_e(
										'Manage sender templates',
										'alsendo-connect'
									)
									?>
								</a>
						<?php } ?>
						</div>
						<?php
				}
				self::$prevent_duplicate[ $type ] = true;
			},
			10
		);

		return array(

			'title'       => esc_html__(
				'Manage package templates',
				'alsendo-connect'
			),
			'type'        => $custom_button_id,
			'id'          => $this->config->get_setting_id( 'package_templates_management' ),
			'description' => '',
			'default'     => '',

		);
	}
}
