<?php

/**
 * This file is part of the bitrix24-php-sdk package.
 *
 * © Maksim Mesilov <mesilov.maxim@gmail.com>
 *
 * For the full copyright and license information, please view the MIT-LICENSE.txt
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Bitrix24\SDK\Services\Entity;

use Bitrix24\SDK\Attributes\ApiServiceBuilderMetadata;
use Bitrix24\SDK\Core\Credentials\Scope;
use Bitrix24\SDK\Services\AbstractServiceBuilder;
use Bitrix24\SDK\Services\Entity;

#[ApiServiceBuilderMetadata(new Scope(['entity']))]
class EntityServiceBuilder extends AbstractServiceBuilder
{
    public function entity(): Entity\Entity\Service\Entity
    {
        if (!isset($this->serviceCache[__METHOD__])) {
            $this->serviceCache[__METHOD__] = new Entity\Entity\Service\Entity(
                new Entity\Entity\Service\Batch($this->batch, $this->log),
                $this->core,
                $this->log
            );
        }

        return $this->serviceCache[__METHOD__];
    }

    public function item(): Entity\Item\Service\Item
    {
        if (!isset($this->serviceCache[__METHOD__])) {
            $this->serviceCache[__METHOD__] = new Entity\Item\Service\Item(
                new Entity\Item\Service\Batch($this->batch, $this->log),
                $this->core,
                $this->log
            );
        }

        return $this->serviceCache[__METHOD__];
    }
    
    public function section(): Entity\Section\Service\Section
    {
        if (!isset($this->serviceCache[__METHOD__])) {
            $this->serviceCache[__METHOD__] = new Entity\Section\Service\Section(
                new Entity\Section\Service\Batch($this->batch, $this->log),
                $this->core,
                $this->log
            );
        }

        return $this->serviceCache[__METHOD__];
    }
    
    public function itemProperty(): Entity\Item\Property\Service\Property
    {
        if (!isset($this->serviceCache[__METHOD__])) {
            $batch = new Entity\Item\Property\Batch(
                $this->core,
                $this->log
            );
            $this->serviceCache[__METHOD__] = new Entity\Item\Property\Service\Property(
                new Entity\Item\Property\Service\Batch($batch, $this->log),
                $this->core,
                $this->log
            );
        }

        return $this->serviceCache[__METHOD__];
    }
}
