<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\hooks;

use apexl\Io\includes\Entity;
use apexl\Io\interfaces\AbstractHook;
use apexl\Io\modules\user\entities\RefreshToken;
use apexl\Io\modules\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;
    }
}