<?php

namespace goals\traits;

use objects\products_o;

trait goals_criteria_products_t
{
    /**
     * @var products_o[] The Products to be included in the criteria
     */
    protected array $list = [];

    /**
     * @inheritDoc
     */
    public function set(array $Products): self
    {
        $this->list = $Products;
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function list(): array
    {
        return $this->list;
    }

    /**
     * @inheritdoc
     */
    public function listIDs(): array
    {
        return array_map(function ($product) {
            return $product->id;
        }, $this->list);
    }

    /**
     * @inheritDoc
     */
    public function add(array|products_o $Products): self
    {
        if (is_array($Products)) {
            $this->list = array_merge($this->list, $Products);
        } else {
            $this->list[] = $Products;
        }
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function clear(): self
    {
        $this->list = [];
        return $this;
    }

    /**
     * @inheritDoc
     */
    public function remove(array|products_o $Products): self
    {
        if (is_array($Products)) {
            $this->list = array_filter($this->list, function ($dept) use ($Products) {
                return !in_array($dept, $Products, true);
            });
        } else {
            $this->list = array_filter($this->list, function ($dept) use ($Products) {
                return $dept !== $Products;
            });
        }
        return $this;
    }
}