<?php

declare(strict_types=1);

namespace Apexl\Io\User\Hooks;

use Apexl\Io\Entity;
use Apexl\Io\Interfaces\AbstractHook;
use Apexl\Io\User\Entities\RefreshToken;
use Apexl\Io\User\Interfaces\TokenHasherInterface;

final readonly class HashRefreshTokenBeforeStore implements AbstractHook
{

    public function __construct(private TokenHasherInterface $tokenHasher) {}

    public function hooks(): array
    {
        return [Entity::entityHook(Entity::HOOK__BEFORE_STORE, RefreshToken::class)];
    }

    public function __invoke(array $data, RefreshToken $entity): array
    {
        if ($entity->isDirty('token')) {
            $data['token'] = $this->tokenHasher->hash($data['token']);
        }

        return $data;
    }
}