<?php
/**
 * Gets the regeneration data for the given bundle ID for the interactive form.
 *
 * @package EaLab\WP_Deployer
 */

use EaLab\WP_Deployer\vendor\Dakwamine\Component\ComponentBasedObject;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Exception\WpDeployException;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\TrackingManager;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\FileManager;

/**
 * Gets the regeneration data for the given bundle ID for the interactive form.
 *
 * @param string $bundle_id The bundle ID.
 *
 * @return array Data for the interactive form.
 *
 * @throws WpDeployException Deploy & Sync Content exceptions.
 */
function deploy_sync_content__interactive_form__get_data__regeneration( string $bundle_id ): array {
	if ( empty( $bundle_id ) ) {
		throw new WpDeployException( esc_html__( 'No "bundle ID" given.', 'deploy-sync-content' ) );
	}

	$context['bundle_id'] = $bundle_id;

	$tracking_manager = ComponentBasedObject::getRootComponentByClassName( TrackingManager::class, true );

	$file_manager = ComponentBasedObject::getRootComponentByClassName( FileManager::class, true );

	// Load the bundle from disk. We do this even when we just created the bundle for consistency.
	$deployment_bundle = $file_manager->loadDeploymentItemsData(
		$context['bundle_id'],
		$file_manager->getAllAvailableUuidsForImport( $context['bundle_id'] )
	);

	if ( null === $deployment_bundle ) {
		// translators: %s is a bundle ID.
		throw new WpDeployException( sprintf( esc_html__( 'Could not load the bundle “%s” from the file system.', 'deploy-sync-content' ), esc_html( $bundle_id ) ) );
	}

	require_once __DIR__ . '/interactive-form--get-data.php';

	return deploy_sync_content__interactive_form__get_data(
		$deployment_bundle,
		array(
			// Regeneration is always for export.
			'form_context' => 'export',
			'build_state'  => $context['build_state'] ?? new Deploy_Sync_Content__State(),
			'bundle_id'    => $bundle_id,
		)
	);
}
