const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/plus/webPosLogin/components/MediaDisplay/index.tsx
/**
* MediaDisplay 媒体组件
* @description 通用媒体展示组件，支持图片/视频展示
* - 支持单个媒体或轮播（预留）
* - 支持全屏模式
* - 支持视频播放控制和状态回调
*/
const MediaDisplay = (0, react.memo)((props) => {
	var _config$carouselItems3;
	const { config, className = "", fullscreen = false, muted = true, loop = false, onVideoEnded, onVideoStateChange, canClose = true, onClose } = props;
	const videoRef = (0, react.useRef)(null);
	const [videoState, setVideoState] = (0, react.useState)("idle");
	const [isVideoEnded, setIsVideoEnded] = (0, react.useState)(false);
	/**
	* 获取当前要展示的媒体项
	* 如果只有一个媒体项，直接返回；否则返回第一个（轮播逻辑预留）
	*/
	const currentMedia = (0, react.useMemo)(() => {
		var _config$carouselItems;
		if (config === null || config === void 0 || (_config$carouselItems = config.carouselItems) === null || _config$carouselItems === void 0 ? void 0 : _config$carouselItems.length) return config.carouselItems[0];
		if (config === null || config === void 0 ? void 0 : config.logo) return {
			type: "image",
			url: config.logo
		};
		return null;
	}, [config === null || config === void 0 ? void 0 : config.carouselItems, config === null || config === void 0 ? void 0 : config.logo]);
	/**
	* 是否为视频类型
	*/
	const isVideo = (currentMedia === null || currentMedia === void 0 ? void 0 : currentMedia.type) === "video";
	/**
	* 是否为单一媒体（不需要显示切换控件）
	*/
	const isSingleMedia = (0, react.useMemo)(() => {
		var _config$carouselItems2;
		return ((config === null || config === void 0 || (_config$carouselItems2 = config.carouselItems) === null || _config$carouselItems2 === void 0 ? void 0 : _config$carouselItems2.length) || 0) <= 1;
	}, [config === null || config === void 0 || (_config$carouselItems3 = config.carouselItems) === null || _config$carouselItems3 === void 0 ? void 0 : _config$carouselItems3.length]);
	/**
	* 更新视频播放状态
	*/
	const updateVideoState = (0, react.useCallback)((state) => {
		setVideoState(state);
		onVideoStateChange === null || onVideoStateChange === void 0 || onVideoStateChange(state);
	}, [onVideoStateChange]);
	/**
	* 处理视频播放结束
	*/
	const handleVideoEnded = (0, react.useCallback)(() => {
		setIsVideoEnded(true);
		updateVideoState("ended");
		onVideoEnded === null || onVideoEnded === void 0 || onVideoEnded();
		if (canClose) onClose === null || onClose === void 0 || onClose();
	}, [
		canClose,
		onClose,
		onVideoEnded,
		updateVideoState
	]);
	/**
	* 监听 canClose 变化，如果视频已结束且 canClose 变为 true，触发关闭
	*/
	(0, react.useEffect)(() => {
		if (isVideoEnded && canClose) onClose === null || onClose === void 0 || onClose();
	}, [
		isVideoEnded,
		canClose,
		onClose
	]);
	/**
	* 视频事件处理
	*/
	(0, react.useEffect)(() => {
		const video = videoRef.current;
		if (!video || !isVideo) return;
		const handlePlay = () => updateVideoState("playing");
		const handlePause = () => updateVideoState("paused");
		const handleError = () => updateVideoState("error");
		const handleLoadedData = () => {
			video.play().catch((err) => {
				console.warn("[MediaDisplay] 视频自动播放失败:", err);
				updateVideoState("error");
			});
		};
		video.addEventListener("play", handlePlay);
		video.addEventListener("pause", handlePause);
		video.addEventListener("ended", handleVideoEnded);
		video.addEventListener("error", handleError);
		video.addEventListener("loadeddata", handleLoadedData);
		return () => {
			video.removeEventListener("play", handlePlay);
			video.removeEventListener("pause", handlePause);
			video.removeEventListener("ended", handleVideoEnded);
			video.removeEventListener("error", handleError);
			video.removeEventListener("loadeddata", handleLoadedData);
		};
	}, [
		isVideo,
		handleVideoEnded,
		updateVideoState
	]);
	/** 品牌标语 */
	const slogan = config === null || config === void 0 ? void 0 : config.slogan;
	/**
	* 渲染媒体内容
	*/
	const renderMedia = () => {
		if (!currentMedia) return null;
		if (currentMedia.type === "video") return /* @__PURE__ */ react.default.createElement("video", {
			style: { backgroundColor: currentMedia.backgroundColor },
			ref: videoRef,
			src: currentMedia.url,
			className: "media-display__video",
			muted,
			loop,
			playsInline: true,
			autoPlay: true
		});
		if (currentMedia.type === "logo") return /* @__PURE__ */ react.default.createElement("div", { className: "media-display__logo-wrapper" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Image, {
			style: currentMedia.logoStyle,
			src: currentMedia.url,
			preview: false
		}));
		return /* @__PURE__ */ react.default.createElement("div", { className: "media-display__image-wrapper" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Image, {
			style: {
				width: "100%",
				height: "100%",
				backgroundColor: currentMedia.backgroundColor
			},
			src: currentMedia.url,
			preview: false,
			alt: "Media",
			className: "media-display__image"
		}));
	};
	if (fullscreen) return /* @__PURE__ */ react.default.createElement("div", { className: `media-display media-display--fullscreen ${className}`.trim() }, renderMedia());
	console.log(currentMedia, "currentMedia");
	return /* @__PURE__ */ react.default.createElement("div", { className: `webpos-login-brand-panel-media ${className}`.trim() }, /* @__PURE__ */ react.default.createElement("div", { className: "webpos-login-brand-panel-media__content" }, /* @__PURE__ */ react.default.createElement("div", { className: "webpos-login-brand-panel-media__logo-wrapper" }, renderMedia()), slogan && /* @__PURE__ */ react.default.createElement("p", { className: "webpos-login-brand-panel-media__slogan" }, slogan), !isSingleMedia && /* @__PURE__ */ react.default.createElement("div", { className: "webpos-login-brand-panel-media__carousel-controls" })));
});
MediaDisplay.displayName = "MediaDisplay";
//#endregion
exports.default = MediaDisplay;
