"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useExpose = require("../composables/use-expose.js");
const index$1 = require("../icon/index.js");
const index$2 = require("../swipe/index.js");
const index = require("../popup/index.js");
const ImagePreviewItem = require("./ImagePreviewItem.js");
const props = require("../utils/props.js");
const create = require("../utils/create.js");
const dom = require("../utils/dom.js");
const basic = require("../utils/basic.js");
const constant = require("../utils/constant.js");
const interceptor = require("../utils/interceptor.js");
const [name, bem] = create.createNamespace("image-preview");
const popupProps = ["show", "teleport", "transition", "overlayStyle", "closeOnPopstate"];
const imagePreviewProps = {
  show: Boolean,
  loop: props.truthProp,
  images: props.makeArrayProp(),
  minZoom: props.makeNumericProp(1 / 3),
  maxZoom: props.makeNumericProp(3),
  overlay: props.truthProp,
  vertical: Boolean,
  closeable: Boolean,
  showIndex: props.truthProp,
  className: props.unknownProp,
  closeIcon: props.makeStringProp("clear"),
  transition: String,
  beforeClose: Function,
  doubleScale: props.truthProp,
  overlayClass: props.unknownProp,
  overlayStyle: Object,
  swipeDuration: props.makeNumericProp(300),
  startPosition: props.makeNumericProp(0),
  showIndicators: Boolean,
  closeOnPopstate: props.truthProp,
  closeOnClickImage: props.truthProp,
  closeOnClickOverlay: props.truthProp,
  closeIconPosition: props.makeStringProp("top-right"),
  teleport: [String, Object]
};
var stdin_default = Vue.defineComponent({
  name,
  props: imagePreviewProps,
  emits: ["scale", "close", "closed", "change", "longPress", "update:show"],
  setup(props2, {
    emit,
    slots
  }) {
    const swipeRef = Vue.ref();
    const activedPreviewItemRef = Vue.ref();
    const state = Vue.reactive({
      active: 0,
      rootWidth: 0,
      rootHeight: 0,
      disableZoom: false
    });
    const resize = () => {
      if (swipeRef.value) {
        const rect = index_esm.useRect(swipeRef.value.$el);
        state.rootWidth = rect.width;
        state.rootHeight = rect.height;
        swipeRef.value.resize();
      }
    };
    const emitScale = (args) => emit("scale", args);
    const updateShow = (show) => emit("update:show", show);
    const emitClose = () => {
      interceptor.callInterceptor(props2.beforeClose, {
        args: [state.active],
        done: () => updateShow(false)
      });
    };
    const setActive = (active) => {
      if (active !== state.active) {
        state.active = active;
        emit("change", active);
      }
    };
    const renderIndex = () => {
      if (props2.showIndex) {
        return Vue.createVNode("div", {
          "class": bem("index")
        }, [slots.index ? slots.index({
          index: state.active
        }) : `${state.active + 1} / ${props2.images.length}`]);
      }
    };
    const renderCover = () => {
      if (slots.cover) {
        return Vue.createVNode("div", {
          "class": bem("cover")
        }, [slots.cover()]);
      }
    };
    const onDragStart = () => {
      state.disableZoom = true;
    };
    const onDragEnd = () => {
      state.disableZoom = false;
    };
    const renderImages = () => Vue.createVNode(index$2.Swipe, {
      "ref": swipeRef,
      "lazyRender": true,
      "loop": props2.loop,
      "class": bem("swipe"),
      "vertical": props2.vertical,
      "duration": props2.swipeDuration,
      "initialSwipe": props2.startPosition,
      "showIndicators": props2.showIndicators,
      "indicatorColor": "white",
      "onChange": setActive,
      "onDragEnd": onDragEnd,
      "onDragStart": onDragStart
    }, {
      default: () => [props2.images.map((image, index2) => Vue.createVNode(ImagePreviewItem.default, {
        "ref": (item) => {
          if (index2 === state.active) {
            activedPreviewItemRef.value = item;
          }
        },
        "src": image,
        "show": props2.show,
        "active": state.active,
        "maxZoom": props2.maxZoom,
        "minZoom": props2.minZoom,
        "rootWidth": state.rootWidth,
        "rootHeight": state.rootHeight,
        "disableZoom": state.disableZoom,
        "doubleScale": props2.doubleScale,
        "closeOnClickImage": props2.closeOnClickImage,
        "closeOnClickOverlay": props2.closeOnClickOverlay,
        "vertical": props2.vertical,
        "onScale": emitScale,
        "onClose": emitClose,
        "onLongPress": () => emit("longPress", {
          index: index2
        })
      }, {
        image: slots.image
      }))]
    });
    const renderClose = () => {
      if (props2.closeable) {
        return Vue.createVNode(index$1.Icon, {
          "role": "button",
          "name": props2.closeIcon,
          "class": [bem("close-icon", props2.closeIconPosition), constant.HAPTICS_FEEDBACK],
          "onClick": emitClose
        }, null);
      }
    };
    const onClosed = () => emit("closed");
    const swipeTo = (index2, options) => {
      var _a;
      return (_a = swipeRef.value) == null ? void 0 : _a.swipeTo(index2, options);
    };
    useExpose.useExpose({
      resetScale: () => {
        var _a;
        (_a = activedPreviewItemRef.value) == null ? void 0 : _a.resetScale();
      },
      swipeTo
    });
    Vue.onMounted(resize);
    Vue.watch([dom.windowWidth, dom.windowHeight], resize);
    Vue.watch(() => props2.startPosition, (value) => setActive(+value));
    Vue.watch(() => props2.show, (value) => {
      const {
        images,
        startPosition
      } = props2;
      if (value) {
        setActive(+startPosition);
        Vue.nextTick(() => {
          resize();
          swipeTo(+startPosition, {
            immediate: true
          });
        });
      } else {
        emit("close", {
          index: state.active,
          url: images[state.active]
        });
      }
    });
    return () => Vue.createVNode(index.Popup, Vue.mergeProps({
      "class": [bem(), props2.className],
      "overlayClass": [bem("overlay"), props2.overlayClass],
      "onClosed": onClosed,
      "onUpdate:show": updateShow
    }, basic.pick(props2, popupProps)), {
      default: () => [renderClose(), renderImages(), renderIndex(), renderCover()]
    });
  }
});
exports.default = stdin_default;
exports.imagePreviewProps = imagePreviewProps;
