<?php

namespace CouponsPlus\App\Conditions\BuiltIn\Filters;

use CouponsPlus\App\Conditions\BuiltIn\Filters\Helpers\CombinedAmountItemsFilter;
use CouponsPlus\App\Conditions\CartItem;
use CouponsPlus\App\Conditions\Filter;
use CouponsPlus\App\Conditions\IntersectionFilter;
use CouponsPlus\App\Conditions\ItemsSet;
use CouponsPlus\App\Conditions\SubsetFilter;
use CouponsPlus\App\Quantities\AmountValidator;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\Original\Collections\Mapper\Types;
use Mistralys\SubsetSum\SubsetSum;

/**
 * The number of items across all products.
 * the sum of the quanities of all products from all line items
 * 
 */
#[\AllowDynamicProperties]
Class NumberOfItems extends Filter
{
    const TYPE = 'NumberOfItems';
    const SET = SubsetFilter::class;
    
    protected $amountValidator;

    public static function getName() : string
    {
        return __('Quantity', 'coupons-plus-for-woocommerce');
    }

    public static function getDescription() : string
    {
        return '';
    }

    public static function getOptions() : Collection
    {
        return new Collection(array_merge(
            CombinedAmountItemsFilter::getOptionsMap([
                'equals' => [
                    __('Selects exactly this number of items from the cart that match ALL your filters combined. The cart may have more items, but not less', 'coupons-plus-for-woocommerce'),
                    //__('Selects exactly this quantity from items that match ALL filters combined. If the customer adds more to the cart, the extra items are not included in this selection, only the fixed amount specified.', 'coupons-plus-for-woocommerce'),
//                    __('For example, if you have a group with the Categories filter set to take Hoodies and set the Quantity to 2, the cart needs to have at least 2 Hoodies. It may have 2 or 5 or 10. This group would only take 2. If the cart had 1 Hoodie, this filter group would fail. If the cart had 2 items from other categories, it would fail too.', 'coupons-plus-for-woocommerce'),
                    //'For BOGO offers: The quantity you select here will represent the required items, not the target for the discounts.',
                    //'For 2-for-1 offers using the Discount offer: The quantity you select here will represent the BOTH required items and the target for the discounts. EG. if you set "2", one of these two may be used for discounts.',
                ],
                'minimum' => [
                    //__('Not compatible with BOGO offers. Select the Fixed option instead.', 'coupons-plus-for-woocommerce'),
                ],
                'maximum' => [
                    //__('Not compatible with BOGO offers. Select the Fixed option instead.', 'coupons-plus-for-woocommerce'),
                    __('Will take the maximum amount of matching items from all filters in this group. The cart may have more or less matching items. This filter will only make sure to take as many as the maximum amount.', 'coupons-plus-for-woocommerce'),
                    __('For example, if you have a group with the Categories filter set to take Hoodies and set the Quantity to Maximum 2, the cart may have any number of Hoodies. It may have 1, 2, 5 or 10. This group would only take a maximum of 2. If the cart had 1 Hoodie, this filter would take it. If the cart had 3 Hoodies, this filter would take 2.', 'coupons-plus-for-woocommerce'),
                    //'For BOGO offers: The quantity you select here will represent the required items, not the target for the discounts.',
                    //'For 2-for-1 offers using the Discount offer: The quantity you select here will represent the BOTH required items and the target for the discounts. EG. if you set "2", one of these two may be used for discounts.',
                ],
                'range' => [
                    //__('Not compatible with BOGO offers. Select the Fixed option instead.', 'coupons-plus-for-woocommerce'),
                    __('Will take a minimum and maximum amount of matching items from all filters in this group. The cart may have more matching items. This filter will only make sure to take as many as the maximum amount when the minimum amount is met.', 'coupons-plus-for-woocommerce'),
                    __('For example, if you have a group with the Categories filter set to take Hoodies and set the Quantity to Minimum 2 Maximum 4, the cart may have more Hoodies. It may have 2, 5 or 10. This group would only take an amount between 2 and 4. If the cart only had 1 Hoodie, this filter would fail. If the cart had 3 Hoodies, this filter would take all 3. If the cart had 6 Hoodies, this filter would only take 4.', 'coupons-plus-for-woocommerce'),
                    //'For BOGO offers: The quantity you select here will represent the required items, not the target for the discounts.',
                    //'For 2-for-1 offers using the Discount offer: The quantity you select here will represent the BOTH required items and the target for the discounts. EG. if you set "2", one of these two may be used for discounts.',
                ],
            ])->asArray()
        ));
    }

    public function filterSet(ItemsSet $itemsSet) : ItemsSet
    {
        $amount = $this->options->quantity->amount;

        if ($this->options->quantity->type->is('range')) {
            $amount = [
                $this->options->quantity->range->minimum,
                $this->options->quantity->range->maxmimum
            ];
        }

        return $itemsSet->selectQuantityOrdered( $this->options->quantity->type, $amount);

        // Deprecated as of 3.0, ordering is done in another layer
        /*(object) $combinedAmountItemsFilter = new CombinedAmountItemsFilter(
            $itemsSet,
            $this->options->asArray(),
            'quantity'
        );

        return $combinedAmountItemsFilter->getFilteredItems();*/
    }
}