<?php
/**
 * @package     Splitit_WooCommerce_Plugin
 *
 * File - class-splitit-flexfields-payment-plugin-settings.php
 * Class for plugin settings
 */

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

/**
 * Class SplitIt_FlexFields_Payment_Plugin_Settings
 */
class SplitIt_FlexFields_Payment_Plugin_Settings {
	/**
	 * Return fields for plugin settings page
	 *
	 * @param array $settings Settings.
	 *
	 * @return array
	 */
	public static function get_fields( $settings ) {
		return array(
			'splitit_merchant_login'                  => array(
				'title' => '',
			),

			'general_setting_section'                 => array(
				'logout_button'       => array(
					'title' => get_option( 'api_key' ) ? '<button type="button" class="login-button" id="merchant_logout">' . __( 'Logout', 'splitit-installment-payments' ) . '</button>' : '<button type="button" class="login-button" id="merchant_login">' . __( 'Login', 'splitit-installment-payments' ) . '</button>',
				),
				'merchant'            => array(
					'title'       => __( 'Merchant', 'splitit-installment-payments' ),
					'description' => self::get_logged_merchant_name(),
					'desc_tip'    => false,
				),
				'terminal'            => array(
					'title'       => __( 'Terminal', 'splitit-installment-payments' ),
					'description' => self::get_logged_merchant_terminal(),
					'desc_tip'    => false,
				),
				'enabled'             => array(
					'title'   => __( 'Enable/Disable Splitit Payments', 'splitit-installment-payments' ),
					'type'    => 'checkbox',
					'label'   => __( 'Enable Payment', 'splitit-installment-payments' ),
					'default' => 'no',
				),
				'splitit_environment' => array(
					'title'             => __( 'Environment', 'splitit-installment-payments' ),
					'description'       => self::get_splitit_environment(),
					'setting_block_css' => 'margin-top: 0; /*display: none;*/',
					'type'              => 'select',
					'desc_tip'          => false,
					'css'               => 'background: #fff url(' . plugins_url( 'assets/img/icon-20-px-triangle-down.svg', __DIR__ ) . ') no-repeat right 5px top 55% !important',
					'options'           => array(
						'sandbox'    => __( 'Sandbox', 'splitit-installment-payments' ),
						'production' => __( 'Production', 'splitit-installment-payments' ),
					),
				),
			),

			'Payment_Method_Settings_section'         => array(
				'title'                                 => __( 'Payment Method Settings', 'splitit-installment-payments' ),
				'description'                           => __( 'Specify the payment options you’d like to offer to your customers.', 'splitit-installment-payments' ),
				'splitit_settings_3d'                   => array(
					'title'           => __( 'Require checkout with 3DS', 'splitit-installment-payments' ),
					'type'            => 'select',
					'options'         => array(
						'1' => 'On',
						'0' => 'Off',
					),
					'default'         => '0',
					'desc_tip'        => true,
					'tip_title'       => __( 'Protect your customers', 'splitit-installment-payments' ),
					'tip_description' => __( 'For extra fraud protection, 3D Secure (3DS) requires customers to complete an additional verification step with the card issuer when paying.', 'splitit-installment-payments' ),
				),
				'splitit_auto_capture'                  => array(
					'title'           => __( 'Auto-Capture', 'splitit-installment-payments' ),
					'type'            => 'select',
					'options'         => array(
						'1' => 'On',
						'0' => 'Off',
					),
					'default'         => '0',
					'desc_tip'        => true,
					'tip_title'       => __( 'Protect your customers', 'splitit-installment-payments' ),
					'tip_description' => __( 'With AutoCapture on, your customer’s first payment will be captured instantly, when their card is authorized, NOT later when their product is shipped.', 'splitit-installment-payments' ),
				),
				'splitit_refund_strategy'               => array(
					'title'       => __( 'Refund Strategy', 'splitit-installment-payments' ),
					'description' => __( 'Choose order refund strategy', 'splitit-installment-payments' ),
					'type'        => 'select',
					'class'       => 'wc-enhanced-select',
					'css'         => 'width: 305px; margin-bottom: 1px;',
					'default'     => '',
					'desc_tip'    => false,
					'options'     => self::refund_strategy_selection(),
				),
				'splitit_default_order_status_title'    => __( 'Default order status', 'splitit-installment-payments' ),
				'splitit_default_order_status_auto_capture_on' => array(
					'title'    => __( 'Auto-Capture is ON', 'splitit-installment-payments' ),
					'type'     => 'select',
					'class'    => 'wc-enhanced-select',
					'css'      => 'width: 305px; margin-top: 0; margin-bottom: 1px;',
					'default'  => '',
					'desc_tip' => false,
					'options'  => self::available_order_statuses_selection(),
				),
				'splitit_default_order_status_auto_capture_off' => array(
					'title'    => __( 'Auto-Capture is OFF', 'splitit-installment-payments' ),
					'type'     => 'select',
					'class'    => 'wc-enhanced-select',
					'css'      => 'width: 305px; margin-top: 0; margin-bottom: 1px;',
					'default'  => '',
					'desc_tip' => false,
					'options'  => self::available_order_statuses_selection(),
				),
				'splitit_inst_conf'                     => self::get_new_installment_fields( $settings ),
				'splitit_upstream_default_installments' => array(
					'title'       => __( 'Choose the number by which you\'d like your customer journey amount to be divided', 'splitit-installment-payments' ),
					'type'        => 'number',
					'description' => __( 'OPTIONAL, We recommend to leave this field empty and we will use the highest value from the ranges you\'ll set above', 'splitit-installment-payments' ),
				),
			),

			'splitit_product_option'                  => array(
				'title'   => __( 'Enable Splitit per product', 'splitit-installment-payments' ),
				'type'    => 'select',
				'class'   => 'wc-enhanced-select',
				'css'     => 'width: 450px;',
				'default' => '',
				'options' => array(
					'0' => __( 'Disabled', 'splitit-installment-payments' ),
					'1' => __( 'Enable Splitit if the cart consists only of products from the list below', 'splitit-installment-payments' ),
					'2' => __( 'Enable Splitit if the cart consists of at least one of the products from the list below', 'splitit-installment-payments' ),
				),
			),

			'New_Upstream_Messaging_Settings_section' => array(
				'title'                                => __( 'On-Site Messaging Settings', 'splitit-installment-payments' ),
				'description'                          => __( 'Adding on-site messaging is a great way to let shoppers know that you offer installment payments. It’s important to inform the customer throughout their shopping journey to help them make decisions before they get to the checkout.', 'splitit-installment-payments' ),
				'option_name'                          => 'splitit_upstream_messaging_selection',
				'splitit_upstream_messaging_selection' => array(
					'title'             => __( 'Upstream Messaging Selection', 'splitit-installment-payments' ),
					'type'              => 'multiselect',
					'desc_tip'          => true,
					'class'             => 'wc-enhanced-select',
					'css'               => 'width: 450px;',
					'custom_attributes' => array(
						'data-placeholder' => __( 'Upstream Messaging Selection', 'splitit-installment-payments' ),
					),
					'options'           => self::upstream_messaging_selection(),
				),
				'enabled_visa'                         => array(
					'title'   => __( 'Enable/Disable Visa Installment Supported', 'splitit-installment-payments' ),
					'type'    => 'checkbox',
					'label'   => __( 'Enable Visa', 'splitit-installment-payments' ),
					'default' => 'no',
				),
				'default_installments_number'          => array(
					'title'   => __( 'Set the default number of payments to display in your On-Site Messages', 'splitit-installment-payments' ),
					'type'    => 'input',
					'class'   => 'default-installments',
					'label'   => __( 'Set the default number of payments to display in your On-Site Messages', 'splitit-installment-payments' ),
					'default' => 4,
				),
				'pages'                                => array(
					'splitit_upstream_messaging_position_home_page' => array(
						'title'    => __( 'Home Page', 'splitit-installment-payments' ),
						'checkbox' => 'home_page_banner',
						'name'     => 'home_page_banner',
						'type'     => 'select',
						'options'  => array(
							'left'   => __( 'Left', 'splitit-installment-payments' ),
							'center' => __( 'Center', 'splitit-installment-payments' ),
							'right'  => __( 'Right', 'splitit-installment-payments' ),
						),
					),

					'splitit_upstream_messaging_position_shop_page' => array(
						'title'    => __( 'Shop Page', 'splitit-installment-payments' ),
						'checkbox' => 'shop',
						'name'     => 'shop',
						'type'     => 'select',
						'options'  => array(
							'left'   => __( 'Left', 'splitit-installment-payments' ),
							'center' => __( 'Center', 'splitit-installment-payments' ),
							'right'  => __( 'Right', 'splitit-installment-payments' ),
						),
					),

					'splitit_upstream_messaging_position_product_page' => array(
						'title'    => __( 'Product Page', 'splitit-installment-payments' ),
						'checkbox' => 'product',
						'name'     => 'product',
						'type'     => 'select',
						'options'  => array(
							'left'   => __( 'Left', 'splitit-installment-payments' ),
							'center' => __( 'Center', 'splitit-installment-payments' ),
							'right'  => __( 'Right', 'splitit-installment-payments' ),
						),
					),

					'splitit_upstream_messaging_position_cart_page' => array(
						'title'    => __( 'Cart Page', 'splitit-installment-payments' ),
						'checkbox' => 'cart',
						'name'     => 'cart',
						'type'     => 'select',
						'options'  => array(
							'left'   => __( 'Left', 'splitit-installment-payments' ),
							'center' => __( 'Center', 'splitit-installment-payments' ),
							'right'  => __( ' Right', 'splitit-installment-payments' ),
						),
					),

					'splitit_upstream_messaging_position_checkout_page' => array(
						'title'    => __( 'Checkout Page', 'splitit-installment-payments' ),
						'checkbox' => 'checkout',
						'name'     => 'checkout',
						'type'     => 'select',
						'options'  => array(
							'left'   => __( 'Left', 'splitit-installment-payments' ),
							'center' => __( 'Center', 'splitit-installment-payments' ),
							'right'  => __( ' Right', 'splitit-installment-payments' ),
						),
					),
				),
			),

			'css_provider'                            => array(
				'title' => __( 'Custom CSS', 'splitit-installment-payments' ),
				'type'  => 'title',
				'class' => 'css-provider-title',
			),

			'splitit_upstream_messaging_css'          => array(
				'title'       => __( 'On-Site Messaging CSS', 'splitit-installment-payments' ),
				'description' => __( 'Add custom CSS for On-Site Messaging, for example, to conform to your brand or in-house style', 'splitit-installment-payments' ),
				'type'        => 'textarea',
				'default'     => '<style></style>',
				'font_types'  => self::font_types(),
				'font_sizes'  => self::font_sizes(),
				'css'         => '',
				'placeholder' => '',
			),

			'splitit_flex_fields_css'                 => array(
				'title'       => __( 'Flex Fields CSS', 'splitit-installment-payments' ),
				'description' => __( 'Add custom CSS to change the UI, for example, to conform to your brand or in-house style', 'splitit-installment-payments' ),
				'type'        => 'textarea',
				'default'     => '<style></style>',
				'css'         => '',
				'placeholder' => '',
			),
		);
	}

