<?php
/**
 * Export Function
 *
 * @author Jegstudio
 * @since 1.0.0
 * @package gutenverse-themes-builder
 */

namespace Gutenverse_Themes_Builder\Export;

/**
 * Class Export Function
 *
 * @package gutenverse-themes-builder
 */
class Export_Function {
	/**
	 * Create Functions.php File
	 *
	 * @param object $system .
	 * @param array  $data .
	 */
	public static function create_function_php( $system, $data ) {
		$placeholder = $system->get_contents( GUTENVERSE_THEMES_BUILDER_DIR . '/includes/data/functions.txt' );

		$theme_data = maybe_unserialize( $data['theme_data'] );
		$other      = maybe_unserialize( $data['other'] );
		$theme_mode = $data['theme_mode'];

		$placeholder             = str_replace( '{{constant}}', Misc::get_constant_name( $theme_data['slug'] ), $placeholder );
		$placeholder             = str_replace( '{{namespace}}', Misc::get_namespace( $theme_data['slug'] ), $placeholder );
		$placeholder             = str_replace( '{{slug}}', $theme_data['slug'], $placeholder );
		$placeholder             = str_replace( '{{author_name}}', $theme_data['author_name'], $placeholder );
		$placeholder             = str_replace( '{{theme_version}}', $theme_data['theme_version'], $placeholder );
		$additional_constant = array();

		if ( 'gutenverse-only' === $theme_mode ) {
			$companion = null;
			foreach ( $other['plugins'] as $value ) {
				if ( 'gutenverse-companion' === $value['value'] ) {
					$companion = $value;
					break;
				}
			}
			$companion_version     = isset( $companion['version'] ) ? $companion['version'] : '2.0.0';
			$additional_constant[] = "defined( 'GUTENVERSE_COMPANION_REQUIRED_VERSION' ) || define( 'GUTENVERSE_COMPANION_REQUIRED_VERSION', '" . $companion_version . "' );";
			$additional_constant[] = "defined( 'GUTENVERSE_FRAMEWORK_REQUIRED_VERSION' ) || define( 'GUTENVERSE_FRAMEWORK_REQUIRED_VERSION', '2.0.0' );";

		}

		$placeholder = str_replace( '{{additional_constant}}', join( "\n", $additional_constant ), $placeholder );

		$system->put_contents(
			gutenverse_themes_builder_theme_built_path() . 'functions.php',
			$placeholder,
			FS_CHMOD_FILE
		);
	}
}
