<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\services;

use apexl\Io\interfaces\GlobalDataInterface;
use apexl\Io\modules\user\entities\userEntity;
use apexl\Io\modules\user\interfaces\MasqueradeServiceInterface;

final readonly class CurrentUserFactory
{
    public function __construct(
        private GlobalDataInterface $globalData,
        private MasqueradeServiceInterface $masqueradeService,
    ) {}

    public function get(): ?userEntity
    {
        $entities = $this->globalData->getData('entities');

        $currentUser = $entities['user']['entity'] ?? null;

        if ($this->masqueradeService->isMasquerading()) {
            return $this->masqueradeService->getMasqueraded();
        }

        return $currentUser;
    }

}
