<?php
/**
 * Registers the emails with WooCommerce.
 *
 * @package SkyServers\Retragere
 */

declare(strict_types=1);

namespace SkyServers\Retragere\Wc\Emails;

/**
 * Once registered, both appear under WooCommerce - Settings - Emails, with the
 * same enable, recipient and template controls as every other store email.
 */
final class Registrar
{
    public static function register(): void
    {
        add_filter('woocommerce_email_classes', [self::class, 'classes']);
    }

    /**
     * @param array<string,mixed> $emails
     *
     * @return array<string,mixed>
     */
    public static function classes(array $emails): array
    {
        $emails['SSRC_Customer_Email'] = new CustomerEmail();
        $emails['SSRC_Store_Email']    = new StoreEmail();
        $emails['SSRC_Decision_Email'] = new DecisionEmail();

        return $emails;
    }

    /**
     * @return CustomerEmail|StoreEmail|DecisionEmail|null
     */
    public static function get(string $key)
    {
        if (!function_exists('WC')) {
            return null;
        }

        $mailer = WC()->mailer();
        $emails = $mailer->get_emails();

        return isset($emails[$key]) ? $emails[$key] : null;
    }
}
