<?php
/**
 * Class Noesia Cs Emails Module.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_Emails_Module extends Noesia_CS_Admin_Module {
	/**
	 * Handles the related plugin operation.
	 */
	public function __construct() {
		$this->key         = 'emails';
		$this->label       = __( 'Emails', 'noesia-commerce-suite' );
		$this->icon        = '✉';
		$this->description = __( 'Clean brand email settings.', 'noesia-commerce-suite' );
		$this->view        = 'modules/emails/views/admin.php';
		$this->primary     = false;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		$email_service = $this->service( 'email_service' );
		$page = isset( $_GET['noesia_cs_email_log_page'] ) ? max( 1, absint( wp_unslash( $_GET['noesia_cs_email_log_page'] ) ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$per_page = 20;
		return array(
			'email_previews' => array(
				'passport'        => $email_service && method_exists( $email_service, 'get_preview_html' ) ? $email_service->get_preview_html( 'passport' ) : '',
				'reorder'         => $email_service && method_exists( $email_service, 'get_preview_html' ) ? $email_service->get_preview_html( 'reorder' ) : '',
			),
			'email_placeholders'   => $email_service && method_exists( $email_service, 'get_supported_placeholders' ) ? $email_service->get_supported_placeholders() : array(),
			'email_log_page'       => $page,
			'email_logs_per_page'  => $per_page,
			'email_log_count'      => $email_service && method_exists( $email_service, 'get_log_count' ) ? $email_service->get_log_count() : 0,
			'logs'                 => $email_service && method_exists( $email_service, 'get_logs_page' ) ? $email_service->get_logs_page( $page, $per_page ) : array(),
		);
	}

}
