<?php
/**
 * File Path: includes/helpers/class-mbs-format-options.php
 *
 * Format Options Helper
 *
 * Pure-static helpers for book format (edition type) option lists and labels.
 * Extracted from MBS_Formatting in R-08 (2026-04-25).
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

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

	/**
	 * Get the hardcoded default format options.
	 *
	 * These are the canonical defaults used on first install and as a fallback
	 * when no admin-configured list is stored.  Separate from get_format_options()
	 * so the admin UI can reference them for a "Reset to defaults" operation.
	 *
	 * @return array<string, string>  key → label
	 */
	public static function get_default_format_options(): array {
		return array(
			'ebook'     => __( 'eBook (EPUB)',     'my-book-showroom' ),
			'paperback' => __( 'Paperback',        'my-book-showroom' ),
			'hardcover' => __( 'Hardcover',        'my-book-showroom' ),
			'audiobook' => __( 'Audiobook',        'my-book-showroom' ),
			'boardbook' => __( 'Board Book',       'my-book-showroom' ),
			'special'   => __( 'Special Edition',  'my-book-showroom' ),
			'other'     => __( 'Other',            'my-book-showroom' ),
		);
	}

	/**
	 * Get format options for the repeater dropdown.
	 *
	 * F-023: Reads from the admin-configurable wp_options list when present.
	 * Falls back to the hardcoded defaults when no stored list exists or the
	 * stored JSON is invalid/empty.
	 *
	 * @return array<string, string>  key → label
	 */
	public static function get_format_options(): array {
		static $cache = null;
		if ( null !== $cache ) {
			return $cache;
		}
		$stored = get_option( 'mbs_setting_format_types', null );
		if ( null !== $stored ) {
			$decoded = json_decode( $stored, true );
			if ( is_array( $decoded ) && ! empty( $decoded ) ) {
				$cache = $decoded;
				return $cache;
			}
		}
		$cache = self::get_default_format_options();
		return $cache;
	}

	/**
	 * Get human-readable label for a format key.
	 *
	 * @param  string $format  Format key (e.g. 'paperback', 'ebook').
	 * @return string          Human-readable label, or ucfirst($format) on no match.
	 */
	public static function get_format_label( string $format ): string {
		$options = self::get_format_options();
		return $options[ $format ] ?? ucfirst( $format );
	}
}
