<?php

declare(strict_types=1);

namespace KupShop\GTMBundle\ServerSideGTMEvent;

use KupShop\GTMBundle\Utils\DataLoaders\PurchaseState;
use KupShop\UserBundle\Event\UserNewsletterEvent;
use Symfony\Contracts\EventDispatcher\Event;

class UserSubscribeEvent extends AbstractServerSideGTMEvent
{
    protected function getPayload(Event $event): array
    {
        if (!($event instanceof UserNewsletterEvent)) {
            return [];
        }

        return $this->getCommonData($event) +
            [
                'events' => [
                    'name' => $this->getEventName(),
                    'params' => [
                        'email' => $event->getEmail(),
                        'shaEmail' => PurchaseState::shaEmail($event->getEmail()),
                    ],
                ],
            ];
    }

    public function getEventName(): string
    {
        return 'success_newsletter_subscribe';
    }
}
