<?php
/**
 * Base class for Builder_Mosaic_Provider classes
 *
 * @package Themify
 */
abstract class Builder_Mosaic_Provider {

	public static function is_available():bool {
		return true;
	}


	public abstract static function get_items(array &$settings,int $limit,int $paged ):array;
	
	/**
	 * Check if page is currently displayed inside lightbox window
	 *
	 * @return bool
	 */
	protected static function is_lightbox(string $key = 'tb-mosaic-lightbox' ):bool {
		return isset( $_GET[$key], $_GET['iframe'] )
			// this parameter is sent by getiFrameLink() in themify.gallery.js
			&& $_GET['iframe'] === 'true';
	}

	protected static function init():void{}

	

	/**
	 * Initialize data providers for the module
	 *
	 * Other plugins or themes can extend or add to this list
	 * by using the "builder_tiled_posts_providers" filter.
	 */
	private static function init_providers(string $type='all') {
		$coreList= array(
			'posts' => 'Builder_Mosaic_Posts',
			'terms' => 'Builder_Mosaic_Terms',
			'text' => 'Builder_Mosaic_Text',
			'gallery' => 'Builder_Mosaic_Gallery',
			'wc' => 'Builder_Mosaic_WooCommerce',
			'portfolio' => 'Builder_Mosaic_Portfolio',
			'ptb' => 'Builder_Mosaic_PTB',
			'tep' => 'Builder_Mosaic_Event_Posts',
			'nextgen' => 'Builder_Mosaic_Nextgen',
			'rss' => 'Builder_Mosaic_RSS',
			'files' => 'Builder_Mosaic_Files',
		);
		$providersList = apply_filters( 'builder_data_providers',$coreList );

		if ( $type !== 'all' ) {
			if ( ! isset( $providersList[ $type ] ) ) {
				return;
			}
			$providersList = array( $type => $providersList[ $type ] );
		}
		$providers=[];
		$dir = __DIR__.'/';
		foreach ( $providersList as $id => $provider ) {
			$path=isset($coreList[$id])?($dir . $id):$id;
			include_once( $path . '.php' );
			if ( class_exists( $provider ,false) && $provider::is_available() ) {
				$provider::init();
				$providers[ $id ] = $provider;
			}
		}
		return $providers;
	}

	/**
	 * Helper function to retrieve a provider instance
	 *
	 * @return object
	 */
	public static function get_providers(string $id='all' ) {
		$providers= self::init_providers($id);
		if($id==='all'){
			return $providers;
		}
		return isset( $providers[ $id ] ) ? $providers[ $id ] : null;
	}
}