import { n as e } from "../chunks/button-CC0Jdx1j.js";
import { r as t } from "../chunks/grid-dI94AAbi.js";
import { o as n } from "../chunks/input-CZLUUggm.js";
import { i as r, n as i, r as a } from "../chunks/modal-Cll8YZ7b.js";
import { t as o } from "../chunks/input-files-aB8iaqGk.js";
import { useCallback as s, useEffect as c, useMemo as l, useRef as u, useState as d } from "react";
import { Fragment as f, jsx as p, jsxs as m } from "react/jsx-runtime";
import h from "react-easy-crop";
//#region src/image/lib/buildImageSrcSet.ts
function g(e) {
	return e.map((e) => "width" in e ? `${e.src} ${e.width}w` : `${e.src} ${e.density}x`).join(", ");
}
//#endregion
//#region src/image/lib/buildImageSources.ts
function _(e, t) {
	return (e ?? []).map((e) => ({
		srcSet: g(e.candidates),
		type: e.type,
		sizes: e.sizes ?? t
	})).filter((e) => e.srcSet.length > 0);
}
//#endregion
//#region src/image/ui/ImageCropper.tsx
var v = {
	x: 0,
	y: 0
};
function y({ image: e, aspect: t = 16 / 9, width: n = "100%", className: r, style: i, crop: a, defaultCrop: o = v, zoom: c, defaultZoom: l = 1, minZoom: u = 1, maxZoom: f = 3, zoomSpeed: m = .1, objectFit: g = "contain", disabled: _ = !1, onCropChange: y, onZoomChange: b, onCropComplete: x }) {
	let [S, C] = d(o), [w, T] = d(l), E = a ?? S, D = c ?? w, O = s((e) => {
		_ || (a === void 0 && C(e), y?.(e));
	}, [
		a,
		_,
		y
	]), k = s((e) => {
		_ || (c === void 0 && T(e), b?.(e));
	}, [
		_,
		b,
		c
	]), A = s((e, t) => {
		_ || x?.(t, e);
	}, [_, x]);
	return /* @__PURE__ */ p("div", {
		className: r,
		style: {
			position: "relative",
			width: n,
			aspectRatio: t,
			pointerEvents: _ ? "none" : void 0,
			...i
		},
		children: /* @__PURE__ */ p(h, {
			image: e,
			crop: E,
			zoom: D,
			aspect: t,
			minZoom: u,
			maxZoom: f,
			zoomSpeed: m,
			objectFit: g,
			onCropChange: O,
			onZoomChange: k,
			onCropComplete: A,
			cropperProps: { "aria-disabled": _ || void 0 }
		})
	});
}
//#endregion
//#region src/image/ui/ImagePlaceholder.tsx
function b({ width: e = "100%", height: t = "100%", aspectRatio: n = "16 / 9", className: r }) {
	return /* @__PURE__ */ p("div", {
		style: {
			width: e,
			height: t,
			aspectRatio: n
		},
		className: r,
		children: /* @__PURE__ */ p("svg", {
			width: e,
			height: t,
			viewBox: "0 0 200 200",
			xmlns: "http://www.w3.org/2000/svg",
			role: "img",
			fill: "none",
			rx: "10",
			ry: "10",
			style: { display: "block" },
			"aria-label": "Изображение недоступно",
			children: /* @__PURE__ */ m("g", {
				fill: "var(--surface-0)",
				children: [
					/* @__PURE__ */ p("rect", {
						x: "40",
						y: "50",
						width: "120",
						height: "100",
						rx: "10"
					}),
					/* @__PURE__ */ p("circle", {
						cx: "60",
						cy: "70",
						r: "8",
						fill: "var(--surface-2)"
					}),
					/* @__PURE__ */ p("path", {
						d: "M50 130 L75 100 L100 130 Z",
						fill: "var(--surface-2)"
					}),
					/* @__PURE__ */ p("path", {
						d: "M90 130 L120 90 L150 130 Z",
						fill: "var(--surface-2)"
					}),
					/* @__PURE__ */ p("line", {
						x1: "160",
						y1: "40",
						x2: "40",
						y2: "160",
						stroke: "var(--surface-0)",
						strokeWidth: "15",
						strokeLinecap: "round"
					}),
					/* @__PURE__ */ p("line", {
						x1: "160",
						y1: "40",
						x2: "40",
						y2: "160",
						stroke: "var(--surface-2)",
						strokeWidth: "5",
						strokeLinecap: "round"
					})
				]
			})
		})
	});
}
//#endregion
//#region src/image/ui/ImageRenderer.tsx
function x({ src: e, sources: t, alt: n, role: r, ariaLabel: i, caption: a, fallback: o, useFigure: s = !1, layout: l = "cover", width: h = "100%", height: g = "100%", aspectRatio: _, intrinsicWidth: v, intrinsicHeight: y, onLoad: x, onError: S, style: C, wrapperStyle: w, ...T }) {
	let E = u(null), [D, O] = d(0), [k, A] = d(!1), [j, M] = d(!1), [N, P] = d(!1);
	c(() => {
		if (typeof IntersectionObserver > "u") {
			let e = window.setTimeout(() => A(!0), 0);
			return () => window.clearTimeout(e);
		}
		let e = new IntersectionObserver(([t]) => {
			if (t.isIntersecting) {
				let n = Math.round(t.boundingClientRect.width);
				O(n), A(!0), e.disconnect();
			}
		}, {
			rootMargin: "100px",
			threshold: .01
		});
		return E.current && e.observe(E.current), () => e.disconnect();
	}, []);
	let F = l === "intrinsic", I = F ? "auto" : g, L = v && y ? `${v} / ${y}` : void 0, R = F ? L ?? _ ?? "auto" : _ ?? "16 / 9", z = F && R !== "auto" && R !== "unset" ? "100%" : I, B = D ? `${D}px` : "100vw", V = /* @__PURE__ */ p(b, {
		width: h,
		height: z,
		aspectRatio: R
	}), H = F ? {
		...C,
		display: "block",
		width: "100%",
		height: "auto",
		objectFit: void 0
	} : {
		display: "block",
		width: "100%",
		height: "100%",
		objectFit: l,
		...C
	}, U = /* @__PURE__ */ m(f, { children: [
		!j && !N && (o || V),
		k && !N && /* @__PURE__ */ m("picture", { children: [t?.map((e) => /* @__PURE__ */ p("source", {
			srcSet: e.srcSet,
			type: e.type,
			sizes: e.sizes ?? B
		}, `${e.type ?? "source"}-${e.srcSet}`)), /* @__PURE__ */ p("img", {
			src: e,
			alt: n,
			role: r,
			"aria-label": i,
			onLoad: (e) => {
				M(!0), x?.(e);
			},
			onError: (e) => {
				P(!0), S?.(e);
			},
			style: j ? H : { display: "none" },
			"aria-busy": !j,
			width: v,
			height: y,
			...T
		})] }),
		N && V
	] }), W = /* @__PURE__ */ p("div", {
		ref: E,
		style: {
			width: h,
			height: I,
			aspectRatio: R,
			...w
		},
		children: U
	});
	return s ? /* @__PURE__ */ m("figure", {
		className: "relative",
		children: [W, a && /* @__PURE__ */ p("figcaption", {
			style: {
				bottom: 2,
				right: 2,
				maxWidth: "calc(100% - 4px)"
			},
			className: "absolute paddingInlineMd textRight radiusSm fontSizeSm",
			children: a
		})]
	}) : W;
}
//#endregion
//#region src/image/lib/getImageSelectionKey.ts
function S(e, t) {
	return `${e.file.meta.name}-${e.file.meta.size}-${e.file.meta.lastModified}-${t}`;
}
//#endregion
//#region src/image/model/useImageSelectionState.ts
var C = {
	crop: {
		x: 0,
		y: 0
	},
	zoom: 1
};
function w({ value: e, defaultValue: t, disabled: n, onChange: r, onApply: i }) {
	let [a, o] = d(t ?? []), [c, u] = d(0), [f, p] = d({}), m = e ?? a, h = s((t) => {
		let n = t(m);
		e === void 0 && o(n), r?.(n);
	}, [
		r,
		m,
		e
	]), g = l(() => m.map((e) => e.file), [m]), _ = m.length === 0 ? 0 : Math.min(c, m.length - 1), v = m[_], y = v ? S(v, _) : void 0, b = y ? f[y] ?? C : C, x = m.length > 0, w = !n && x && m.every((e) => e.alt.trim() && e.crop), T = s((e) => {
		h((t) => e.map((e) => {
			let n = t.find((t) => t.file.file === e.file);
			return n ? {
				...n,
				file: e
			} : {
				file: e,
				alt: ""
			};
		}));
	}, [h]), E = s((e) => {
		h((t) => t.map((t, n) => n === _ ? {
			...t,
			alt: e
		} : t));
	}, [_, h]), D = s((e) => {
		h((t) => t.map((t, n) => n === _ ? {
			...t,
			crop: e
		} : t));
	}, [_, h]), O = s((e) => {
		y && p((t) => ({
			...t,
			[y]: {
				crop: e,
				zoom: t[y]?.zoom ?? 1
			}
		}));
	}, [y]), k = s((e) => {
		y && p((t) => ({
			...t,
			[y]: {
				crop: t[y]?.crop ?? {
					x: 0,
					y: 0
				},
				zoom: e
			}
		}));
	}, [y]);
	return {
		value: m,
		files: g,
		activeIndex: _,
		activeImage: v,
		activeCropperState: b,
		hasSelected: x,
		canApply: w,
		apply: s(() => {
			i?.(m);
		}, [i, m]),
		setFiles: T,
		setActiveIndex: u,
		setActiveAlt: E,
		setActiveCrop: D,
		setActiveCropperCrop: O,
		setActiveCropperZoom: k
	};
}
//#endregion
//#region src/image/lib/getImageCropPreviewStyle.ts
function T(e, t, n) {
	if (!e.crop || !t) return {
		width: "100%",
		aspectRatio: n,
		objectFit: "cover",
		display: "block"
	};
	let r = e.crop;
	return {
		position: "absolute",
		left: `${-(r.x / r.width) * 100}%`,
		top: `${-(r.y / r.height) * 100}%`,
		width: `${t.width / r.width * 100}%`,
		height: `${t.height / r.height * 100}%`,
		maxWidth: "none"
	};
}
//#endregion
//#region src/image/ui/ImageSelectionFields.tsx
var E = [
	"image/jpeg",
	"image/png",
	"image/webp",
	"image/avif"
];
function D({ aspect: e = 16 / 9, disabled: r, allowedMime: i = E, maxBytes: a, minimumCropWidth: s, onReadError: c, state: l }) {
	let u = l.activeImage?.crop, [f, h] = d({});
	return /* @__PURE__ */ m(t, {
		gap: "md",
		children: [
			/* @__PURE__ */ p(o, {
				label: "Файл изображения",
				allowedMime: i,
				value: l.files,
				onChange: l.setFiles,
				maxBytes: a,
				disabled: r,
				onReadError: c
			}),
			l.hasSelected && l.activeImage && /* @__PURE__ */ m("div", { children: [
				/* @__PURE__ */ p(t, {
					templateColumns: "repeat(auto-fill, minmax(10em, 1fr))",
					gap: "xs",
					children: l.value.map((t, n) => {
						let i = S(t, n);
						return /* @__PURE__ */ p("button", {
							type: "button",
							disabled: r,
							onClick: () => l.setActiveIndex(n),
							style: {
								padding: 0,
								border: n === l.activeIndex ? "2px solid var(--border-accent)" : "2px solid transparent",
								background: "transparent",
								cursor: r ? "default" : "pointer"
							},
							children: /* @__PURE__ */ p("span", {
								style: {
									position: "relative",
									display: "block",
									width: "100%",
									aspectRatio: e,
									overflow: "hidden"
								},
								children: /* @__PURE__ */ p("img", {
									src: t.file.dataUrl,
									alt: t.alt || `Предпросмотр изображения ${n + 1}`,
									style: T(t, f[i], e),
									onLoad: (e) => {
										let t = e.currentTarget;
										h((e) => ({
											...e,
											[i]: {
												width: t.naturalWidth,
												height: t.naturalHeight
											}
										}));
									}
								})
							})
						}, i);
					})
				}),
				/* @__PURE__ */ m("div", { children: [
					"Выберите область изображения",
					" ",
					u && /* @__PURE__ */ m("code", {
						className: s && u.width < s ? "statusError" : "statusSuccess",
						children: [
							u.width,
							"x",
							u.height,
							s && u.width < s ? " — недостаточная ширина" : ""
						]
					})
				] }),
				/* @__PURE__ */ p(y, {
					image: l.activeImage.file.dataUrl,
					crop: l.activeCropperState.crop,
					zoom: l.activeCropperState.zoom,
					aspect: e,
					disabled: r,
					onCropChange: l.setActiveCropperCrop,
					onZoomChange: l.setActiveCropperZoom,
					onCropComplete: l.setActiveCrop
				}, S(l.activeImage, l.activeIndex))
			] }),
			/* @__PURE__ */ p(n, {
				required: !0,
				disabled: !l.hasSelected || r,
				label: "Краткое описание изображения",
				placeholder: "",
				value: l.activeImage?.alt ?? "",
				onChange: l.setActiveAlt
			})
		]
	});
}
//#endregion
//#region src/image/ui/ImageSelectionModal.tsx
function O({ disabled: t, isOpen: n = !0, onClose: o = () => {}, title: s = "Загрузка нового изображения", ...c }) {
	let l = w({
		...c,
		disabled: t
	});
	return c.onApply ? /* @__PURE__ */ m(i, {
		isOpen: n,
		onClose: o,
		title: s,
		size: "xl",
		children: [/* @__PURE__ */ p(a, {
			scrollable: !0,
			children: /* @__PURE__ */ p(D, {
				...c,
				disabled: t,
				state: l
			})
		}), /* @__PURE__ */ p(r, { children: /* @__PURE__ */ p(e, {
			disabled: !l.canApply,
			tone: "success",
			onClick: l.apply,
			children: "Применить"
		}) })]
	}) : /* @__PURE__ */ p(i, {
		isOpen: n,
		onClose: o,
		title: s,
		size: "xl",
		children: /* @__PURE__ */ p(a, {
			scrollable: !0,
			children: /* @__PURE__ */ p(D, {
				...c,
				disabled: t,
				state: l
			})
		})
	});
}
//#endregion
//#region src/image/ui/ImageSelectionPanel.tsx
function k({ disabled: t, renderActions: n, ...r }) {
	let i = w({
		...r,
		disabled: t
	}), a = {
		value: i.value,
		activeIndex: i.activeIndex,
		hasSelected: i.hasSelected,
		canApply: i.canApply,
		apply: i.apply
	};
	return /* @__PURE__ */ m(f, { children: [/* @__PURE__ */ p(D, {
		...r,
		disabled: t,
		state: i
	}), n ? n(a) : r.onApply && /* @__PURE__ */ p("div", { children: /* @__PURE__ */ p(e, {
		disabled: !i.canApply,
		tone: "success",
		onClick: i.apply,
		children: "Применить"
	}) })] });
}
//#endregion
//#region src/image/ui/ImageView.tsx
function A({ image: e, alt: t, sizes: n, aspectRatio: r, ...i }) {
	let a = _(e.sources, n), o = e.candidates ? g(e.candidates) : void 0, s = r ?? (e.intrinsicWidth && e.intrinsicHeight ? `${e.intrinsicWidth} / ${e.intrinsicHeight}` : void 0);
	return /* @__PURE__ */ p(x, {
		...i,
		src: e.src,
		srcSet: o || void 0,
		sources: a,
		sizes: n,
		alt: t ?? e.alt,
		intrinsicWidth: e.intrinsicWidth,
		intrinsicHeight: e.intrinsicHeight,
		aspectRatio: s
	});
}
//#endregion
export { y as ImageCropper, b as ImagePlaceholder, x as ImageRenderer, O as ImageSelectionModal, k as ImageSelectionPanel, A as ImageView, _ as buildImageSources, g as buildImageSrcSet };

//# sourceMappingURL=index.js.map