var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/webPosLogin/components/MediaDisplay/index.tsx
var MediaDisplay_exports = {};
__export(MediaDisplay_exports, {
  default: () => MediaDisplay_default
});
module.exports = __toCommonJS(MediaDisplay_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_index = require("./index.less");
var MediaDisplay = (0, import_react.memo)((props) => {
  var _a;
  const {
    config,
    className = "",
    fullscreen = false,
    muted = true,
    loop = false,
    onVideoEnded,
    onVideoStateChange,
    canClose = true,
    onClose
  } = props;
  const videoRef = (0, import_react.useRef)(null);
  const [videoState, setVideoState] = (0, import_react.useState)("idle");
  const [isVideoEnded, setIsVideoEnded] = (0, import_react.useState)(false);
  const currentMedia = (0, import_react.useMemo)(() => {
    var _a2;
    if ((_a2 = config == null ? void 0 : config.carouselItems) == null ? void 0 : _a2.length) {
      return config.carouselItems[0];
    }
    if (config == null ? void 0 : config.logo) {
      return { type: "image", url: config.logo };
    }
    return null;
  }, [config == null ? void 0 : config.carouselItems, config == null ? void 0 : config.logo]);
  const isVideo = (currentMedia == null ? void 0 : currentMedia.type) === "video";
  const isSingleMedia = (0, import_react.useMemo)(() => {
    var _a2;
    const itemCount = ((_a2 = config == null ? void 0 : config.carouselItems) == null ? void 0 : _a2.length) || 0;
    return itemCount <= 1;
  }, [(_a = config == null ? void 0 : config.carouselItems) == null ? void 0 : _a.length]);
  const updateVideoState = (0, import_react.useCallback)((state) => {
    setVideoState(state);
    onVideoStateChange == null ? void 0 : onVideoStateChange(state);
  }, [onVideoStateChange]);
  const handleVideoEnded = (0, import_react.useCallback)(() => {
    setIsVideoEnded(true);
    updateVideoState("ended");
    onVideoEnded == null ? void 0 : onVideoEnded();
    if (canClose) {
      onClose == null ? void 0 : onClose();
    }
  }, [canClose, onClose, onVideoEnded, updateVideoState]);
  (0, import_react.useEffect)(() => {
    if (isVideoEnded && canClose) {
      onClose == null ? void 0 : onClose();
    }
  }, [isVideoEnded, canClose, onClose]);
  (0, import_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 ? void 0 : config.slogan;
  const renderMedia = () => {
    if (!currentMedia) {
      return null;
    }
    if (currentMedia.type === "video") {
      return /* @__PURE__ */ import_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__ */ import_react.default.createElement("div", { className: "media-display__logo-wrapper" }, /* @__PURE__ */ import_react.default.createElement(
        import_materials.Image,
        {
          style: currentMedia.logoStyle,
          src: currentMedia.url,
          preview: false
        }
      ));
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "media-display__image-wrapper" }, /* @__PURE__ */ import_react.default.createElement(
      import_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__ */ import_react.default.createElement("div", { className: `media-display media-display--fullscreen ${className}`.trim() }, renderMedia());
  }
  console.log(currentMedia, "currentMedia");
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `webpos-login-brand-panel-media ${className}`.trim() }, /* @__PURE__ */ import_react.default.createElement("div", { className: "webpos-login-brand-panel-media__content" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "webpos-login-brand-panel-media__logo-wrapper" }, renderMedia()), slogan && /* @__PURE__ */ import_react.default.createElement("p", { className: "webpos-login-brand-panel-media__slogan" }, slogan), !isSingleMedia && /* @__PURE__ */ import_react.default.createElement("div", { className: "webpos-login-brand-panel-media__carousel-controls" })));
});
MediaDisplay.displayName = "MediaDisplay";
var MediaDisplay_default = MediaDisplay;