	/**
	 * Method that getting logged merchant`s name from DB
	 *
	 * @return string
	 */
	public static function get_logged_merchant_name() {
		$merchant_name = get_option( 'merchant_name' );

		return $merchant_name ? ucwords( $merchant_name ) : 'Not chosen';
	}

	/**
	 * Method that getting logged merchant`s terminal from DB
	 *
	 * @return string
	 */
	public static function get_logged_merchant_terminal() {
		$terminal_name = get_option( 'terminal_name' );

		return $terminal_name ? ucwords( $terminal_name ) : 'Not chosen';
	}

	/**
	 * Method that getting splitit environment from DB
	 *
	 * @return string
	 */
	public static function get_splitit_environment() {
		return ucwords( get_option( 'splitit_environment' ) );
	}

	/**
	 * Initiate admin scripts and styles
	 *
	 * @param string $plugin_id Plugin ID
	 */
	public static function get_admin_scripts_and_styles( $plugin_id = 'splitit' ) {
		$_GET    = stripslashes_deep( $_GET );
		$section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : null;
		$action  = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : null;
		$post    = isset( $_GET['post'] ) ? sanitize_text_field( wp_unslash( $_GET['post'] ) ) : null;
		$id      = isset( $_GET['id'] ) ? sanitize_text_field( wp_unslash( $_GET['id'] ) ) : null;

		if ( $section == $plugin_id ) {
			add_action(
				'admin_enqueue_scripts',
				array(
					'SplitIt_FlexFields_Payment_Plugin_Settings',
					'add_admin_files',
				)
			);
			add_action( 'admin_footer', array( 'SplitIt_FlexFields_Payment_Plugin_Settings', 'wpb_hook_javascript' ) );

			if ( ! get_option( 'splitit_logged_user_data' ) || ! get_option( 'api_key' ) ) {
				add_action( 'admin_footer', array( 'SplitIt_FlexFields_Payment_Plugin_Settings', 'welcome_pop_up' ) );
			}
		}

		if ( ( 'edit' === $action && isset( $post ) ) || ( 'edit' === $action && isset( $id ) ) ) {
			add_action(
				'admin_enqueue_scripts',
				array(
					'SplitIt_FlexFields_Payment_Plugin_Settings',
					'add_admin_order_files',
				)
			);
		}
	}

