<?php

declare(strict_types=1);

namespace Apexl\Io\Collections;

use Apexl\Io\Entity;
use Arrayy\Arrayy;
use Arrayy\Collection\AbstractCollection;
use Exception;

/**
 * @extends  AbstractCollection<array-key,Entity>
 */
class EntityCollection extends AbstractCollection
{
    /**
     * @throws Exception
     */
    /**
     * @deprecated Use the entity's {@see Entity::findMultiple()} and wrap the
     *             EntityCollection it returns. This takes positional condition and orderBy
     *             arrays and goes through the deprecated Entity::loadMultiple().
     */
    public static function loadMultiple(
        array $conditions = [],
        array $orderBy = [],
        $limit = false,
        $offset = false,
    ): static {
        $instance = new static();
        $entity = new ($instance->getType())();

        return $instance->createFromArray(
            $entity->loadMultiple(
                $conditions,
                $orderBy,
                $limit,
                $offset
            )
        );
    }

    public function getType(): string
    {
        return Entity::class;
    }

    public function pluck(string $field): Arrayy
    {
        return Arrayy::createFromObject($this->map(fn(Entity $entity) => $entity->{$field}));
    }
}
