<?php

namespace EDPW\Core\Helpers;


use PasswordHash;
use WC_Order;

defined( 'ABSPATH' ) || exit;

class ShortCodes {

	/**
	 * Get address fields.
	 *
	 * @param string $type Address type.
	 * @param string $data_type Data type.
	 * @param string $pre_fix Prefix.
	 *
	 * @return array
	 */
	public static function getAddressFields( $type = 'billing', $data_type = 'value', $pre_fix = '', $args = [] ) {
		if ( ! in_array( $type, [ 'billing', 'shipping' ] ) || ! Plugin::isActive( 'woocommerce/woocommerce.php' ) ) {
			return [];
		}
		$default = [
			'order'           => '',
			'email_arguments' => []
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$province     = '';
		$country_code = '';
		if ( ! empty( $order ) ) {
			$country_code = WC::getOrderData( $type . '_country', $order );
			$states       = WC::getStates( $country_code );
			$state_code   = WC::getOrderData( $type . '_state', $order );
			$province     = $states[ $state_code ] ?? null;
		}
		$address_fields = apply_filters( 'edpw_address_fields', [
			'formatted_address' => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'formatted_' . $type . '_address', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get formatted %s address', 'email-editor-plus' ), $type ),
				'default' => 'Cecilia Chapman<br/>8105 West Brickell Drive<br/>Pomona, CA 92703<br/>United States (US)'
			],
			'first_name'        => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_' . 'first_name', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s first name', 'email-editor-plus' ), $type ),
				'default' => 'Cecilia'
			],
			'last_name'         => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_' . 'last_name', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s last name', 'email-editor-plus' ), $type ),
				'default' => 'Chapman'
			],
			'name'              => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_first_name', $order ) . ' ' . WC::getOrderData( $type . '_last_name', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s name', 'email-editor-plus' ), $type ),
				'default' => 'Cecilia Chapman'
			],
			'company'           => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_company', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s company', 'email-editor-plus' ), $type ),
				'default' => ''
			],
			'phone'             => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_phone', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s phone', 'email-editor-plus' ), $type ),
				'default' => '563-7401'
			],
			'address_1'         => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_address_1', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s address 1', 'email-editor-plus' ), $type ),
				'default' => '8105 West Brickell Drive'
			],
			'address_2'         => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_address_2', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s address 2', 'email-editor-plus' ), $type ),
				'default' => 'Steve\'s Apartment'
			],
			'city'              => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_city', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s city', 'email-editor-plus' ), $type ),
				'default' => 'Pomona'
			],
			'state'             => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_state', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s state', 'email-editor-plus' ), $type ),
				'default' => 'CA'
			],
			'province'          => [
				'value'   => $province,
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s province', 'email-editor-plus' ), $type ),
				'default' => 'California'
			],
			'zip'               => [
				'value'   => ! empty( $order ) ? WC::getOrderData( $type . '_postcode', $order ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s zip code', 'email-editor-plus' ), $type ),
				'default' => '92703'
			],
			'country'           => [
				'value'   => ! empty( $order ) ? WC::getCountry( $country_code ) : '',
				// translators: 1. %s will replace an address type
				'desc'    => sprintf( esc_html__( 'Get %s country', 'email-editor-plus' ), $type ),
				'default' => 'United States (US)'
			]
		], $type );

		return self::getFieldData( $address_fields, $data_type, $pre_fix );
	}

	/**
	 * Get shipping method fields.
	 *
	 * @param string $type Data type.
	 * @param string $pre_fix Prefix
	 * @param array $args arguments.
	 *
	 * @return array
	 */
	public static function getShippingMethodFields( $type = 'value', $pre_fix = '', $args = [] ) {
		if ( ! Plugin::isActive( 'woocommerce/woocommerce.php' ) ) {
			return [];
		}
		$default = [
			'order'           => '',
			'email_arguments' => []
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$fields = apply_filters( 'edpw_shipping_method_fields', [
			'title'          => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'shipping_method', $order ) : '',
				'desc'    => esc_html__( 'Get shipping method title', 'email-editor-plus' ),
				'default' => 'Flat rate'
			],
			'formated_price' =>
				[
					'value'   => ! empty( $order ) ? WC::formatPrice( WC::getOrderData( 'shipping_total', $order ) ) : '',
					'desc'    => esc_html__( 'Get shipping price with price format', 'email-editor-plus' ),
					'default' => WC::formatPrice( 20 )
				],
			'price'          => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'shipping_total', $order ) : '',
				'desc'    => esc_html__( 'Get shipping price without price format', 'email-editor-plus' ),
				'default' => 20
			]
		], $order );

		return self::getFieldData( $fields, $type, $pre_fix );
	}

	/**
	 * Get order fields.
	 *
	 * @param string $type Data type.
	 * @param string $pre_fix Prefix.
	 * @param array $args Arguments.
	 *
	 * @return array
	 */
	public static function getOrderFields( $type = 'value', $pre_fix = '', $args = [] ) {
		if ( ! Plugin::isActive( 'woocommerce/woocommerce.php' ) ) {
			return [];
		}
		$default = [
			'order'           => '',
			'email_arguments' => [],
			'language'        => 'en_US',
			'email_template'  => '',
			'extra_setting'   => [
				'show_sku'                 => true,
				'show_image'               => true,
				'product_image_width'      => 32,
				'product_image_height'     => 32,
				'product_image_size'       => 'thumbnail',
				'show_payment_instruction' => 'yes'
			]
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$order_notes  = WC::getOrderData( 'customer_order_notes', $order, [] );
		$order_status = ! empty( $order ) ? WC::getOrderData( 'status', $order ) : '';
		$items_totals = WC::getOrderData( 'order_item_totals', $order, 0 );
		$order_fee    = $order_refund = 0;
		if ( ! empty( $items_totals ) ) {
			foreach ( $items_totals as $item_total_key => $items_total_value ) {
				if ( strpos( $item_total_key, 'fee' ) !== false ) {
					$fees = WC::getOrderData( 'fees', $order, [] );
					foreach ( $fees as $fee ) {
						$order_fee += (float) WC::getOrderData( 'amount', $fee );
					}
				}
				if ( strpos( $item_total_key, 'refund' ) !== false ) {
					$order_refund = WC::getOrderData( 'total_refunded', $order, 0 );
				}
			}
		}
		$date_format = WC::getDateFormat();
		$created_at  = ! empty( $order ) ? WC::getOrderData( 'date_created', $order ) : '';
		if ( ! empty( $created_at ) ) {
			$created_at = gmdate( $date_format, strtotime( $created_at ) );
			if ( apply_filters( 'edpw_translate_display_date', true ) ) {
				$time       = strtotime( $created_at );
				$created_at = date_i18n( $date_format, $time );
			}
		}
		if ( is_object( $email_template ) && ! empty( $email_template->mj_component_data ) && ! empty( $email_template->mj_component_data['mj_order_summary'] ) ) {
			$order_summery         = $email_template->mj_component_data['mj_order_summary'];
			$extra_setting_mapping = [
				'show_sku'                 => [ 'key' => 'show_sku', 'default' => true ],
				'show_image'               => [ 'key' => 'show_image', 'default' => true ],
				'product_image_width'      => [ 'key' => 'product_image_width', 'default' => 32 ],
				'product_image_height'     => [ 'key' => 'product_image_height', 'default' => 32 ],
				'product_image_size'       => [ 'key' => 'product_image_height', 'default' => 'thumbnail' ],
				'show_payment_instruction' => [ 'key' => 'show_payment_instruction', 'default' => 'yes' ],
				'border'                   => [ 'key' => 'order-summary-border', 'default' => '1px solid #e1e1e1;' ],
			];
			foreach ( $extra_setting_mapping as $field_key => $field ) {
				if ( in_array( $field_key, [ 'show_sku', 'show_image' ] ) ) {
					$extra_setting[ $field_key ] = empty( $order_summery[ $field['key'] ] ) || $order_summery[ $field['key'] ] == 'yes';
				} else {
					$extra_setting[ $field_key ] = ! empty( $order_summery[ $field['key'] ] ) ? $order_summery[ $field['key'] ] : $field['default'];
				}
			}

			$style_map = [
				'font_family'      => [ 'key' => 'font-family', 'default' => 'Helvetica, sans-serif' ],
				'font_size'        => [ 'key' => 'font-size', 'default' => '13px' ],
				'color'            => [ 'key' => 'color', 'default' => '#000000' ],
				'background_color' => [ 'key' => 'background-color', 'default' => '' ],
				'text_align'       => [ 'key' => 'align', 'default' => ( is_rtl() ? 'right' : 'left' ) ],
				'line_height'      => [ 'key' => 'line-height', 'default' => '1.5' ],
				'font_weight'      => [ 'key' => 'font-weight', 'default' => '400' ],
				/*'border_top'       => [ 'key' => 'border-top', 'default' => '1px solid #e1e1e1;' ],
				'border_right'     => [ 'key' => 'border-right', 'default' => '1px solid #e1e1e1;' ],
				'border_left'      => [ 'key' => 'border-left', 'default' => '1px solid #e1e1e1;' ],
				'border_bottom'    => [ 'key' => 'border-bottom', 'default' => '1px solid #e1e1e1;' ],*/

				'padding' => [ 'key' => 'inner-padding', 'default' => '10px 25px' ],
			];
			foreach ( $style_map as $field_key => $field ) {
				$extra_setting[ $field_key ] = ! empty( $order_summery['styles'][ $field['key'] ] ) ? $order_summery['styles'][ $field['key'] ] : $field['default'];
			}
			//$extra_setting['padding'] = implode( ' ', $padding_value );
		}
		$image_path = EmailTemplate::getImagePath() . '/editor/blocks/order-summary.png';
		$fields     = apply_filters( 'edpw_order_fields', [
			'id'                     => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'order_number', $order ) : '',
				'desc'    => esc_html__( 'Get order id', 'email-editor-plus' ),
				'default' => '1234'
			],
			'email'                  =>
				[
					'value'   => ! empty( $order ) ? WC::getOrderData( 'billing_email', $order ) : '',
					'desc'    => esc_html__( 'Get order email', 'email-editor-plus' ),
					'default' => 'cecilia.chapman@flycart.test'
				],
			'items_count'            => [
				'value'   => ! empty( $order ) ? count( WC::getOrderItems( $order ) ) : 0,
				'desc'    => esc_html__( 'Get order items count', 'email-editor-plus' ),
				'default' => 3
			],
			'items_qty_count'        => [
				'value'   => ! empty( $order ) ? WC::getOrderItemsCount( $order ) : 0,
				'desc'    => esc_html__( 'Get order items quantity count', 'email-editor-plus' ),
				'default' => 3
			],
			'last_note'              => [
				'value'   => ! empty( $order ) && ! empty( $order_notes ) ?
					self::getOrderCustomerNotesContent( [ $order_notes[0] ], $order ) : '',
				'desc'    => esc_html__( 'Get order last note', 'email-editor-plus' ),
				'default' => ''
			],
			'notes'                  => [
				'value'   => ! empty( $order ) && ! empty( $order_notes ) ?
					self::getOrderCustomerNotesContent( $order_notes, $order ) : '',
				'desc'    => esc_html__( 'Get order notes', 'email-editor-plus' ),
				'default' => ''
			],
			'status'                 => [
				'value'   => ! empty( $order_status ) ? wc_get_order_status_name( $order_status ) : '',
				'desc'    => esc_html__( 'Get order status', 'email-editor-plus' ),
				'default' => 'completed'
			],
			'status_link'            => [
				'value'   => ! empty( $order ) ?
					'<a href="' . WC::getOrderData( 'view_order_url', $order ) . '">' . esc_html__( "Order", 'email-editor-plus' ) . '</a>' : '',
				'desc'    => esc_html__( 'Get order status link', 'email-editor-plus' ),
				'default' => '<a href="#">Order</a>'
			],
			'subtotal_price'         => [
				'value'   => $items_totals['cart_subtotal']['value'] ?? 0,
				'desc'    => esc_html__( 'Get order sub-total', 'email-editor-plus' ),
				'default' => WC::formatPrice( 640 )
			],
			'total_price'            => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'formatted_order_total', $order ) : 0,
				'desc'    => esc_html__( 'Get order total', 'email-editor-plus' ),
				'default' => WC::formatPrice( 690 )
			],
			'created_at'             => [
				'value'   => $created_at,
				'desc'    => esc_html__( 'Get order created date', 'email-editor-plus' ),
				'default' => gmdate( 'F d, Y', current_time( 'timestamp' ) )
			],
			'fee'                    => [
				'value'   => $order_fee,
				'desc'    => esc_html__( 'Get order fee', 'email-editor-plus' ),
				'default' => 0
			],
			'refund'                 => [
				'value'   => $order_refund,
				'desc'    => esc_html__( 'Get order refund', 'email-editor-plus' ),
				'default' => 0
			],
			'url'                    => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'view_order_url', $order ) : '',
				'desc'    => esc_html__( 'Get order url', 'email-editor-plus' ),
				'default' => ''
			],
			'order_items'            => [
				'value'   => ! empty( $order ) ? self::getOrderItemsTable( $order, $language, $email_arguments, $extra_setting ) : '',
				'desc'    => esc_html__( 'Get order item details', 'email-editor-plus' ),
				'default' => '<div><img alt="sample cart" src="' . $image_path . '" width="100%"></div>'
			],
			'downloads'              => [
				'value'   => ! empty( $order ) ? self::getOrderDownloadsTable( $order, $language ) : '',
				'desc'    => esc_html__( 'Get order downloads details', 'email-editor-plus' ),
				'default' => ''
			],
			'customer_provided_note' => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'customer_note', $order, [] ) : '',
				'desc'    => esc_html__( 'Get order customer note', 'email-editor-plus' ),
				'default' => 'Sample note by the customer.'
			],
			'customer_note'          => [
				'value'   => ! empty( $email_arguments['customer_note'] ) ? $email_arguments['customer_note'] : '',
				'desc'    => esc_html__( 'Customer Note', 'email-editor-plus' ),
				'default' => ''
			],
			'payment_url'            => [
				'value'   => ! empty( $order ) && WC::isMethodExists( $order, 'get_checkout_payment_url' ) ? $order->get_checkout_payment_url() : '',
				'desc'    => esc_html__( 'Checkout Payment Url', 'email-editor-plus' ),
				'default' => ''
			]
		], $order );


		return self::getFieldData( $fields, $type, $pre_fix );
	}

	public static function getPaymentFields( $type = 'value', $pre_fix = '', $args = [] ) {
		if ( ! Plugin::isActive( 'woocommerce/woocommerce.php' ) ) {
			return [];
		}
		$default = [
			'order'           => '',
			'email_arguments' => []
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$payment_method = '';
		if ( $order instanceof WC_Order ) {
			$items_totals = WC::getOrderData( 'order_item_totals', $order, [] );
			if ( isset( $items_totals['payment_method']['value'] ) ) {
				$payment_method = $items_totals['payment_method']['value'];
			}
			if ( empty( $payment_method ) ) {
				$payment_method = WC::getOrderData( 'payment_method_title', $order, '' );
			}
		}
		$fields = apply_filters( 'edpw_payment_fields', [
			'payment_method' => [
				'value'   => ! empty( $order ) ? $payment_method : '',
				'desc'    => esc_html__( 'Get order payment method', 'email-editor-plus' ),
				'default' => 'Check payments'
			],
			'url'            => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'checkout_payment_url', $order, '' ) : '',
				'desc'    => esc_html__( 'Get order payment url', 'email-editor-plus' ),
				'default' => '#'
			],
			'transaction_id' => [
				'value'   => ! empty( $order ) ? WC::getOrderData( 'transaction_id', $order, '' ) : '',
				'desc'    => esc_html__( 'Get order payment transaction id', 'email-editor-plus' ),
				'default' => ''
			]
		], $order );

		return self::getFieldData( $fields, $type, $pre_fix );
	}

	/**
	 * Get order meta.
	 * @return array
	 */
	public static function getOrderMetas( $args ) {
		$default = [
			'order'           => '',
			'email_arguments' => []
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$order_meta_data = [];
		if ( $order instanceof WC_Order ) {
			$order_meta_data = WC::getOrderMetaData( $order );
		}

		$order_meta = [];
		foreach ( $order_meta_data as $meta_data ) {
			if ( ! is_object( $meta_data ) ) {
				continue;
			}
			$order_meta_key   = WC::isMethodExists( $meta_data, '__get' ) ? $meta_data->__get( 'key' ) : '';
			$order_meta_value = WC::isMethodExists( $meta_data, '__get' ) ? $meta_data->__get( 'value' ) : '';
			if ( ! empty( $order_meta_key ) && is_string( $order_meta_value ) ) {
				$order_meta_key_for_short_code                = str_replace( ' ', '_', $order_meta_key );
				$order_meta[ $order_meta_key_for_short_code ] = $order_meta_value;
			}
		}

		return $order_meta;
	}

	public static function getOrderMetaFields( $type = 'value', $pre_fix = '', $args = [] ) {
		$order_meta = self::getOrderMetas( $args );
		$fields     = [];
		foreach ( $order_meta as $key => $value ) {
			$fields[ $key ] = [
				'value'   => $value,
				'desc'    => '',
				'default' => ''
			];
		}
		$fields = apply_filters( 'edpw_order_meta_fields', $fields, $args );

		return self::getFieldData( $fields, $type, $pre_fix );
	}

	public static function getCustomerFields( $type = 'value', $pre_fix = '', $args = [] ) {
		$default = [
			'order'           => '',
			'email_arguments' => [],
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$user       = false;
		$user_login = $name = $first_name = $last_name = $user_nicename = $email = $password = $activation_url = $set_password_url = $password_reset_url = '';

		if ( ! empty( $order ) && $order instanceof WC_Order ) {
			$user = WC::isMethodExists( $order, 'get_user' ) ? $order->get_user() : false;
			if ( ! empty( $user ) ) {
				$name          = $user->first_name . ' ' . $user->last_name;
				$first_name    = $user->first_name;
				$last_name     = $user->last_name;
				$email         = $user->user_email;
				$user_login    = $user->user_login;
				$user_nicename = $user->user_nicename;
			}
			if ( empty( $name ) ) {
				$first_name = WC::getOrderData( 'billing_first_name', $order );
				$last_name  = WC::getOrderData( 'billing_last_name', $order );
				$name       = $first_name . ' ' . $last_name;
				$email      = WC::getOrderData( 'billing_email', $order );
			}
		}

		if ( ! empty( $email_arguments ) ) {
			if ( ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->id ) && ( $email_arguments['email']->id == 'customer_new_account' || $email_arguments['email']->id == 'customer_new_account_activation' ) ) {
				$password = isset( $email_arguments['email']->user_pass ) && ! empty( $email_arguments['email']->user_pass ) ? $email_arguments['email']->user_pass : '';
				if ( empty( $password ) ) {
					$password = Input::get( 'pass1-text' );
					if ( empty( $password ) ) {
						$password = Input::get( 'pass1' );
					}
				}
			}
			if ( ! empty( $email_arguments['email'] ) && ! empty( $email_arguments['email']->object ) && $email_arguments['email']->object instanceof \WP_User ) {
				/**
				 * @var $wp_user \WP_User
				 */
				$wp_user = $email_arguments['email']->object;
				if ( empty( $name ) ) {
					$name = ! empty( $wp_user->first_name ) ? $wp_user->first_name . ' ' . $wp_user->last_name : $wp_user->display_name;
				}
				$first_name    = empty( $first_name ) ? $wp_user->first_name : $first_name;
				$last_name     = empty( $last_name ) ? $wp_user->last_name : $last_name;
				$user_login    = empty( $user_login ) ? $wp_user->user_login : $user_login;
				$user_nicename = empty( $user_nicename ) ? $wp_user->user_nicename : $user_nicename;
			}

			// WooCommerce Password Reset Url
			if ( ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->id ) && $email_arguments['email']->id == 'customer_reset_password' ) {
				$password_reset_url = esc_url( add_query_arg( array(
					'key'   => $email_arguments['email']->reset_key ?? '',
					'id'    => $email_arguments['email']->user_id ?? '',
					'login' => rawurlencode( $email_arguments['email']->user_login ?? '' )
				), wc_get_endpoint_url( 'lost-password', '', wc_get_page_permalink( 'myaccount' ) ) ) );
			}

			if ( ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->id ) && $email_arguments['email']->id == 'customer_new_account_activation' ) {
				$activation_url = isset( $email_arguments['email']->user_activation_url ) && ! empty( $email_arguments['email']->user_activation_url )
					? $email_arguments['email']->user_activation_url : $activation_url;
			}

			// WooCommerce set password url
			if ( ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->id ) && $email_arguments['email']->id == 'customer_new_account' ) {
				$set_password_url = isset( $email_arguments['email']->set_password_url ) && ! empty( $email_arguments['email']->set_password_url )
					? $email_arguments['email']->set_password_url : $set_password_url;
			}
			// WordPress Password Reset Url
			if ( ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->id ) && $email_arguments['email']->id == 'wp_retrieve_password_email' && ! empty( $email_arguments['email']->reset_key ) ) {
				$password_key       = $email_arguments['email']->reset_key;
				$user_login         = ! empty( $email_arguments['email']->user_login ) ? $email_arguments['email']->user_login : $user_login;
				$password_reset_url = network_site_url( "wp-login.php?action=rp&key=$password_key&login=" . rawurlencode( $user_login ), 'login' );
				if ( ! empty( $email_arguments['email']->locale ) ) {
					$password_reset_url .= '&wp_lang=' . $email_arguments['email']->locale;
				}
			}
			// WordPress set password url
			if ( ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->id ) && $email_arguments['email']->id == 'wp_new_user_register_email' && ! empty( $email_arguments['email']->key ) ) {
				$key              = $email_arguments['email']->key;
				$user_login       = ! empty( $email_arguments['email']->user_login ) ? $email_arguments['email']->user_login : $user_login;
				$locale           = ! empty( $email_arguments['email']->locale ) ? $email_arguments['email']->locale : '';
				$set_password_url = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' );
				if ( ! empty( $locale ) ) {
					$set_password_url .= '&wp_lang=' . $locale;
				}
			}

			if ( empty( $user_login ) && ! empty( $email_arguments['email'] ) && ! empty( $email_arguments['email']->user_login ) ) {
				$user_login = $email_arguments['email']->user_login;
			}

			if ( empty( $email ) && ! empty( $email_arguments['email'] ) && isset( $email_arguments['email']->user_email ) && ! empty( $email_arguments['email']->user_email ) ) {
				$email = $email_arguments['email']->user_email;
			}

		}

		$fields = apply_filters( 'edpw_customer_fields', [
			'name'               => [
				'value'   => $name,
				'desc'    => esc_html__( 'Get customer name', 'email-editor-plus' ),
				'default' => 'Cecilia Chapman'
			],
			'first_name'         => [
				'value'   => $first_name,
				'desc'    => esc_html__( 'Get customer first name', 'email-editor-plus' ),
				'default' => 'Cecilia'
			],
			'last_name'          => [
				'value'   => $last_name,
				'desc'    => esc_html__( 'Get customer last name', 'email-editor-plus' ),
				'default' => 'Chapman'
			],
			'user_nicename'      => [
				'value'   => $user_nicename,
				'desc'    => esc_html__( 'Get customer nice name(WordPress)', 'email-editor-plus' ),
				'default' => 'Chapman'
			],
			'email'              => [
				'value'   => $email,
				'desc'    => esc_html__( 'Get customer email', 'email-editor-plus' ),
				'default' => 'cecilia.chapman@flycart.test'
			],
			'password'           => [
				'value'   => $password,
				'desc'    => esc_html__( 'Get customer password', 'email-editor-plus' ),
				'default' => 'sample password'
			],
			'user_login'         => [
				'value'   => $user_login,
				'desc'    => esc_html__( 'Get customer user login', 'email-editor-plus' ),
				'default' => 'Chapman'
			],
			'activation_url'     => [
				'value'   => $activation_url,
				'desc'    => esc_html__( 'Get customer activation URL', 'email-editor-plus' ),
				'default' => '#'
			],
			'set_password_url'   => [
				'value'   => $set_password_url,
				'desc'    => esc_html__( 'Get customer set password URL', 'email-editor-plus' ),
				'default' => '#'
			],
			'password_reset_url' => [
				'value'   => $password_reset_url,
				'desc'    => esc_html__( 'Get customer reset password URL', 'email-editor-plus' ),
				'default' => '#'
			]
		], $args );


		return self::getFieldData( $fields, $type, $pre_fix );
	}

	public static function getShopFields( $type = 'value', $pre_fix = '' ) {

		$scheme = ( false !== strstr( get_option( 'home' ), 'https:' ) ) ? 'https' : 'http';
		$fields = apply_filters( 'edpw_shop_fields', [
			'landing_site' => [
				'value'   => site_url(),
				'desc'    => esc_html__( 'Get landing site URL', 'email-editor-plus' ),
				'default' => site_url()
			],
			'name'         => [
				'value'   => get_option( 'blogname' ),
				'desc'    => esc_html__( 'Get shop name', 'email-editor-plus' ),
				'default' => get_option( 'blogname' )
			],
			'email'        => [
				'value'   => get_option( 'admin_email' ),
				'desc'    => esc_html__( 'Get shop E-Mail', 'email-editor-plus' ),
				'default' => get_option( 'admin_email' )
			],
			'url'          => [
				'value'   => get_home_url( null, null, $scheme ),
				'desc'    => esc_html__( 'Get shop URL', 'email-editor-plus' ),
				'default' => get_home_url( null, null, $scheme )
			],
			'address_1'    => [
				'value'   => get_option( 'woocommerce_store_address', null ),
				'desc'    => esc_html__( 'Get shop address 1', 'email-editor-plus' ),
				'default' => get_option( 'woocommerce_store_address', null )
			],
			'address_2'    => [
				'value'   => get_option( 'woocommerce_store_address_2', null ),
				'desc'    => esc_html__( 'Get shop address 2', 'email-editor-plus' ),
				'default' => get_option( 'woocommerce_store_address_2', null )
			],
			'city'         => [
				'value'   => get_option( 'woocommerce_store_city', null ),
				'desc'    => esc_html__( 'Get shop city', 'email-editor-plus' ),
				'default' => get_option( 'woocommerce_store_city', null )
			],
			'zip'          => [
				'value'   => get_option( 'woocommerce_store_postcode', null ),
				'desc'    => esc_html__( 'Get shop zip code', 'email-editor-plus' ),
				'default' => get_option( 'woocommerce_store_postcode', null )
			],
		] );
		if ( in_array( $type, [ 'value', 'default' ] ) ) {
			$fields['image_path'] = [
				'value'   => EmailTemplate::getImagePath(),
				'desc'    => esc_html__( 'Image folder path', 'email-editor-plus' ),
				'default' => EmailTemplate::getImagePath()
			];
		}

		return self::getFieldData( $fields, $type, $pre_fix );
	}

	public static function getSocialFields( $type = 'value', $pre_fix = '' ) {
		$fields = apply_filters( 'edpw_social_fields', [
			'facebook'  => [
				'value'   => EmailTemplate::getSetting( 'facebook_link', 'https://www.facebook.com/' ),
				'desc'    => esc_html__( 'Facebook link', 'email-editor-plus' ),
				'default' => 'https://www.facebook.com/'
			],
			'twitter'   => [
				'value'   => EmailTemplate::getSetting( 'twitter_link', 'https://x.com/' ),
				'desc'    => esc_html__( 'X/Twitter link', 'email-editor-plus' ),
				'default' => 'https://x.com/'
			],
			'linkedin'  => [
				'value'   => EmailTemplate::getSetting( 'linkedin_link', 'https://www.linkedin.com/' ),
				'desc'    => esc_html__( 'Linkedin link', 'email-editor-plus' ),
				'default' => 'https://www.linkedin.com/'
			],
			'pinterest' => [
				'value'   => EmailTemplate::getSetting( 'pinterest_link', 'https://www.pinterest.com/' ),
				'desc'    => esc_html__( 'Pinterest link', 'email-editor-plus' ),
				'default' => 'https://www.pinterest.com/'
			],
		], $type, $pre_fix );

		return self::getFieldData( $fields, $type, $pre_fix );
	}

	public static function getWordPressFields( $type = 'value', $pre_fix = '', $args = [] ) {
		$default = [
			'order'           => '',
			'email_arguments' => [],
		];
		$args    = wp_parse_args( $args, $default );
		extract( $args );
		$fields = apply_filters( 'edpw_wordpress_fields', [
			'login_url' => [
				'value'   => wp_login_url(),
				'desc'    => __( 'Site login url', 'eca-wordpress-compatibility' ),
				'default' => 'https://example.com/',
			]
		], $args );

		return self::getFieldData( $fields, $type, $pre_fix );
	}

	public static function getOrderDownloadsTable( $order, $language ) {
		$show_downloads = WC::hasDownloadableItem( $order ) && WC::isDownloadPermitted( $order );
		if ( ! $show_downloads ) {
			return '';
		}
		$template_path = EDPW_PLUGIN_PATH . 'templates/order_downloads.php';
		$override_path = get_theme_file_path( EDPW_PLUGIN_SLUG . '/templates/order_downloads.php' );
		if ( file_exists( $override_path ) ) {
			$template_path = $override_path;
		}
		$downloads = WC::getOrderData( 'downloadable_items', $order, [] );
		$columns   = apply_filters( 'edpw_email_downloads_columns',
			[
				'download-product' => esc_html__( 'Product', 'email-editor-plus' ),
				'download-expires' => esc_html__( 'Expires', 'email-editor-plus' ),
				'download-file'    => esc_html__( 'Download', 'email-editor-plus' ),
			]
		);
		$details   = [
			'direction' => EmailTemplate::getLanguageDirection( $language ),
			'columns'   => $columns,
			'downloads' => $downloads
		];
		$content   = Util::renderTemplate( $template_path, $details );
		$css       = EmailTemplate::getSetting( 'custom_css', EmailTemplate::getDefaultCustomCss() );
		if ( ! empty( $css ) ) {
			$content = WC::styleInline( $content, $css );
		}

		return apply_filters( 'edpw_get_order_downloads_table', $content, $order );
	}

	public static function getOrderItemsTable( $order, $language, $email_arguments = [], $extra_setting = [] ) {
		$template_path = EDPW_PLUGIN_PATH . 'templates/order_items.php';
		$override_path = get_theme_file_path( EDPW_PLUGIN_SLUG . '/templates/order_items.php' );
		if ( file_exists( $override_path ) ) {
			$template_path = $override_path;
		}
		$template_path = apply_filters( 'edpw_order_notes_template', $template_path );
		$arguments     = [
			'show_image'               => true,
			'product_image_size'       => 'thumbnail',
			'product_image_height'     => 32,
			'product_image_width'      => 32,
			'show_sku'                 => true,
			'show_payment_instruction' => 'yes',
			'text_align'               => is_rtl() ? 'right' : 'left',
			'background_color'         => '',
			'color'                    => '',
			'font_family'              => '',
			'font_size'                => '',
			'line_height'              => '',
			'font_weight'              => '',
			'boarder'                  => '',
			/*'border_top'               => '',
			'border_right'             => '',
			'border_left'              => '',
			'border_bottom'            => '',*/
			'padding'                  => '',
			'direction'                => EmailTemplate::getLanguageDirection( $language ),
			'margin_side'              => apply_filters( 'edpw_email_template_order_item_migign_side', ( is_rtl() ? 'right' : 'left' ) ),
			'items'                    => WC::getOrderItems( $order ),
			'order'                    => $order,
			'plain_text'               => $email_arguments['plain_text'] ?? false,
			'show_download_links'      => WC::isOrderDownLoadPermitted( $order ) && ( isset( $email_arguments['sent_to_admin'] ) && ! $email_arguments['sent_to_admin'] ),
			'show_purchase_note'       => WC::isOrderPaid( $order ) && ( isset( $email_arguments['sent_to_admin'] ) && ! $email_arguments['sent_to_admin'] ),
			'totals'                   => [],
			'email'                    => isset( $email_arguments['email'] ) && is_object( $email_arguments['email'] ) ? $email_arguments['email'] : '',
			'sent_to_admin'            => isset( $email_arguments['sent_to_admin'] ) ? $email_arguments['sent_to_admin'] : false,
		];
		/*'border_top'       => [ 'key' => 'border-top', 'default' => '1px solid #e1e1e1;' ],
				'border_right'     => [ 'key' => 'border-right', 'default' => '1px solid #e1e1e1;' ],
				'border_left'      => [ 'key' => 'border-left', 'default' => '1px solid #e1e1e1;' ],
				'border_bottom'    => [ 'key' => 'border-bottom', 'default' => '1px solid #e1e1e1;' ],
				'padding'          => [ 'key' => 'inner-padding', 'default' => '' ],*/
		$arguments    = array_merge( $arguments, $extra_setting );
		$order_totals = WC::getOrderData( 'order_item_totals', $order, [] );

		if ( $arguments['show_payment_instruction'] == 'no' ) {
			unset( $order_totals['payment_method'] );
		}
		$sent_to_admin = $email_arguments['sent_to_admin'] ?? false;
		if ( $arguments['show_payment_instruction'] == 'only_customer' && $sent_to_admin ) {
			unset( $order_totals['payment_method'] );
		}

		$arguments['totals'] = $order_totals;
		$content             = Util::renderTemplate( $template_path, $arguments );
		$css                 = EmailTemplate::getSetting( 'custom_css', EmailTemplate::getDefaultCustomCss() );
		if ( ! empty( $css ) ) {
			$content = WC::styleInline( $content, $css );
		}

		return apply_filters( 'edpw_get_order_table', $content, $order );
	}

	public static function getOrderCustomerNotesContent( $order_notes, $order ) {
		$template_path = EDPW_PLUGIN_PATH . 'templates/customer_notes.php';
		$override_path = get_theme_file_path( EDPW_PLUGIN_SLUG . '/templates/customer_notes.php' );
		if ( file_exists( $override_path ) ) {
			$template_path = $override_path;
		}
		$template_path = apply_filters( 'edpw_order_notes_template', $template_path );
		$content       = Util::renderTemplate( $template_path, [ 'order_notes' => (object) $order_notes ] );

		$css = EmailTemplate::getSetting( 'custom_css', EmailTemplate::getDefaultCustomCss() );
		if ( ! empty( $css ) ) {
			$content = WC::styleInline( $content, $css );
		}

		return apply_filters( 'edpw_get_order_notes', $content, $order );
	}

	/**
	 * Fields process.
	 *
	 * @param array $fields Fields.
	 * @param string $type Data type.
	 * @param string $pre_fix Prefix.
	 *
	 * @return array
	 */
	public static function getFieldData( $fields, $type = 'value', $pre_fix = '' ) {
		if ( empty( $fields ) || ! is_array( $fields ) ) {
			return $fields;
		}
		switch ( $type ) {
			case 'default':
				foreach ( $fields as $field => $field_data ) {
					unset( $fields[ $field ] );
					$fields[ $pre_fix . $field ] = ! empty( $field_data['default'] ) ? $field_data['default'] : '';
				}
				break;
			case 'desc':
				foreach ( $fields as $field => $field_data ) {
					unset( $fields[ $field ] );
					$fields[ $pre_fix . $field ] = ! empty( $field_data['desc'] ) ? $field_data['desc'] : '';
				}
				break;
			case 'value':
			default:
				foreach ( $fields as $field => $field_data ) {
					unset( $fields[ $field ] );
					$fields[ $pre_fix . $field ] = $field_data['value'] ?? '';
				}
				break;
		}

		return $fields;
	}

	public static function processShortCodeList( $type, $short_codes ) {
		if ( ! is_string( $type ) || ! is_array( $short_codes ) ) {
			return $short_codes;
		}
		switch ( $type ) {
			case 'wp_new_user_register_email':
				$ignore_short_codes = [
					'billing_address'  => [],
					'shipping_address' => [],
					'shipping_method'  => [],
					'order'            => [],
					'payment'          => [],
					'order_meta'       => [],
					'customer'         => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.password_reset_url}}'
						]
					],
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'wp_retrieve_password_email':
				$ignore_short_codes = [
					'billing_address'  => [],
					'shipping_address' => [],
					'shipping_method'  => [],
					'order'            => [],
					'payment'          => [],
					'order_meta'       => [],
					'customer'         => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.set_password_url}}',
						]
					]
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'wp_password_change_email':
				$ignore_short_codes = [
					'billing_address'  => [],
					'shipping_address' => [],
					'shipping_method'  => [],
					'order'            => [],
					'payment'          => [],
					'order_meta'       => [],
					'customer'         => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.set_password_url}}',
							'{{customer.password_reset_url}}'
						]
					]
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'wp_email_change_email':
				$ignore_short_codes = [
					'billing_address'  => [],
					'shipping_address' => [],
					'shipping_method'  => [],
					'order'            => [],
					'payment'          => [],
					'order_meta'       => [],
					'customer'         => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.set_password_url}}',
							'{{customer.password_reset_url}}'
						]
					]
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'new_order':
			case 'cancelled_order':
			case 'failed_order':
			case 'customer_on_hold_order':
			case 'customer_processing_order':
			case 'customer_completed_order':
			case 'customer_refunded_order':
			case 'customer_invoice':
				$ignore_short_codes = [
					'order'    => [ 'short_codes' => [ '{{order.notes}}' ] ],
					'customer' => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.set_password_url}}',
							'{{customer.password_reset_url}}'
						]
					]
				];
				//customer_note
				$short_codes = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'customer_note':
				$ignore_short_codes = [
					'customer' => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.set_password_url}}',
							'{{customer.password_reset_url}}'
						]
					]
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'customer_new_account':
				$ignore_short_codes = [
					'billing_address'  => [],
					'shipping_address' => [],
					'shipping_method'  => [],
					'order'            => [],
					'payment'          => [],
					'customer'         => [
						'short_codes' => [
							'{{customer.password_reset_url}}',
						]
					],
					'order_meta'       => []
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
			case 'customer_reset_password':
				$ignore_short_codes = [
					'billing_address'  => [],
					'shipping_address' => [],
					'shipping_method'  => [],
					'order'            => [],
					'payment'          => [],
					'customer'         => [
						'short_codes' => [
							'{{customer.password}}',
							'{{customer.activation_url}}',
							'{{customer.set_password_url}}'
						]
					],
					'order_meta'       => []
				];
				$short_codes        = self::removeIgnoreShortCodes( $short_codes, $ignore_short_codes );
				break;
		}

		return $short_codes;
	}

	public static function removeIgnoreShortCodes( $short_codes, $ignore_short_codes ) {
		if ( empty( $short_codes ) || empty( $ignore_short_codes ) ) {
			return $short_codes;
		}

		foreach ( $ignore_short_codes as $short_code_key => $ignore_short_code ) {
			if ( ! empty( $short_codes[ $short_code_key ] ) && empty( $ignore_short_code ) ) {
				unset( $short_codes[ $short_code_key ] );
			}
			if ( ! empty( $short_codes[ $short_code_key ] ) && ! empty( $short_codes[ $short_code_key ]['short_codes'] ) && ! empty( $ignore_short_code['short_codes'] ) ) {
				foreach ( $ignore_short_code['short_codes'] as $single_short_code ) {
					if ( ! empty( $short_codes[ $short_code_key ]['short_codes'][ $single_short_code ] ) ) {
						unset( $short_codes[ $short_code_key ]['short_codes'][ $single_short_code ] );
					}
				}
			}
		}

		return $short_codes;
	}
}