<?php
namespace SabaiApps\Directories\Component\Entity\Helper;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity;

class DisplaysHelper {

	public function help( Application $application, Entity\Model\Bundle $bundle, $excludeHidden = true, $useCache = true ) {
		$displays = array();
		if ( ! empty( $bundle->info['public'] )
			|| $application->Entity_BundleTypeInfo( $bundle, 'display_enable_detailed' )
		) {
			$displays['detailed'] = array(
				'label'  => _x( 'Detailed', 'display name', 'directories-pro' ),
				'weight' => 0,
			);
		}

		$weight = 50;
		foreach ( array_keys( $application->View_Modes( false, $useCache ) ) as $view_mode_name ) {
			if ( ( ! $view_mode = $application->View_Modes_impl( $view_mode_name, $bundle, true ) )
				|| ( ! $_displays = $view_mode->viewModeInfo( 'displays' ) )
			) {
				continue;
			}

			foreach ( $_displays as $display_name => $display ) {
				if ( is_array( $display ) ) {
					$displays[ $display_name ] = array(
						'label'  => $display['label'],
						'weight' => isset( $display['weight'] ) ? $display['weight'] : ++$weight,
					);
				} else {
					$displays[ $display_name ] = array(
						'label'  => $display,
						'weight' => ++$weight,
					);
				}
			}
		}
		$displays = $application->Filter( 'entity_displays', $displays, array( $bundle, $excludeHidden ) );
		uasort(
			$displays,
			function ( $a, $b ) {
				return $a['weight'] < $b['weight'] ? -1 : 1;
			}
		);
		foreach ( array_keys( $displays ) as $display_name ) {
			if ( $excludeHidden
				&& ! empty( $displays[ $display_name ]['hidden'] )
			) {
				continue;
			}

			$displays[ $display_name ] = $displays[ $display_name ]['label'];
		}

		return $displays;
	}
}
