<?php
/**
 * File Path: helpers/class-mbs-section-order.php
 *
 * Section Order Helper
 *
 * Thin helper class consumed by the single-book template to determine which
 * sections to render and in what order.
 *
 * Canonical section keys are defined in MBS_Section_Helper::get_canonical_keys()
 * (UX-NAV-001 / Phase B) — long hyphenated form, 16 keys.
 *
 */

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

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

	/**
	 * Get the ordered list of visible sections for a book (front-end use).
	 *
	 * Delegates to MBS_Section_Order_DAO::get_order() which reads the
	 * mbs_section_order table and filters by is_visible = 1.
	 *
	 * The DAO is trusted unconditionally — an intentional [] (author hid all
	 * sections) is honoured rather than overridden by the canonical key list.
	 * The secondary empty-check fallback was removed in UX-NAV-001 Phase C.
	 *
	 * @param int $post_id Post ID, or 0 for the global default order.
	 * @return string[] Ordered array of visible section keys.
	 */
	public static function get_order( int $post_id ): array {
		if ( class_exists( 'MBS_Section_Order_DAO' ) ) {
			// Trust the DAO completely. An intentional [] (all sections hidden)
			// must be honoured — no secondary empty-check fallback.
			return array_map( 'sanitize_key', MBS_Section_Order_DAO::get_order( $post_id ) );
		}
		// Last-resort fallback — DAO class unavailable (autoload failure only).
		return MBS_Section_Helper::get_canonical_keys();
	}
}
