"use strict";
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);
var fullscreen_provider_exports = {};
__export(fullscreen_provider_exports, {
  FullscreenProvider: () => FullscreenProvider
});
module.exports = __toCommonJS(fullscreen_provider_exports);
var import_react = __toESM(require("react"));
var import_extract_next = require("./../../../ricos-content/RicosContentAPI/extract-next.js");
var import_next_schema = require("./../../../ricos-schema/next-schema/index.js");
const loadFullscreenModal = () => Promise.resolve().then(() => __toESM(require(
  /* webpackChunkName: "ricos_FullscreenModalViewer"  */
  "./fullscreen-modal.js"
)));
const FullscreenModal = import_react.default.lazy(loadFullscreenModal);
const FullscreenProvider = (props) => {
  const { children, isMobile, fullscreenProps, content } = props;
  const [state, setState] = import_react.default.useState({ type: "closed" });
  const expandModeData = import_react.default.useMemo(() => {
    if (content) {
      return getImagesData(content);
    } else {
      return { images: [], imageMap: {} };
    }
  }, [props.content]);
  const addExpand = (config) => {
    const onExpand = (nodeId, innerIndex = 0) => {
      setState({
        type: "open",
        activeIndex: expandModeData ? expandModeData.imageMap[nodeId] + innerIndex : 0
      });
    };
    const newConfig = { ...config };
    const imageConfig = config["wix-draft-plugin-image"];
    const galleryConfig = config["wix-draft-plugin-gallery"];
    if (imageConfig && !imageConfig.onExpand) {
      newConfig["wix-draft-plugin-image"] = { ...imageConfig, onExpand };
    }
    if (galleryConfig && !galleryConfig.onExpand) {
      newConfig["wix-draft-plugin-gallery"] = { ...galleryConfig, onExpand };
    }
    return newConfig;
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.Fragment, null, children({ config: addExpand(props.config), onHover: () => loadFullscreenModal() }), state.type === "open" && /* @__PURE__ */ import_react.default.createElement(import_react.Suspense, { fallback: /* @__PURE__ */ import_react.default.createElement("div", null) }, /* @__PURE__ */ import_react.default.createElement(
    FullscreenModal,
    {
      images: expandModeData.images,
      onClose: () => setState({ type: "closed" }),
      index: state.activeIndex,
      isMobile,
      ...fullscreenProps,
      jsdomMode: props.jsdomMode,
      t: props.t
    }
  )));
};
const getImagesData = (content) => {
  const imagesData = {
    images: [],
    imageMap: {}
  };
  let index = 0;
  return (0, import_extract_next.extract)(content.nodes).filter((node) => [import_next_schema.Node_Type.IMAGE, import_next_schema.Node_Type.GALLERY].includes(node.type)).get().reduce((items, node) => {
    const { images, imageMap } = items;
    const galleryItems = convertDataToGalleryItems(node);
    const currentIndex = index;
    index += galleryItems.length;
    return {
      images: [...images, ...galleryItems],
      imageMap: {
        ...imageMap,
        // @TODO: currently indexing doesn't work when nodes don't have ids
        [node.id ?? ""]: currentIndex
      }
    };
  }, imagesData);
};
const convertDataToGalleryItems = (node) => {
  switch (node.type) {
    case import_next_schema.Node_Type.IMAGE:
      return node.imageData.image?.src && !node.imageData?.disableExpand ? [
        {
          image: { media: { ...node.imageData.image } },
          disableDownload: node.imageData.disableDownload,
          altText: node.imageData.altText
        }
      ] : [];
    case import_next_schema.Node_Type.GALLERY: {
      return node.galleryData?.disableExpand ? [] : node.galleryData.items.map((item) => {
        return { ...item, disableDownload: node.galleryData.disableDownload };
      });
    }
    default:
      return [];
  }
};
//# sourceMappingURL=fullscreen-provider.js.map
