import { defineComponent, useModel, watch, ref, openBlock, createElementBlock, withDirectives, createVNode, unref, vShow, createCommentVNode, withCtx, Fragment, renderList, createBlock, normalizeStyle, createElementVNode, mergeProps, renderSlot, mergeModels } from 'vue';
import { VueDraggable } from 'vue-draggable-plus';
import { ElInput } from 'element-plus';
import { uuid } from '../../utils/uuid.mjs';
import { useImage } from './composables/useImage.mjs';
import { MeUpload } from '../../upload/index.mjs';
import { MeSvgIcon } from '../../svg-icon/index.mjs';
import { MeImageViewer } from '../../image-viewer/index.mjs';
import './upload-image-item.vue.mjs';
import { MeFlexBox } from '../../flex-box/index.mjs';
import script$1 from './upload-image-item.vue2.mjs';

"use strict";
const _hoisted_1 = { class: "me-upload-image" };
const _hoisted_2 = { style: { "align-items": "center", "height": "24px" } };
const __default__ = defineComponent({
  name: "me-upload-image"
});
var script = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: /* @__PURE__ */ mergeModels({
    url: { type: String, required: true, default: "" },
    uploadRequest: { type: Function, required: false, default: () => Promise.resolve() },
    imgUrlRequest: { type: Function, required: false, default: () => Promise.resolve() },
    title: { type: String, required: false, default: "\u4E0A\u4F20\u56FE\u7247" },
    width: { type: Number, required: false, default: 100 },
    height: { type: Number, required: false, default: 100 },
    itemStyle: { type: Object, required: false },
    maxCount: { type: Number, required: false, default: 1 },
    closeEvent: { type: Function, required: false, default: () => {
    } },
    uploadRules: { type: Array, required: false, default: () => [
      { name: "jpg", size: 1024 * 1024 * 5 },
      { name: "jpeg", size: 1024 * 1024 * 5 },
      { name: "png", size: 1024 * 1024 * 1 },
      { name: "gif", size: 1024 * 1024 * 1 }
    ] },
    previewImg: { type: Boolean, required: false, default: false },
    module: { type: String, required: false }
  }, {
    "modelValue": { type: String },
    "modelModifiers": {}
  }),
  emits: /* @__PURE__ */ mergeModels(["itemClick", "removeEvent"], ["update:modelValue"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const inputVal = useModel(__props, "modelValue");
    const imageStore = useImage(props);
    watch(
      inputVal,
      (newValue) => {
        if (imageStore.allowWatch) {
          imageStore.importData(newValue || "");
        } else {
          setTimeout(() => {
            imageStore.allowWatch = true;
          }, 100);
        }
      },
      { immediate: true }
    );
    const el = ref();
    const emit = __emit;
    const clickImageItem = (item, index) => {
      if (props.previewImg) {
        MeImageViewer(item.url);
      }
      emit("itemClick", item, index);
    };
    const uploadCompleteEventHandler = (uploadResultList) => {
      uploadResultList.forEach((item) => {
        imageStore.fileList.value.push(item);
      });
      imageStore.allowWatch = false;
      inputVal.value = imageStore.getValue();
    };
    const setFileList = (uploadResultList) => {
      uploadResultList.forEach((item) => {
        item.domuuid = uuid();
        imageStore.fileList.value.push(item);
      });
      imageStore.allowWatch = false;
      inputVal.value = imageStore.getValue();
    };
    __expose({
      setFileList,
      openUpload: () => imageStore.openUpload(),
      closeUpload: () => imageStore.closeUpload()
    });
    const onDraggableEnd = () => {
      imageStore.allowWatch = false;
      inputVal.value = imageStore.getValue();
    };
    const removeImageItem = (index) => {
      emit("removeEvent", index);
      imageStore.fileList.value.splice(index, 1);
      imageStore.allowWatch = false;
      inputVal.value = imageStore.getValue();
    };
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        withDirectives(createVNode(unref(ElInput), {
          modelValue: inputVal.value,
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => inputVal.value = $event)
        }, null, 8, ["modelValue"]), [
          [vShow, false]
        ]),
        createCommentVNode(" \u56FE\u7247\u5217\u8868 "),
        createVNode(unref(VueDraggable), {
          ref_key: "el",
          ref: el,
          modelValue: unref(imageStore).fileList.value,
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => unref(imageStore).fileList.value = $event),
          style: { "display": "flex", "float": "left" },
          onEnd: onDraggableEnd
        }, {
          default: withCtx(() => [
            (openBlock(true), createElementBlock(
              Fragment,
              null,
              renderList(unref(imageStore).fileList.value, (item, index) => {
                return openBlock(), createBlock(script$1, {
                  style: normalizeStyle([{ "margin-right": "10px" }, { width: `${props.width}px`, height: `${props.height}px` }]),
                  key: item.domuuid,
                  imgUrl: item.url,
                  imgId: item.id,
                  index,
                  onCloseEvent: removeImageItem,
                  onClick: ($event) => clickImageItem(item, index)
                }, null, 8, ["style", "imgUrl", "imgId", "index", "onClick"]);
              }),
              128
              /* KEYED_FRAGMENT */
            ))
          ]),
          _: 1
          /* STABLE */
        }, 8, ["modelValue"]),
        createCommentVNode(" \u6253\u5F00\u4E0A\u4F20\u5BF9\u8BDD\u6846 "),
        unref(imageStore).fileList.value.length < props.maxCount ? (openBlock(), createElementBlock(
          "div",
          {
            key: 0,
            style: normalizeStyle([{ "float": "left" }, { width: `${props.width}px`, height: `${props.height}px` }])
          },
          [
            createVNode(unref(MeFlexBox), {
              class: "image-container",
              style: { "width": "100%", "height": "100%" },
              onClick: unref(imageStore).openUpload
            }, {
              default: withCtx(() => [
                createElementVNode("div", _hoisted_2, [
                  createVNode(unref(MeSvgIcon), {
                    name: "ri-image-add-line",
                    size: 24,
                    style: { "color": "#767676" }
                  })
                ])
              ]),
              _: 1
              /* STABLE */
            }, 8, ["onClick"])
          ],
          4
          /* STYLE */
        )) : createCommentVNode("v-if", true),
        createCommentVNode(" \u901A\u7528\u4E0A\u4F20\u7EC4\u4EF6 "),
        createVNode(unref(MeUpload), mergeProps({
          modelValue: unref(imageStore).uploadVisible.value,
          "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => unref(imageStore).uploadVisible.value = $event),
          title: props.title,
          uploadRequest: __props.uploadRequest,
          maxCount: __props.maxCount,
          url: __props.url,
          rules: __props.uploadRules,
          uploadCompleteEvent: uploadCompleteEventHandler
        }, _ctx.$attrs), {
          footer: withCtx(() => [
            renderSlot(_ctx.$slots, "footer")
          ]),
          _: 3
          /* FORWARDED */
        }, 16, ["modelValue", "title", "uploadRequest", "maxCount", "url", "rules"])
      ]);
    };
  }
});

export { script as default };
//# sourceMappingURL=upload-image.vue2.mjs.map
