<?php
/**
 * CardSelect class.
 *
 * @package DokanKits\Admin\Dashboard\Components\Fields
 * @since   3.0.5
 */

namespace DokanKits\Admin\Dashboard\Components\Fields;

/**
 * CardSelect Field.
 */
class CardSelect extends Select {

	/**
	 * Input Type.
	 *
	 * @var string $input_type Input Type.
	 */
	protected string $input_type = 'card-select';

	/**
	 * Number of columns for card layout.
	 *
	 * @var int $columns Number of columns.
	 */
	protected int $columns = 3;

	/**
	 * Whether multiple selection is allowed.
	 *
	 * @var bool $multiple Multiple selection flag.
	 */
	protected bool $multiple = false;

	/**
	 * Get columns.
	 *
	 * @return int
	 */
	public function get_columns(): int {
		return $this->columns;
	}

	/**
	 * Set columns.
	 *
	 * @param int $columns Number of columns.
	 *
	 * @return CardSelect
	 */
	public function set_columns( int $columns ): CardSelect {
		$this->columns = max( 1, min( 4, $columns ) ); // Limit between 1-4 columns.

		return $this;
	}

	/**
	 * Get multiple selection flag.
	 *
	 * @return bool
	 */
	public function get_multiple(): bool {
		return $this->multiple;
	}

	/**
	 * Set multiple selection flag.
	 *
	 * @param bool $multiple Multiple selection flag.
	 *
	 * @return CardSelect
	 */
	public function set_multiple( bool $multiple ): CardSelect {
		$this->multiple = $multiple;

		return $this;
	}

	/**
	 * Data validation.
	 *
	 * @param mixed $data Data for validation.
	 *
	 * @return bool
	 */
	public function data_validation( $data ): bool {
		if ( $this->multiple ) {
			return isset( $data ) && is_array( $data );
		}
		return isset( $data ) && ! is_array( $data );
	}

	/**
	 * Populate features array.
	 *
	 * @return array
	 */
	public function populate(): array {
		$data             = parent::populate();
		$data['columns']  = $this->get_columns();
		$data['multiple'] = $this->get_multiple();

		return $data;
	}
}
