<?php

namespace domain\Setting;

use domain\_\AbstractRepository;

class SettingRepository extends AbstractRepository
{
    protected SettingRepositoryQuery $query;

    public function loadDependencies(): void
    {
        $this->loadQuery(SettingRepositoryQuery::class);
    }
    
    public function getDefaultOptionsSearch(): array
    {
        return [
            self::WITH => ['producer'],
            self::JOIN_WITH => [],
            self::ORDER_BY => '',
            self::ATTRIBUTE_ID_PRODUCER => 'setting.id_producer'
        ];
    }

    public function findOneSettingByName(string $name)
    {
        if($this->isOutOfProducerContext()) {
            return $this->findOneAdminSettingByName($name);
        }
        else {
            return $this->findOneProducerSettingByName($name);
        }
    }

    public function findOneAdminSettingByName(string $name)
    {
        return $this->createQuery()
            ->filterProducerIsNull()
            ->filterByName($name)
            ->findOne();
    }

    public function findOneProducerSettingByName(string $name)
    {
        return $this->createDefaultQuery()
            ->filterByName($name)
            ->findOne();
    }
}