<?php
/**
 * CPT defaults helper.
 *
 * Provides a single place to declare initial "enable indexing" defaults
 * for the CPT tab (field mapping).
 *
 * @package WLC\Algolia
 */

namespace wlc_algolia;

defined( 'ABSPATH' ) || die( 'Direct file access is forbidden' );

/**
 * CPT defaults helper class.
 */
class Cpt_Defaults {
	/**
	 * Return default fields configuration map.
	 *
	 * Shape:
	 * [
	 *   'post' => [
	 *     'post_title' => [ 'type' => 'string', 'index' => true ],
	 *     ...
	 *   ],
	 *   'page' => [ ... ],
	 * ]
	 *
	 * You can override these via the `wlc_algolia_default_index_fields` filter.
	 *
	 * @return array
	 */
	public static function get_default_index_fields(): array {
		$defaults = array(
			'post'       => array(
				'ID'           => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_content' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_title'   => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_excerpt' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_date'    => array(
					'type'  => 'string',
					'index' => true,
				),
			),
			'page'       => array(
				'ID'           => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_content' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_title'   => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_excerpt' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_date'    => array(
					'type'  => 'string',
					'index' => true,
				),
			),
			'product'    => array(
				'ID'           => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_content' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_title'   => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_excerpt' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_date'    => array(
					'type'  => 'string',
					'index' => true,
				),
			),
			'attachment' => array(
				'ID'           => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_title'   => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_content' => array(
					'type'  => 'string',
					'index' => true,
				),
				'post_date'    => array(
					'type'  => 'string',
					'index' => true,
				),
			),
		);

		return $defaults;
	}

	/**
	 * Convenience: check default indexing for a single field.
	 *
	 * @param string $post_type Post type.
	 * @param string $field Field name.
	 *
	 * @return bool
	 */
	public static function is_indexed_by_default( string $post_type, string $field ): bool {
		$defaults = self::get_default_index_fields();
		return ! empty( $defaults[ $post_type ][ $field ]['index'] );
	}
}
