<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly!
}

if ( ! class_exists( 'WPSC_EP_Admin' ) ) :

	final class WPSC_EP_Admin {

		/**
		 * Initialize this class
		 *
		 * @return void
		 */
		public static function init() {

			// load scripts & styles.
			add_action( 'wpsc_js_backend', array( __CLASS__, 'backend_scripts' ) );
			add_action( 'wpsc_css_backend', array( __CLASS__, 'backend_styles' ) );

			// email notifications.
			add_filter( 'wpsc_en_to_addresses', array( __CLASS__, 'en_restrict_addresses' ), 10, 2 );
			add_filter( 'wpsc_en_cc_addresses', array( __CLASS__, 'en_restrict_addresses' ), 10, 2 );
			add_filter( 'wpsc_en_bcc_addresses', array( __CLASS__, 'en_restrict_addresses' ), 10, 2 );
		}

		/**
		 * Backend scripts
		 *
		 * @return void
		 */
		public static function backend_scripts() {

			echo file_get_contents( WPSC_EP_ABSPATH . 'asset/js/admin.js' ) . PHP_EOL . PHP_EOL; //phpcs:ignore
		}

		/**
		 * Backend scripts
		 *
		 * @return void
		 */
		public static function backend_styles() {

			if ( is_rtl() ) {
				echo file_get_contents( WPSC_EP_ABSPATH . 'asset/css/admin-rtl.css' ) . PHP_EOL . PHP_EOL; //phpcs:ignore
			} else {
				echo file_get_contents( WPSC_EP_ABSPATH . 'asset/css/admin.css' ) . PHP_EOL . PHP_EOL; //phpcs:ignore
			}
		}

		/**
		 * Restrict addresses to send email notifications to avoid looping
		 *
		 * @param array                    $email_addresses - forwarding emails.
		 * @param WPSC_Email_Notifications $email - email object.
		 * @return str
		 */
		public static function en_restrict_addresses( $email_addresses, $email ) {

			$piping_address = WPSC_EP_Settings_General::get_piping_email_address();
			if ( $piping_address ) {
				$email_addresses = array_diff( $email_addresses, array( $piping_address ) );
			}

			$forwarding_addresses = WPSC_EP_Settings_General::get_forwarding_addresses();
			if ( $forwarding_addresses ) {
				$email_addresses = array_diff( $email_addresses, $forwarding_addresses );
			}

			return $email_addresses;
		}
	}
endif;

WPSC_EP_Admin::init();
