<?php

namespace CouponsPlus\App\Coupon\Tree\Cart;

use CouponsPlus\App\Conditions\CartItem;

class AutoAddedItem
{
    public string $cartItemKey;
    public int $quantity;
    public string $componentId;

    /**
     * @param string|CartItem $cartItem
     * @param int $quantity
     * @param string $componentId
     */
    public function __construct($cartItem, int $quantity, string $componentId)
    {
        $this->cartItemKey = $cartItem instanceof CartItem? ($cartItem->getState('key') ?: $cartItem->getProduct()->get_id()) : $cartItem;
        $this->quantity = $quantity;
        $this->componentId = $componentId;
    }

    /**
     * **IMPORTANT**: It ignores quantities, only matches key and component id
     */
    public function isTheSameAs(AutoAddedItem $autoAddedItem) : bool
    {
        $sameKey = $this->cartItemKey === $autoAddedItem->cartItemKey;
        $sameComponentId = $this->componentId === $autoAddedItem->componentId;

        return $sameKey && $sameComponentId;
    }
}