import { isEFTemporal } from "./EFTemporal.js";
const updateAnimations = (element) => {
	element.style.setProperty("--ef-progress", `${Math.max(0, Math.min(1, element.currentTimeMs / element.durationMs)) * 100}%`);
	const timelineTimeMs = (element.rootTimegroup ?? element).currentTimeMs;
	if (element.startTimeMs > timelineTimeMs || element.endTimeMs < timelineTimeMs) {
		element.style.display = "none";
		return;
	}
	element.style.display = "";
	if (typeof element.getAnimations !== "function") return;
	const animations = element.getAnimations({ subtree: true });
	element.style.setProperty("--ef-duration", `${element.durationMs}ms`);
	element.style.setProperty("--ef-transition-duration", `${element.parentTimegroup?.overlapMs ?? 0}ms`);
	element.style.setProperty("--ef-transition-out-start", `${element.durationMs - (element.parentTimegroup?.overlapMs ?? 0)}ms`);
	for (const animation of animations) {
		if (animation.playState === "running") animation.pause();
		const effect = animation.effect;
		if (!(effect && effect instanceof KeyframeEffect)) continue;
		const target = effect.target;
		if (!target) continue;
		if (target.closest("ef-timegroup") !== element) continue;
		const timing = effect.getTiming();
		const duration = Number(timing.duration) ?? 0;
		const delay = Number(timing.delay) ?? 0;
		const iterations = Number(timing.iterations) ?? 1;
		const timeTarget = isEFTemporal(target) ? target : target.closest("ef-timegroup");
		if (!timeTarget) continue;
		const currentTime = timeTarget.ownCurrentTimeMs;
		if (currentTime < delay) {
			animation.currentTime = 0;
			continue;
		}
		const currentIteration = Math.floor((currentTime - delay) / duration);
		const currentIterationTime = (currentTime - delay) % duration;
		if (currentIteration >= iterations) {
			animation.currentTime = duration - .01;
			continue;
		}
		animation.currentTime = Math.min(currentIterationTime, duration - .01) + delay;
	}
};
export { updateAnimations };
