<?php

namespace goals\traits;

use objects\departments_o;

trait goals_criteria_departments_t
{
    /**
     * @var departments_o[] The Departments to be included in the criteria
     */
    protected array $list = [];

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

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

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

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

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

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