"use strict";
const React = require("react");
const is = require("../_util/is.js");
const useIsFirstRender = require("../_util/hooks/useIsFirstRender.js");
const useMergeValue = require("../_util/hooks/useMergeValue.js");
const imagePreview = require("./image-preview.js");
const previewGroupContext = require("./previewGroupContext.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
function PreviewGroup(props, ref) {
  var children = props.children, srcList = props.srcList, infinite = props.infinite, propCurrentIndex = props.current, defaultCurrent = props.defaultCurrent, onChange = props.onChange, propVisible = props.visible, defaultVisible = props.defaultVisible, onVisibleChange = props.onVisibleChange, restProps = __rest(props, ["children", "srcList", "infinite", "current", "defaultCurrent", "onChange", "visible", "defaultVisible", "onVisibleChange"]);
  var _a = __read(useMergeValue(false, {
    value: propVisible,
    defaultValue: defaultVisible
  }), 2), visible = _a[0], setVisible = _a[1];
  var propPreviewUrlMap = React.useMemo(function() {
    return srcList ? new Map(srcList.map(function(url, index) {
      return [index, {
        url,
        preview: true
      }];
    })) : null;
  }, [srcList]);
  var isFirstRender = useIsFirstRender();
  var getPreviewUrlMap = function() {
    return propPreviewUrlMap ? new Map(propPreviewUrlMap) : /* @__PURE__ */ new Map();
  };
  var _b = __read(React.useState(getPreviewUrlMap()), 2), previewUrlMap = _b[0], setPreviewUrlMap = _b[1];
  var _c = __read(React.useState(/* @__PURE__ */ new Map()), 2), previewPropsMap = _c[0], setPreviewPropsMap = _c[1];
  React.useEffect(function() {
    if (isFirstRender)
      return;
    setPreviewUrlMap(getPreviewUrlMap());
  }, [propPreviewUrlMap]);
  var canPreviewUrlMap = new Map(Array.from(previewUrlMap).filter(function(_a2) {
    var _b2 = __read(_a2, 2), preview = _b2[1].preview;
    return preview;
  }).map(function(_a2) {
    var _b2 = __read(_a2, 2), id = _b2[0], url = _b2[1].url;
    return [id, url];
  }));
  var _d = __read(useMergeValue(0, {
    value: propCurrentIndex,
    defaultValue: defaultCurrent
  }), 2), currentIndex = _d[0], setCurrentIndex = _d[1];
  function registerPreviewUrl(id, url, preview) {
    if (!propPreviewUrlMap) {
      setPreviewUrlMap(function(pre) {
        return new Map(pre).set(id, {
          url,
          preview
        });
      });
    }
    return function unRegisterPreviewUrl() {
      if (!propPreviewUrlMap) {
        setPreviewUrlMap(function(pre) {
          var cloneMap = new Map(pre);
          var hasDelete = cloneMap.delete(id);
          return hasDelete ? cloneMap : pre;
        });
      }
    };
  }
  function registerPreviewProps(id, previewProps) {
    setPreviewPropsMap(function(pre) {
      return new Map(pre).set(id, is.isObject(previewProps) ? previewProps : {});
    });
    return function unRegisterPreviewProps() {
      setPreviewPropsMap(function(pre) {
        var cloneMap = new Map(pre);
        var hasDelete = cloneMap.delete(id);
        return hasDelete ? cloneMap : pre;
      });
    };
  }
  var refPreview = React.useRef();
  React.useImperativeHandle(ref, function() {
    return {
      reset: function() {
        refPreview.current && refPreview.current.reset();
      }
    };
  });
  var handleVisibleChange = function(newVisible, preVisible) {
    var _preVisible = is.isUndefined(preVisible) ? visible : preVisible;
    onVisibleChange && onVisibleChange(newVisible, _preVisible);
    setVisible(newVisible);
  };
  var handleSwitch = function(index) {
    onChange && onChange(index);
    setCurrentIndex(index);
  };
  var loopImageIndex = function(children2) {
    var index = 0;
    var loop = function(children3) {
      return React__default.default.Children.map(children3, function(child) {
        if (child && child.props && child.type) {
          var displayName = child.type.displayName;
          if (displayName === "Image") {
            return React__default.default.cloneElement(child, {
              _index: index++
            });
          }
        }
        if (child && child.props && child.props.children) {
          return React__default.default.cloneElement(child, {
            children: loop(child.props.children)
          });
        }
        return child;
      });
    };
    return loop(children2);
  };
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(previewGroupContext.PreviewGroupContext.Provider, {
    value: {
      previewGroup: true,
      previewUrlMap: canPreviewUrlMap,
      previewPropsMap,
      infinite,
      currentIndex,
      setCurrentIndex: handleSwitch,
      setPreviewUrlMap,
      registerPreviewUrl,
      registerPreviewProps,
      visible,
      handleVisibleChange
    },
    children: [loopImageIndex(children), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(imagePreview, {
      ...__assign({
        ref: refPreview,
        src: "",
        visible,
        onVisibleChange: handleVisibleChange
      }, restProps)
    })]
  });
}
var PreviewGroupComponent = React.forwardRef(PreviewGroup);
PreviewGroupComponent.displayName = "ImagePreviewGroup";
const ImagePreviewGroup = PreviewGroupComponent;
module.exports = ImagePreviewGroup;
//# sourceMappingURL=image-preview-group.js.map
