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

use SabaiApps\Directories\Application;

class TaxonomyContentBundleTypesHelper {

	public function help( Application $application, $bundleType, $bundleGroup = null ) {
		if ( ! $bundle_types = $application->getPlatform()->getCache( 'entity_taxonomy_content_bundle_types' ) ) {
			$bundle_types = array();
			foreach ( $application->Entity_Bundles() as $bundle ) {
				if ( empty( $bundle->info['taxonomies'] ) ) {
					continue;
				}

				foreach ( array_keys( $bundle->info['taxonomies'] ) as $taxonomy_bundle_type ) {
					$bundle_types[ $taxonomy_bundle_type ][ $bundle->group ] = $bundle->name;
				}
			}
			$application->getPlatform()->setCache( $bundle_types, 'entity_taxonomy_content_bundle_types' );
		}

		if ( ! isset( $bundle_types[ $bundleType ] ) ) {
			return array();
		}

		return isset( $bundleGroup ) ? ( $bundle_types[ $bundleType ][ $bundleGroup ] ?? null ) : $bundle_types[ $bundleType ];
	}


	public function clear( Application $application ) {
		$application->getPlatform()->deleteCache( 'entity_taxonomy_content_bundle_types' );
	}
}
