<?php

// Exit if accessed directly.
if (!defined('ABSPATH')) exit;

/**
 * Class Ftm_Blocks_Helpers
 *
 * This class contains repetitive functions that
 * are used globally within the plugin.
 *
 * @package		FTMBLOCKS
 * @subpackage	Classes/Ftm_Blocks_Helpers
 * @author		IT
 * @since		1.0.0
 */
class Ftm_Blocks_Helpers
{

	/**
	 * ######################
	 * ###
	 * #### CALLABLE FUNCTIONS
	 * ###
	 * ######################
	 */

	/**
	 * HELPER COMMENT START
	 *
	 * Within this class, you can define common functions that you are 
	 * going to use throughout the whole plugin. 
	 * 
	 * Down below you will find a demo function called output_text()
	 * To access this function from any other class, you can call it as followed:
	 * 
	 * FTMBLOCKS()->helpers->output_text( 'my text' );
	 * 
	 */

	/**
	 * Output some text
	 *
	 * @param	string	$text	The text to output
	 * @since	1.0.0
	 *
	 * @return	void
	 */
	public function output_text($text = '')
	{
		echo $text;
	}

	/**
	 * HELPER COMMENT END
	 */


	public function webpack($file, $buildDirectory = 'dist')
	{
		static $manifest;
		static $manifestPath;

		if (is_null($manifest) || $manifestPath !== $buildDirectory) {
			$manifest = json_decode(
				file_get_contents(FTMBLOCKS_PLUGIN_DIR . $buildDirectory . '/manifest.json'),
				true
			);

			$manifestPath = $buildDirectory;
		}

		if (isset($manifest[$file])) {
			return FTMBLOCKS_PLUGIN_URL . trim($buildDirectory . '/' . $manifest[$file], '/');
		}

		throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
	}

	public static function getCSSFolder($type = "dir")
	{
		$dir = wp_upload_dir();

		if (is_dir($dir["basedir"] . "/" . FTMBLOCKS_CSS_FOLDER) || mkdir($dir["basedir"] . "/" . FTMBLOCKS_CSS_FOLDER)) {
			if ($type == "dir") {
				return $dir["basedir"] . "/" . FTMBLOCKS_CSS_FOLDER . "/";
			} elseif ($type == "url") {
				return $dir["baseurl"] . "/" . FTMBLOCKS_CSS_FOLDER . "/";
			}
		}
	}
}
