<?php

require_once( __DIR__ . "/element-styles.php" );
require_once( __DIR__ . "/render-element.php" );
require_once( __DIR__ . "/render-from-map.php" );
require_once( __DIR__ . "/minify-html.php" );

// RENDER HELPER FUNCTIONS
if (!function_exists("renderElements")) {
	// used when no map is available (old plugin version, when groups were not available yet)
	function renderElements($data, $viewport = "desktop") {
		
		$flexMarkup = "";
		
		if(
			isset($data["maps"]) && 
			isset($data["maps"][$viewport]) &&
			isset($data["groupsSizes"]) && 
			isset($data["groupsSizes"][$viewport])
		) {
			$flexMarkup = flexRenderFromMap($data, $viewport);
		} else {
			// get flat map if available (for z-indexes)
			$flatMap = isset($data["zIndexMaps"]) ? $data["zIndexMaps"]["desktop"] : null;
			
			// get color names and send to renderer
			$colors = null;
			if(
				isset($data["globalOptions"]["themeColors"]) && 
				sizeof($data["globalOptions"]["themeColors"])
			) {
				$colors = $data["globalOptions"]["themeColors"];
			} else {
				$stylekitData = Flex_Builder_Data::getFlexStylekitStyles(get_the_ID());
				
				if($stylekitData) {
					$colors = $stylekitData['colors']['values'];
				}
			}
			
			// get elements obj
			if($viewport === "desktop") {
				$elements = isset($data["elements"]) ? $data["elements"] : [];
			} else {
				$elements = isset($data["mobile_elements"]) ? $data["mobile_elements"] : [];
			}
			
			if(isset($elements) && sizeof($elements)) {
				foreach ($elements as $element) {
					$flexMarkup .= flexRenderEl($element, $flatMap, $colors, $viewport, null, $data['blockID']);
				}
			}	
		}
		
		// return minify_html($flexMarkup);
		return $flexMarkup;
	}
}

?>