	/**
	 * Welcome pop-up
	 */
	public static function welcome_pop_up() {
		?>
		<button style="display: none" id="welcome_pop_up_btn">Open Modal</button>

		<div id="splitit_welcome_pop_up" class="modal">
			<div class="modal-content">
				<div class="modal-header">
					<span class="Welcome-to-Splitit-p">
						Welcome to Splitit!
					</span>
				</div>
				<div class="modal-body">
					<div class="Splitit-is-an-Instal">
						Splitit is an installment solution for WooCommerce that lets your customers pay monthly with their existing credit card, so they don't need to take out a new loan. There's no applications, added interest or fees for the shopper to pay, so the checkout experience is fast and simple.
						<a href="https://www.splitit.com/" target="_blank">Learn more about Splitit</a>.
					</div>

					<div class="How-it-works">
						How do I add Splitit to WooCommerce?
					</div>
					<div class="welcome-img-block">
						<div>
							<img src="<?php echo esc_url( plugins_url( 'assets/img/welcome-connect.png', __DIR__ ) ); ?>" class="Group">
						</div>
						<div class="Path-2"></div>
						<div>
							<img src="<?php echo esc_url( plugins_url( 'assets/img/welcome-config.png', __DIR__ ) ); ?>" class="Group">
						</div>
						<div class="Path-2"></div>
						<div>
							<img src="<?php echo esc_url( plugins_url( 'assets/img/welcome-setup.png', __DIR__ ) ); ?>" class="Group">
						</div>
					</div>

					<div class="welcome-steps-block">
						<span class="-Connect-merchant">
							1. Connect your merchant account
						</span>
						<span class="-Configure-to-your">
							2. Configure according to your needs
						</span>
						<span class="-Setup-complete">
							3. Complete<br>setup!
						</span>
					</div>

				</div>
				<div class="modal-footer">
					<button id="connectMerchant" data-env="sandbox" class="connect_merchant_btn sandbox">
						<span class="Create-Account">
							My demo account
						</span>
					</button>
					<button id="connectMerchantProd" data-env="production" class="connect_merchant_btn">
						<span class="Create-Account">
							My live account
						</span>
					</button>
					<div class="need-help-block">
						<div class="need-help">
							Need help?
							<a href="https://support.splitit.com" target="_blank">
								click here
							</a>
						</div>
					</div>
				</div>
			</div>
		</div>

		<script>
			(function ($) {
				$(window).load(function(){
					$( "#welcome_pop_up_btn" ).trigger( "click" );
				})

				$( "#welcome_pop_up_btn" ).click( function () {
					$( "#splitit_welcome_pop_up" ).show()
				})

				$( "#welcomeModalClose" ).click( function() {
					$( "#splitit_welcome_pop_up" ).hide()
				})
			})(jQuery);
		</script>

		<?php
	}

