import { getRenderInfo } from "../getRenderInfo.js";

//#region src/render/EFRenderAPI.ts
function findRootTimegroup() {
	const workbench = document.querySelector("ef-workbench");
	if (workbench) {
		const timegroup = workbench.querySelector("ef-timegroup");
		if (timegroup) return timegroup;
	}
	return document.querySelector("ef-timegroup");
}
function setWorkbenchRendering(rendering) {
	const workbench = document.querySelector("ef-workbench");
	if (workbench) workbench.rendering = rendering;
}
async function waitForTimegroupDimensions(timegroup) {
	await Promise.all(Array.from(document.styleSheets).map((sheet) => {
		if (sheet.href) {
			const link = Array.from(document.querySelectorAll("link[rel=\"stylesheet\"]")).find((l) => l.href === sheet.href);
			if (link && !link.sheet) return new Promise((resolve) => {
				link.addEventListener("load", resolve);
				link.addEventListener("error", resolve);
			});
		}
		return Promise.resolve();
	}));
	timegroup.offsetHeight;
	const rect = timegroup.getBoundingClientRect();
	const hasOffset = timegroup.offsetWidth > 0 && timegroup.offsetHeight > 0;
	const hasRect = rect.width > 0 && rect.height > 0;
	const computedWidth = getComputedStyle(timegroup).width;
	const computedHeight = getComputedStyle(timegroup).height;
	if (!hasOffset && !hasRect && !(parseFloat(computedWidth) > 0 && parseFloat(computedHeight) > 0)) throw new Error(`Timegroup has no dimensions (${timegroup.offsetWidth}x${timegroup.offsetHeight}). Computed styles: width=${computedWidth}, height=${computedHeight}. Classes: "${timegroup.className}". \n\nTailwind CSS did not generate styles for these classes. Check that:\n1. Your Tailwind config 'content' array includes the HTML file\n2. Tailwind CSS is properly configured in your project\n3. The dev server successfully compiled CSS (check for Tailwind warnings above)`);
}
const api = {
	async renderStreaming(options = {}) {
		const timegroup = findRootTimegroup();
		if (!timegroup) throw new Error("No ef-timegroup found. Cannot render.");
		if (typeof window === "undefined" || !window.onRenderChunk) throw new Error("window.onRenderChunk is not set. Call page.exposeFunction('onRenderChunk', callback) from Playwright first.");
		setWorkbenchRendering(true);
		try {
			await waitForTimegroupDimensions(timegroup);
			await timegroup.waitForMediaDurations();
			const chunkWriter = new WritableStream({ write(chunk) {
				if (window.onRenderChunk) window.onRenderChunk(chunk);
			} });
			const onProgress = options.onProgress || window.onRenderProgress;
			const { renderTimegroupToVideo } = await import("../preview/renderTimegroupToVideo.js");
			await renderTimegroupToVideo(timegroup, {
				...options,
				customWritableStream: chunkWriter,
				onProgress,
				returnBuffer: false
			});
		} finally {
			setWorkbenchRendering(false);
		}
	},
	async render(options = {}) {
		const timegroup = findRootTimegroup();
		if (!timegroup) throw new Error("No ef-timegroup found. Cannot render.");
		setWorkbenchRendering(true);
		try {
			await waitForTimegroupDimensions(timegroup);
			await timegroup.waitForMediaDurations();
			const onProgress = options.onProgress || window.onRenderProgress;
			const { renderTimegroupToVideo } = await import("../preview/renderTimegroupToVideo.js");
			const buffer = await renderTimegroupToVideo(timegroup, {
				...options,
				returnBuffer: true,
				onProgress
			});
			if (!buffer) throw new Error("Render failed: no buffer returned");
			return buffer;
		} finally {
			setWorkbenchRendering(false);
		}
	},
	async getRenderInfo() {
		return getRenderInfo();
	},
	isReady() {
		return findRootTimegroup() !== null;
	}
};
if (typeof window !== "undefined") window.EF_RENDER = api;

//#endregion
//# sourceMappingURL=EFRenderAPI.js.map