import { t as defineConfigurableModule } from "./configurable-module-Bur4JaI3.js";
import { createContext, use, useEffect, useRef } from "react";
import { jsx } from "react/jsx-runtime";
import { ImageRenderer } from "@haklex/rich-renderer-image/static";
//#region src/modules/image/module-config.ts
var { createModule: createImageModule, useConfig: useImageConfig } = defineConfigurableModule({
	name: "image",
	defaultConfig: {}
});
//#endregion
//#region src/modules/image/registry.tsx
var ImageRegistryContext = createContext(null);
function createRegistry() {
	const entries = /* @__PURE__ */ new Set();
	return {
		register(entry) {
			entries.add(entry);
			return () => {
				entries.delete(entry);
			};
		},
		resolve(target) {
			const ordered = [...entries].sort((a, b) => {
				const relation = a.el.compareDocumentPosition(b.el);
				if (relation & Node.DOCUMENT_POSITION_FOLLOWING) return -1;
				if (relation & Node.DOCUMENT_POSITION_PRECEDING) return 1;
				return 0;
			});
			const images = ordered.map((entry) => entry.info);
			const index = ordered.findIndex((entry) => entry.el === target || entry.el.contains(target));
			return {
				images,
				index: index === -1 ? 0 : index
			};
		}
	};
}
function ImageRegistryProvider({ children }) {
	const registryRef = useRef(null);
	if (!registryRef.current) registryRef.current = createRegistry();
	return /* @__PURE__ */ jsx(ImageRegistryContext, {
		value: registryRef.current,
		children
	});
}
function useImageRegistry() {
	return use(ImageRegistryContext);
}
function useRegisterImage(ref, info) {
	const registry = use(ImageRegistryContext);
	const { src, alt, width, height, caption, thumbhash } = info;
	useEffect(() => {
		const el = ref.current;
		if (!registry || !el) return;
		return registry.register({
			el,
			info: {
				src,
				alt,
				width,
				height,
				caption,
				thumbhash
			}
		});
	}, [
		registry,
		ref,
		src,
		alt,
		width,
		height,
		caption,
		thumbhash
	]);
}
//#endregion
//#region src/modules/image/renderer.tsx
function toImageInfo(props) {
	return {
		src: props.src,
		alt: props.altText || void 0,
		width: props.width,
		height: props.height,
		caption: props.caption,
		thumbhash: props.thumbhash
	};
}
function InteractiveImageRenderer({ onImageClick, ...props }) {
	const figureRef = useRef(null);
	const registry = useImageRegistry();
	const info = toImageInfo(props);
	useRegisterImage(figureRef, info);
	const handleActivate = (target) => {
		if (!registry) {
			onImageClick({
				current: info,
				images: [info],
				index: 0,
				target
			});
			return;
		}
		const { images, index } = registry.resolve(target);
		onImageClick({
			current: images[index] ?? info,
			images,
			index,
			target
		});
	};
	return /* @__PURE__ */ jsx(ImageRenderer, {
		...props,
		ref: figureRef,
		onActivate: handleActivate
	});
}
var ComposedImageRenderer = (props) => {
	const { onImageClick } = useImageConfig();
	if (!onImageClick) return /* @__PURE__ */ jsx(ImageRenderer, { ...props });
	return /* @__PURE__ */ jsx(InteractiveImageRenderer, {
		...props,
		onImageClick
	});
};
//#endregion
export { createImageModule as i, ImageRenderer as n, ImageRegistryProvider as r, ComposedImageRenderer as t };
