import {BrowserSafeApis} from '@remotion/renderer/client';
import type {JobProgressCallback, RenderJob} from '@remotion/studio-server';
import {getRendererPortFromConfigFile} from '../config/preview-server';
import {convertEntryPointToServeUrl} from '../convert-entry-point-to-serve-url';
import {parsedCli} from '../parsed-cli';
import {renderStillFlow} from '../render-flows/still';

const {
	publicDirOption,
	askAIOption,
	experimentalClientSideRenderingOption,
	experimentalVisualModeOption,
	keyboardShortcutsOption,
	rspackOption,
	browserExecutableOption,
	bundleCacheOption,
} = BrowserSafeApis.options;

export const processStill = async ({
	job,
	remotionRoot,
	entryPoint,
	onProgress,
	addCleanupCallback,
}: {
	job: RenderJob;
	remotionRoot: string;
	entryPoint: string;
	onProgress: JobProgressCallback;
	addCleanupCallback: (label: string, cb: () => void) => void;
}) => {
	if (job.type !== 'still') {
		throw new Error('Expected still job');
	}

	const browserExecutable = browserExecutableOption.getValue({
		commandLine: parsedCli,
	}).value;

	const publicDir = publicDirOption.getValue({
		commandLine: parsedCli,
	}).value;
	const askAIEnabled = askAIOption.getValue({commandLine: parsedCli}).value;
	const experimentalClientSideRenderingEnabled =
		experimentalClientSideRenderingOption.getValue({
			commandLine: parsedCli,
		}).value;
	const experimentalVisualModeEnabled = experimentalVisualModeOption.getValue({
		commandLine: parsedCli,
	}).value;
	const keyboardShortcutsEnabled = keyboardShortcutsOption.getValue({
		commandLine: parsedCli,
	}).value;
	const shouldCache = bundleCacheOption.getValue({
		commandLine: parsedCli,
	}).value;
	const rspack = rspackOption.getValue({commandLine: parsedCli}).value;

	const fullEntryPoint = convertEntryPointToServeUrl(entryPoint);

	await renderStillFlow({
		remotionRoot,
		browser: 'chrome',
		browserExecutable,
		chromiumOptions: job.chromiumOptions,
		entryPointReason: 'same as Studio',
		envVariables: job.envVariables,
		height: null,
		width: null,
		fps: null,
		durationInFrames: null,
		fullEntryPoint,
		serializedInputPropsWithCustomSchema:
			job.serializedInputPropsWithCustomSchema,
		overwrite: true,
		port: getRendererPortFromConfigFile(),
		publicDir,
		puppeteerTimeout: job.delayRenderTimeout,
		jpegQuality: job.jpegQuality,
		remainingArgs: [],
		scale: job.scale,
		stillFrame: job.frame,
		compositionIdFromUi: job.compositionId,
		imageFormatFromUi: job.imageFormat,
		logLevel: job.logLevel,
		onProgress,
		indent: true,
		addCleanupCallback,
		cancelSignal: job.cancelToken.cancelSignal,
		outputLocationFromUi: job.outName,
		offthreadVideoCacheSizeInBytes: job.offthreadVideoCacheSizeInBytes,
		offthreadVideoThreads: job.offthreadVideoThreads,
		binariesDirectory: job.binariesDirectory,
		publicPath: null,
		chromeMode: job.chromeMode,
		audioLatencyHint: null,
		mediaCacheSizeInBytes: job.mediaCacheSizeInBytes,
		askAIEnabled,
		experimentalClientSideRenderingEnabled,
		experimentalVisualModeEnabled,
		keyboardShortcutsEnabled,
		rspack,
		shouldCache,
	});
};
