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

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Field\Widget\NameWidget;

class NameFieldWidget extends NameWidget {

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

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?Entity\Type\IEntity $entity = null, array $parents = array(), $language = null ) {
		if ( ! isset( $value )
			&& isset( $entity )
			&& $entity->getType() === 'user'
		) {
			$value = array(
				'first_name' => $entity->getFirstName(),
				'last_name'  => $entity->getLastName(),
			);
		}
		return parent::fieldWidgetForm( $field, $settings, $value, $entity, $parents, $language );
	}
}
