	/**
	 * Compiler Config
	 *
	 * @var    Config
	 * @since 3.2.0
	 **/
	protected Config $config;

	/**
	 * Compiler Placeholder Reverse
	 *
	 * @var    Reverse
	 * @since 3.2.0
	 **/
	protected Reverse $reverse;

	/**
	 * Compiler Powers Parser
	 *
	 * @var    Parser
	 * @since 3.2.0
	 **/
	protected Parser $parser;

	/**
	 * Database object to query local DB
	 *
	 * @var    \JDatabaseDriver
	 * @since 3.2.0
	 **/
	protected \JDatabaseDriver $db;

	/**
	 * Database object to query local DB
	 *
	 * @var    CMSApplication
	 * @since 3.2.0
	 **/
	protected CMSApplication $app;

	/**
	 * Constructor.
	 *
	 * @param Config|null             $config  The compiler config object.
	 * @param Reverse|null            $reverse The compiler placeholder reverse object.
	 * @param Parser|null             $parser  The powers parser object.
	 * @param \JDatabaseDriver|null   $db      The Database Driver object.
	 * @param CMSApplication|null     $app     The CMS Application object.
	 *
	 * @throws \Exception
	 * @since 3.2.0
	 */
	public function __construct(?Config $config = null, ?Reverse $reverse = null, ?Parser $parser = null,
		?\JDatabaseDriver $db = null, ?CMSApplication $app = null)
	{
		$this->config = $config ?: Compiler::_('Config');
		$this->reverse = $reverse ?: Compiler::_('Placeholder.Reverse');
		$this->parser = $parser ?: Compiler::_('Power.Parser');
		$this->db = $db ?: Factory::getDbo();
		$this->app = $app ?: Factory::getApplication();
	}

	/**
	 * Set the JCB GUI code placeholder
	 *
	 * @param   string  $string  The code string
	 * @param   array   $config  The placeholder config values
	 *
	 * @return  string
	 * @since 3.2.0
	 */
	public function set(string $string, array $config): string
	{
		if (StringHelper::check($string))
		{
			if ($this->config->get('add_placeholders', false)
				&& $this->check($string) && ArrayHelper::check($config)
				&& isset($config['table']) && StringHelper::check($config['table'])
				&& isset($config['field']) && StringHelper::check($config['field'])
				&& isset($config['type']) && StringHelper::check($config['type'])
				&& isset($config['id']) && is_numeric($config['id']))
			{
				// if we have a key we must get the ID
				if (isset($config['key']) && StringHelper::check($config['key']) && $config['key'] !== 'id')
				{
					if (($id = GetHelper::var($config['table'], $config['id'], $config['key'], 'id')) !== false && is_numeric($id))
					{
						$config['id'] = $id;
					}
					else
					{
						// we must give a error message to inform the user of this issue. (should never happen)
						$this->app->enqueueMessage(
							Text::sprintf('ID mismatch was detected with the %s.%s.%s.%s GUI code field. So the placeholder was not set.',
								$config['table'], $config['field'],
								$config['key'], $config['id']
							), 'Error'
						);
						// check some config
						if (!isset($config['prefix']))
						{
							$config['prefix'] = '';
						}

						return $config['prefix'] . $string;
					}
				}
				// check some config
				if (!isset($config['prefix']))
				{
					$config['prefix'] = PHP_EOL;
				}
				// add placeholder based on type of code
				switch (strtolower((string) $config['type']))
				{
					// adding with html commenting
					case 'html':
						$front = $config['prefix'] . '<!--' . '[JCBGUI.';
						$sufix = '$$$$]-->' . PHP_EOL;
						$back  = '<!--[/JCBGUI' . $sufix;
						break;
					// adding with php commenting
					default:
						$front = $config['prefix'] . '/***' . '[JCBGUI.';
						$sufix = '$$$$]***/' . PHP_EOL;
						$back  = '/***[/JCBGUI' . $sufix;
						break;
				}

				return $front . $config['table'] . '.' . $config['field'] . '.'
					. $config['id'] . '.' . $sufix . $string . $back;
			}
			// check some config
			if (!isset($config['prefix']))
			{
				$config['prefix'] = '';
			}

			return $config['prefix'] . $string;
		}

		return $string;
	}

	/**
	 * search a file for gui code blocks that were updated in the IDE
	 *
	 * @param   string  $file          The file path to search
	 * @param   array   $placeholders  The values to replace in the code being stored
	 * @param   string  $today         The date for today
	 * @param   string  $target        The target path type
	 *
	 * @return  void
	 * @since 3.2.0
	 */
	public function search(string &$file, array &$placeholders, string &$today, string &$target)
	{
		// get file content
		$file_content = FileHelper::getContent($file);

		// get the USE statements (to reverse engineer super power keys)
		$use_statements = $this->parser->getUseStatements($file_content);

		$guiCode = [];
		// we add a new search for the GUI CODE Blocks
		$guiCode[] = GetHelper::allBetween(
			$file_content, '/***[JCB' . 'GUI<>', '/***[/JCBGUI' . '$$$$]***/'
		);
		$guiCode[] = GetHelper::allBetween(
			$file_content, '<!--[JCB' . 'GUI<>', '<!--[/JCBGUI' . '$$$$]-->'
		);

		if (($guiCode = ArrayHelper::merge($guiCode)) !== false
			&& ArrayHelper::check($guiCode, true))
		{
			foreach ($guiCode as $code)
			{
				$first_line = strtok($code, PHP_EOL);
				// get the GUI target details
				$query = explode('.', trim($first_line, '.'));
				// only continue if we have 3 values in the query
				if (is_array($query) && count($query) >= 3)
				{
					// cleanup the newlines around the code
					$code = trim(str_replace($first_line, '', (string) $code), PHP_EOL)
						. PHP_EOL;
					// set the ID
					$id = (int) $query[2];
					// make the field name save
					$field = FieldHelper::safe($query[1]);
					// make the table name save
					$table = StringHelper::safe($query[0]);
					// reverse placeholder as much as we can
					$code = $this->reverse->engine(
						$code, $placeholders, $target, $id, $field, $table, $use_statements
					);
					// update the GUI/Tables/Database
					$object           = new \stdClass();
					$object->id       = $id;
					$object->{$field} = base64_encode(
						(string) $code
					); // (TODO) this may not always work...
					// update the value in GUI
					$this->db->updateObject(
						'#__componentbuilder_' . (string) $table, $object, 'id'
					);
				}
			}
		}
	}

	/**
	 * search a code to see if there is already any custom
	 * code or other reasons not to add the GUI code placeholders
	 *
	 * @param   string  $code  The code to check
	 *
	 * @return  bool   true if GUI code placeholders can be added
	 * @since 3.2.0
	 */
	protected function check(string &$code): bool
	{
		// check for customcode placeholders
		// we do not add GUI wrapper placeholder to code
		// that already has any customcode placeholders
		return strpos($code, '$$$$') === false;
	}
