1import { ImageWrapperEvents } from './ImageWrapper.types';
2import { ImageSource } from '../Image.types';
3
4export function getImageWrapperEventHandler(
5  events: ImageWrapperEvents | undefined,
6  source: ImageSource
7) {
8  return {
9    onLoad: (event) => {
10      if (typeof window !== 'undefined') {
11        // this ensures the animation will run, since the starting class is applied at least 1 frame before the target class set in the onLoad event callback
12        window.requestAnimationFrame(() => {
13          events?.onLoad?.forEach((e) => e?.(event));
14        });
15      } else {
16        events?.onLoad?.forEach((e) => e?.(event));
17      }
18    },
19    onTransitionEnd: () => events?.onTransitionEnd?.forEach((e) => e?.()),
20    onError: () => events?.onError?.forEach((e) => e?.({ source: source || null })),
21  };
22}
23