<?php

namespace KupShop\UserAddressesBundle\Twig\Components\Account;

use KupShop\UserAddressesBundle\Entity\DeliveryAddressEntity;
use KupShop\UserAddressesBundle\Util\UserAddressesUtil;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\UX\LiveComponent\Attribute\LiveProp;

trait UserDeliveryAddressTrait
{
    #[LiveProp]
    public ?int $id = null;

    protected UserAddressesUtil $userAddressesUtil;

    public ?DeliveryAddressEntity $deliveryAddress = null;

    public function getAddress(): DeliveryAddressEntity
    {
        if ($this->deliveryAddress) {
            return $this->deliveryAddress;
        }

        if (!$this->id) {
            return $this->deliveryAddress = new DeliveryAddressEntity();
        }

        return $this->deliveryAddress = $this->userAddressesUtil->getDeliveryAddress($this->id);
    }

    #[Required]
    public function setUserAddressesUtil(UserAddressesUtil $userAddressesUtil): void
    {
        $this->userAddressesUtil = $userAddressesUtil;
    }
}
