<?php

namespace Planaday\Utility\FormBuilder\Structures;

use Planaday\Utility\DataGrouping\Collection;
use Planaday\Utility\FormBuilder\LayoutItem;
use Planaday\Utility\FormBuilder\SettableData;

class RemotelyLoadedForm extends BasicGroup {
	protected string $url = '';
	protected array $headers = [];

	/**
	 * @var Collection<LayoutItem&SettableData>
	 */
	protected Collection $dependencies;

	public function __construct() {
		$this->dependencies = new Collection();
	}

	public function setUrl(string $url): self {
		$this->url = $url;
		return $this;
	}

	public function setHeaders(array $headers): self {
		$this->headers = $headers;
		return $this;
	}

	public function addDependencies( LayoutItem&SettableData ...$dependencies ): self {
		$this->dependencies->append( ...$dependencies );
		return $this;
	}

	public function getImplJson(): array {
		return [
			"type" => 'remotelyLoadedForm',
			"url" => $this->url,
			"headers" => $this->headers,
			"dependencies" => [...$this->dependencies->select(fn ($dependency) => $dependency->getItemId())],
		];
	}
}