	/**
	 * Added script and style to the order page
	 */
	public static function add_admin_order_files() {
		wp_enqueue_style( 'splitit_order_css', plugins_url( 'assets/css/adminOrder.css', __DIR__ ) );
		wp_enqueue_script( 'splitit_order_js', plugins_url( '/assets/js/adminOrder.js', __DIR__ ), array( 'jquery' ) );
		wp_add_inline_script( 'splitit_order_js', 'const WC_SPLITIT = ' . json_encode( array( 'ajaxurl_admin' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'splitit_start_installment_nonce' ) ) ), 'before' );
	}

	/**
	 * Register admin styles and scripts
	 */
	public static function add_admin_files() {
		wp_enqueue_style( 'spliti_admin_css', plugins_url( 'assets/css/admin.css', __DIR__ ) );
		wp_enqueue_script(
			'spliti_admin_js',
			plugins_url( '/assets/js/admin.js', __DIR__ ),
			array(
				'jquery',
				'jquery-validate',
				'jquery-validate-additional',
				'multipleSelect',
			)
		);

		wp_localize_script(
			'spliti_admin_js',
			'splitit_ajax_nonce',
			array(
				'nonce' => wp_create_nonce( 'splitit_secure_action' ),
			)
		);

		// @Codemiror
		$cm_settings['flexFieldsCss']        = wp_enqueue_code_editor( array( 'type' => 'text/css' ) );
		$cm_settings['upstreamMessagingCss'] = wp_enqueue_code_editor( array( 'type' => 'text/css' ) );
		wp_localize_script( 'jquery', 'cm_settings', $cm_settings );

		wp_enqueue_script( 'wp-theme-plugin-editor' );
		wp_enqueue_style( 'wp-codemirror' );

		// @JQuery Validation
		wp_enqueue_script( 'jquery-validate', plugins_url( '/assets/validation/jquery.validate.js', __DIR__ ), array( 'jquery' ) );
		wp_enqueue_script(
			'jquery-validate-additional',
			plugins_url( '/assets/validation/additional-methods.js', __DIR__ ),
			array(
				'jquery',
				'jquery-validate',
			)
		);

		// @select2
		wp_enqueue_script( 'jquery' );
		wp_enqueue_style(
			'select2-css',
			plugins_url( 'assets/js/select2/select2.min.css', __DIR__ ),
			array(),
			'4.0.13'
		);
		wp_enqueue_script(
			'select2-js',
			plugins_url( 'assets/js/select2/select2.min.js', __DIR__ ),
			array( 'jquery' ),
			'4.0.13',
			true
		);
		wp_localize_script(
			'jquery',
			'pluginData',
			array(
				'jquery_url'  => includes_url( 'js/jquery/jquery.js' ),
				'select2_css' => plugins_url( 'assets/js/select2/select2.min.css', __DIR__ ),
				'select2_js'  => plugins_url( 'assets/js/select2/select2.min.js', __DIR__ ),
			)
		);
	}

	/**
	 * Added script for CodeMirror
	 */
	public static function wpb_hook_javascript() {
		?>
		<script>
			ajaxurl_admin = '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>';
			(function ($) {
				$(function () {
					$('#ff_css_collapse').click(function () {
						let ffCodeMirror =  $('#ff_css_advanced_section .CodeMirror')
						if (!ffCodeMirror.length) {
							setTimeout(function() {
								wp.codeEditor.initialize($('#woocommerce_splitit_splitit_flex_fields_css'), cm_settings.flexFieldsCss);
							}, 500);
						}
					})
					$('#um_css_collapse').click(function () {
						let umCodeMirror =  $('#um_css_advanced_section .CodeMirror')
						if (!umCodeMirror.length) {
							setTimeout(function() {
								wp.codeEditor.initialize($('#woocommerce_splitit_splitit_upstream_messaging_css'), cm_settings.upstreamMessagingCss);
							}, 500);
						}
					})
				});
			})(jQuery);
		</script>
		<script
			api-key="<?php echo esc_attr( get_option( 'api_key' ) ? get_option( 'api_key' ) : '' ); ?>"
			currency="<?php echo esc_attr( get_woocommerce_currency() ); ?>"
			lang="<?php echo esc_attr( str_replace( '_', '-', get_locale() ) ); ?>"
			currencySymbol="<?php echo esc_attr( get_woocommerce_currency_symbol( get_woocommerce_currency() ) ); ?>"
			env="<?php echo esc_attr( strtolower( self::get_splitit_environment() ) ); ?>"
			src="https://web-components.splitit.com/upstream.js"
			default-installments="4"
			debug="false"
		></script>
		<?php

		wp_enqueue_style(
			'multipleSelect',
			plugins_url( 'assets/js/multiple-select/multiple-select.min.css', __DIR__ ),
			array(),
			'1.5.2',
			true
		);
		wp_enqueue_script(
			'multipleSelect',
			plugins_url( 'assets/js/multiple-select/multiple-select.min.js', __DIR__ ),
			array( 'jquery' ),
			'1.5.2',
			true
		);
	}

	/**
	 * Return allowed font sizes
	 *
	 * @return string[]
	 */
	private static function font_sizes() {
		$arr = array();

		for ( $i = 1; $i <= 72; $i++ ) {
			$arr[ $i ] = $i;
		}

		return $arr;
	}

	/**
	 * Return allowed font types
	 *
	 * @return string[]
	 */
	private static function font_types() {
		return array(
			'American Typewriter'   => 'American Typewriter',
			'Andale Mono'           => 'Andale Mono',
			'Arial'                 => 'Arial',
			'Arial Black'           => 'Arial Black',
			'Arial Narrow'          => 'Arial Narrow',
			'Arial Rounded MT Bold' => 'Arial Rounded MT Bold',
			'Arial Unicode MS'      => 'Arial Unicode MS',
			'Avenir'                => 'Avenir',
			'Avenir Next'           => 'Avenir Next',
			'Avenir Next Condensed' => 'Avenir Next Condensed',
			'Baskerville'           => 'Baskerville',
			'Big Caslon'            => 'Big Caslon',
			'Bodoni 72'             => 'Bodoni 72',
			'Bodoni 72 Oldstyle'    => 'Bodoni 72 Oldstyle',
			'Bodoni 72 Smallcaps'   => 'Bodoni 72 Smallcaps',
			'Bradley Hand'          => 'Bradley Hand',
			'Brush Script MT'       => 'Brush Script MT',
			'Chalkboard'            => 'Chalkboard',
			'Chalkboard SE'         => 'Chalkboard SE',
			'Chalkduster'           => 'Chalkduster',
			'Charter'               => 'Charter',
			'Cochin'                => 'Cochin',
			'Comic Sans MS'         => 'Comic Sans MS',
			'Copperplate'           => 'Copperplate',
			'Courier'               => 'Courier',
			'Courier New'           => 'Courier New',
			'Didot'                 => 'Didot',
			'DIN Alternate'         => 'DIN Alternate',
			'DIN Condensed'         => 'DIN Condensed',
			'Futura'                => 'Futura',
			'Geneva'                => 'Geneva',
			'Georgia'               => 'Georgia',
			'Gill Sans'             => 'Gill Sans',
			'Helvetica'             => 'Helvetica',
			'Helvetica Neue'        => 'Helvetica Neue',
			'Herculanum'            => 'Herculanum',
			'Hoefler Text'          => 'Hoefler Text',
			'Impact'                => 'Impact',
			'Lucida Grande'         => 'Lucida Grande',
			'Luminari'              => 'Luminari',
			'Marker Felt'           => 'Marker Felt',
			'Menlo'                 => 'Menlo',
			'Microsoft Sans Serif'  => 'Microsoft Sans Serif',
			'Monaco'                => 'Monaco',
			'Noteworthy'            => 'Noteworthy',
			'Optima'                => 'Optima',
			'Palatino'              => 'Palatino',
			'Papyrus'               => 'Papyrus',
			'Phosphate'             => 'Phosphate',
			'Rockwell'              => 'Rockwell',
			'Savoye LET'            => 'Savoye LET',
			'SignPainter'           => 'SignPainter',
			'Skia'                  => 'Skia',
			'Snell Roundhand'       => 'Snell Roundhand',
			'Tahoma'                => 'Tahoma',
			'Times'                 => 'Times',
			'Times New Roman'       => 'Times New Roman',
			'Trattatello'           => 'Trattatello',
			'Trebuchet MS'          => 'Trebuchet MS',
			'Verdana'               => 'Verdana',
			'Zapfino'               => 'Zapfino',
		);
	}

	/**
	 * Return allowed sections for Upstream Messaging
	 *
	 * @return array
	 */
	private static function upstream_messaging_selection() {
		return array(
			'home_page_banner' => __( 'Home Page', 'splitit-installment-payments' ),
			'shop'             => __( 'Shop Page', 'splitit-installment-payments' ),
			'product'          => __( 'Product Page', 'splitit-installment-payments' ),
			'cart'             => __( 'Cart Page', 'splitit-installment-payments' ),
			'checkout'         => __( 'Checkout Page', 'splitit-installment-payments' ),
		);
	}

	/**
	 * Return allowed selections for Refund Strategy
	 *
	 * @return array
	 */
	private static function refund_strategy_selection() {
		return array_reduce(
			\Splitit\Model\RefundStrategy::getAllowableEnumValues(),
			function ( $carry, $item ) {
				$value          = preg_replace( '/([a-z])([A-Z])/', '$1 $2', $item );
				$carry[ $item ] = ucfirst( strtolower( $value ) );
				return $carry;
			},
			array()
		);
	}

	/**
	 * Return allowed selections for Order Statuses
	 *
	 * @return array
	 */
	private static function available_order_statuses_selection() {
		$statuses       = wc_get_order_statuses();
		$clean_statuses = array();

		foreach ( $statuses as $key => $label ) {
			$clean_key                    = str_replace( 'wc-', '', $key );
			$clean_statuses[ $clean_key ] = $label;
		}

		return $clean_statuses;
	}

	/**
	 * Return count of Installments ranges
	 *
	 * @param array $settings Settings.
	 *
	 * @return array
	 */
	private static function get_new_installment_fields( array $settings ) {
		$installments = $settings['splitit_inst_conf']['ic_from'] ?? null;

		$return_data = array();

		if ( ! empty( $installments ) ) {
			foreach ( $installments as $value ) {
				$return_data[] = array(
					'ic_from'        => array(
						'title'   => __( 'Starting price*', 'splitit-installment-payments' ),
						'type'    => 'number',
						'class'   => 'from',
						'default' => '0',
					),
					'ic_to'          => array(
						'title'   => __( 'Ending price*', 'splitit-installment-payments' ),
						'type'    => 'number',
						'class'   => 'to',
						'default' => '1000',
					),
					'ic_installment' => array(
						'title'   => __( 'Installments*', 'splitit-installment-payments' ),
						'type'    => 'select',
						'class'   => 'installments',
						'options' => self::merchant_installments_range(),
					),
					'ic_action'      => array(
						'title' => '<span class="delete_instalment"><span class="trash-icon-new"></span></span>',
						'css'   => 'display:none;',
					),
				);
			}
		}

		return $return_data;
	}

	/**
	 * Return merchant installments range for Upstream Messaging
	 *
	 * @return array
	 */
	public static function merchant_installments_range() {
		$installments = array();

		$inst_from = 1;
		$inst_to   = 15;

		if ( get_option( 'merchant_settings' ) ) {
			$inst_from = get_option( 'merchant_settings' )->MinInstallments;
			$inst_to   = get_option( 'merchant_settings' )->MaxInstallments;
		}

		for ( $i = $inst_from; $i <= $inst_to; $i++ ) {
			$installments[ $i ] = $i;
		}

		return $installments;
	}

	/**
	 * @param object $order Order.
	 */
	public static function update_order_status_to_old( $order ) {
		$order->update_status( $order->get_meta( '_old_status' ) );
	}
}
