<?php

namespace Mollie\Api\Resources;

use Mollie\Api\Contracts\Connector;
use Mollie\Api\Contracts\ResourceOrigin;
use Mollie\Api\Http\Response;

abstract class ResourceCollection extends BaseCollection
{
    /**
     * Resource class name.
     */
    public static string $resource = '';

    public static function getResourceClass(): string
    {
        if (empty(static::$resource)) {
            throw new \RuntimeException('Resource name not set');
        }

        return static::$resource;
    }

    public function setItems(array $items): self
    {
        $this->exchangeArray($items);

        return $this;
    }

    /**
     * @return static
     */
    public static function withResponse(Response $response, Connector $connector, $items = [], ?\stdClass $_links = null): self
    {
        $collection = new static($connector, $items, $_links);

        return $collection->setResponse($response);
    }

    /**
     * Origin-aware sibling of {@see self::withResponse()}. Used by
     * resource methods that propagate a non-HTTP origin (e.g. a
     * webhook snapshot) to a child collection without forcing the
     * caller to construct a synthetic HTTP response.
     *
     * @return static
     */
    public static function withOrigin(ResourceOrigin $origin, Connector $connector, $items = [], ?\stdClass $_links = null): self
    {
        $collection = new static($connector, $items, $_links);

        return $collection->setOrigin($origin);
    }
}
