<?php
/**
 * Dynamic WooCommerce product-type discovery.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChangesWooCommerce\CoreCompat\Discovery;

/**
 * Reports registered types without treating extension types as core.
 */
final class ProductTypeDiscovery {
	/**
	 * @var string[]
	 */
	private const CORE_TYPES = array( 'simple', 'grouped', 'external', 'variable' );

	/**
	 * @return array<string,array<string,mixed>>
	 */
	public function all(): array {
		$types      = function_exists( 'wc_get_product_types' ) ? wc_get_product_types() : array();
		$discovered = array();

		foreach ( $types as $slug => $label ) {
			$class = \WC_Product_Factory::get_product_classname( 0, $slug, 'product' );

			$discovered[ $slug ] = array(
				'slug'       => $slug,
				'label'      => wp_strip_all_tags( (string) $label ),
				'class'      => $class,
				'core'       => in_array( $slug, self::CORE_TYPES, true ),
				'capability' => $this->capabilityHints( $slug ),
			);
		}

		ksort( $discovered );

		return $discovered;
	}

	/**
	 * @return array<string,bool>
	 */
	private function capabilityHints( string $type ): array {
		return array(
			'attributes'       => true,
			'downloads'        => true,
			'grouped_children' => 'grouped' === $type,
			'inventory'        => ! in_array( $type, array( 'external', 'grouped' ), true ),
			'pricing'          => ! in_array( $type, array( 'variable', 'grouped' ), true ),
			'variations'       => 'variable' === $type,
		);
	}
}
