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

"use strict";
const _hoisted_1 = {
  key: 0,
  class: "file-item-wrapper"
};
const _hoisted_2 = ["index"];
const _hoisted_3 = { style: { "align-items": "center", "height": "24px" } };
const __default__ = defineComponent({
  name: "me-upload-file"
});
var script = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: /* @__PURE__ */ mergeModels({
    uploadRequest: { type: Function, required: false, default: () => Promise.resolve() },
    fileRequest: { type: Function, required: false, default: () => Promise.resolve() },
    drag: { type: Boolean, required: false, default: false },
    url: { type: String, required: true, default: "" },
    title: { type: String, required: false, default: "\u4E0A\u4F20\u6587\u4EF6" },
    width: { type: Number, required: false, default: 80 },
    height: { type: Number, required: false, default: 80 },
    fileWidth: { type: Number, required: false, default: 160 },
    fileHeight: { type: Number, required: false, default: 90 },
    maxCount: { type: Number, required: false, default: 1 },
    closeEvent: { type: Function, required: false, default: () => {
    } },
    uploadRules: { type: null, required: false, default: [] },
    module: { type: String, required: false }
  }, {
    "modelValue": { type: String },
    "modelModifiers": {}
  }),
  emits: /* @__PURE__ */ mergeModels(["closeEvent"], ["update:modelValue"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const inputVal = useModel(__props, "modelValue");
    const fileStore = useFile(props);
    const dragFileSuccess = ref(false);
    watch(
      inputVal,
      (newValue) => {
        if (fileStore.allowWatch) {
          fileStore.importData(newValue || "");
        } else {
          setTimeout(() => {
            fileStore.allowWatch = true;
          }, 100);
        }
      },
      { immediate: true }
    );
    const handleUploadSuccess = (res) => {
      res.data.domuuid = uuid();
      fileStore.fileList.value.push(res.data);
      dragFileSuccess.value = true;
    };
    const uploadCompleteEventHandler = (uploadResultList) => {
      fileStore.fileList.value.push(uploadResultList[0]);
      fileStore.allowWatch = false;
      inputVal.value = fileStore.getValue();
    };
    const setFileList = (uploadResultList) => {
      uploadResultList.forEach((item) => {
        item.domuuid = uuid();
        fileStore.fileList.value.push(item);
      });
      fileStore.allowWatch = false;
      inputVal.value = fileStore.getValue();
    };
    __expose({
      setFileList,
      openUpload: fileStore.openUpload,
      closeUpload: fileStore.closeUpload
    });
    const removeItem = (index) => {
      console.log(index);
      fileStore.fileList.value.splice(index, 1);
      fileStore.allowWatch = false;
      inputVal.value = fileStore.getValue();
      dragFileSuccess.value = false;
    };
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock(
        "div",
        {
          class: normalizeClass(["me-upload-file", { "is-drag": props.drag }])
        },
        [
          withDirectives(createVNode(unref(ElInput), {
            modelValue: inputVal.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => inputVal.value = $event)
          }, null, 8, ["modelValue"]), [
            [vShow, false]
          ]),
          !props.drag ? (openBlock(), createElementBlock(
            Fragment,
            { key: 0 },
            [
              unref(fileStore).fileList.value.length > 0 ? (openBlock(), createElementBlock("div", _hoisted_1, [
                (openBlock(true), createElementBlock(
                  Fragment,
                  null,
                  renderList(unref(fileStore).fileList.value, (item, index) => {
                    return openBlock(), createElementBlock("div", {
                      key: item.domuuid,
                      index
                    }, [
                      createVNode(script$1, {
                        index,
                        url: item.url,
                        name: item.fileName + "." + item.extraName,
                        width: props.fileWidth,
                        height: props.fileHeight,
                        onCloseEvent: removeItem
                      }, null, 8, ["index", "url", "name", "width", "height"])
                    ], 8, _hoisted_2);
                  }),
                  128
                  /* KEYED_FRAGMENT */
                ))
              ])) : createCommentVNode("v-if", true),
              createCommentVNode(" \u6253\u5F00\u4E0A\u4F20\u5BF9\u8BDD\u6846 "),
              unref(fileStore).fileList.value.length < props.maxCount ? (openBlock(), createElementBlock(
                "div",
                {
                  key: 1,
                  style: normalizeStyle([{ "float": "left" }, { width: `${props.width}px`, height: `${props.height}px` }])
                },
                [
                  createVNode(unref(MeFlexBox), {
                    class: "file-container",
                    onClick: unref(fileStore).openUpload
                  }, {
                    default: withCtx(() => [
                      createElementVNode("div", _hoisted_3, [
                        createVNode(unref(MeSvgIcon), {
                          name: "ri-file-upload-line",
                          size: 24,
                          style: { "color": "#767676" }
                        })
                      ])
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["onClick"])
                ],
                4
                /* STYLE */
              )) : createCommentVNode("v-if", true)
            ],
            64
            /* STABLE_FRAGMENT */
          )) : (openBlock(), createElementBlock(
            Fragment,
            { key: 1 },
            [
              !dragFileSuccess.value ? (openBlock(), createBlock(script$2, {
                key: 0,
                rules: __props.uploadRules,
                url: __props.url,
                onUploadSuccess: handleUploadSuccess,
                uploadRequest: __props.uploadRequest
              }, null, 8, ["rules", "url", "uploadRequest"])) : createCommentVNode("v-if", true),
              dragFileSuccess.value ? (openBlock(), createBlock(script$1, {
                key: 1,
                index: 0,
                url: unref(fileStore).fileList.value[0].url,
                name: unref(fileStore).fileList.value[0].fileName + "." + unref(fileStore).fileList.value[0].extraName,
                width: props.fileWidth,
                height: props.fileHeight,
                onCloseEvent: removeItem
              }, null, 8, ["url", "name", "width", "height"])) : createCommentVNode("v-if", true)
            ],
            64
            /* STABLE_FRAGMENT */
          )),
          createCommentVNode(" \u901A\u7528\u4E0A\u4F20\u7EC4\u4EF6 "),
          createVNode(unref(MeUpload), mergeProps({
            modelValue: unref(fileStore).uploadVisible.value,
            "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => unref(fileStore).uploadVisible.value = $event),
            uploadRequest: __props.uploadRequest,
            title: props.title,
            maxCount: props.maxCount,
            url: __props.url,
            rules: __props.uploadRules,
            uploadCompleteEvent: uploadCompleteEventHandler
          }, _ctx.$attrs), {
            footer: withCtx(() => [
              renderSlot(_ctx.$slots, "footer")
            ]),
            _: 3
            /* FORWARDED */
          }, 16, ["modelValue", "uploadRequest", "title", "maxCount", "url", "rules"])
        ],
        2
        /* CLASS */
      );
    };
  }
});

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