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

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

class MetaFieldWidget extends Field\Widget\AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'accept_multiple' => false,
			'repeatable'      => false,
			'field_types'     => array( $this->_name ),
		);
	}

	public function fieldWidgetForm( Field\IField $field, array $settings, $value = null, ?IEntity $entity = null, array $parents = array(), $language = null ) {
		if ( $field->getFieldName() === 'entity_meta' ) {
			$value = array();
			if ( isset( $entity ) ) {
				if ( $meta = $this->_application->getPlatform()->getEntityMeta( $entity->getType(), $entity->getId() ) ) {
					foreach ( $meta as $meta_key => $meta_value ) {
						$value[ $meta_key ] = $meta_value;
					}
				}
			}
		} elseif ( isset( $entity ) ) {
				$value = $this->_application->getPlatform()->getEntityMeta( $entity->getType(), $entity->getId(), $field->getFieldName() );
		}

		$form = array();
		return $this->_application->Filter( 'entity_meta_field_widget_form', $form, array( $field, $value, $entity, $parents, $language ) );
	}
}
