<?php
namespace SabaiApps\Directories\Component\Fieldset\FieldWidget;

use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Field;

class FieldsetFieldWidget extends Field\Widget\AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'field_types'           => array( $this->_name ),
			'repeatable'            => true,
			'default_settings'      => array(),
			'default_max_num_items' => 0,
		);
	}

	public function fieldWidgetForm( Field\IField $field, array $settings, $value = null, ?IEntity $entity = null, array $parents = array(), $language = null ) {
		$settings = $field->getFieldSettings();
		if ( empty( $settings['bundle'] )
			|| ( ! $bundle = $this->_application->Entity_Bundle( $settings['bundle'] ) )
		) {
			return;
		}

		return array(
			'#type'        => 'fieldset_fields',
			'#bundle'      => $bundle->name,
			'#fieldset_id' => empty( $value ) ? null : ( is_object( $value ) ? $value->getId() : $value ),
			'#wrap'        => $parents,
		);
	}
}
