<?php
/**
 * File Path: includes/data/class-mbs-bisac-codes.php
 *
 * BISAC Subject Code List
 *
 * Static class exposing the BISG BISAC subject code list as a PHP array.
 * Structure: code => label (e.g. 'FIC014000' => 'FICTION / Fantasy / General').
 * JUV_PREFIXES and JNF_PREFIXES are used to conditionally show age/grade
 * range fields when a Juvenile title subject code is selected.
 *
 * Source: BISG Best Practices for Product Metadata (BISAC Subject Codes).
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Data
 * @since      1.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_BISAC_Codes { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * BISAC code prefixes that trigger the Audience / Age-Range conditional UI.
	 * Codes starting with JUV or JNF indicate Juvenile content.
	 *
	 * @var string[]
	 */
	const JUV_PREFIXES = array( 'JUV', 'JNF' );

	/**
	 * Full BISG BISAC subject code list.
	 * code => display label.
	 *
	 * @return array<string,string>
	 */
	public static function get_all(): array {
		return MBS_BISAC_Data::get();
	}

	/**
	 * Return true if the given BISAC code is in the JUV/JNF category.
	 *
	 * @param string $code BISAC code.
	 * @return bool
	 */
	public static function is_juvenile( string $code ): bool {
		foreach ( self::JUV_PREFIXES as $prefix ) {
			if ( str_starts_with( $code, $prefix ) ) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Validate a code against the whitelist.
	 *
	 * @param string $code BISAC code to validate.
	 * @return bool
	 */
	public static function is_valid( string $code ): bool {
		$all = self::get_all();
		return isset( $all[ $code ] );
	}
}
