'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var elementPlus = require('element-plus');
var index = require('../../flex-box/index.js');
var index$2 = require('../../upload/index.js');
var index$1 = require('../../svg-icon/index.js');
var uuid = require('../../utils/uuid.js');
require('./upload-file-item.vue.js');
var useFile = require('./composables/useFile.js');
require('./upload-file-drag.vue.js');
var uploadFileItem_vue_vue_type_script_setup_true_lang = require('./upload-file-item.vue2.js');
var uploadFileDrag_vue_vue_type_script_setup_true_lang = require('./upload-file-drag.vue2.js');

"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__ = vue.defineComponent({
  name: "me-upload-file"
});
var script = /* @__PURE__ */ vue.defineComponent({
  ...__default__,
  props: /* @__PURE__ */ vue.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__ */ vue.mergeModels(["closeEvent"], ["update:modelValue"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const inputVal = vue.useModel(__props, "modelValue");
    const fileStore = useFile.useFile(props);
    const dragFileSuccess = vue.ref(false);
    vue.watch(
      inputVal,
      (newValue) => {
        if (fileStore.allowWatch) {
          fileStore.importData(newValue || "");
        } else {
          setTimeout(() => {
            fileStore.allowWatch = true;
          }, 100);
        }
      },
      { immediate: true }
    );
    const handleUploadSuccess = (res) => {
      res.data.domuuid = uuid.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.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 vue.openBlock(), vue.createElementBlock(
        "div",
        {
          class: vue.normalizeClass(["me-upload-file", { "is-drag": props.drag }])
        },
        [
          vue.withDirectives(vue.createVNode(vue.unref(elementPlus.ElInput), {
            modelValue: inputVal.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => inputVal.value = $event)
          }, null, 8, ["modelValue"]), [
            [vue.vShow, false]
          ]),
          !props.drag ? (vue.openBlock(), vue.createElementBlock(
            vue.Fragment,
            { key: 0 },
            [
              vue.unref(fileStore).fileList.value.length > 0 ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_1, [
                (vue.openBlock(true), vue.createElementBlock(
                  vue.Fragment,
                  null,
                  vue.renderList(vue.unref(fileStore).fileList.value, (item, index) => {
                    return vue.openBlock(), vue.createElementBlock("div", {
                      key: item.domuuid,
                      index
                    }, [
                      vue.createVNode(uploadFileItem_vue_vue_type_script_setup_true_lang.default, {
                        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 */
                ))
              ])) : vue.createCommentVNode("v-if", true),
              vue.createCommentVNode(" \u6253\u5F00\u4E0A\u4F20\u5BF9\u8BDD\u6846 "),
              vue.unref(fileStore).fileList.value.length < props.maxCount ? (vue.openBlock(), vue.createElementBlock(
                "div",
                {
                  key: 1,
                  style: vue.normalizeStyle([{ "float": "left" }, { width: `${props.width}px`, height: `${props.height}px` }])
                },
                [
                  vue.createVNode(vue.unref(index.MeFlexBox), {
                    class: "file-container",
                    onClick: vue.unref(fileStore).openUpload
                  }, {
                    default: vue.withCtx(() => [
                      vue.createElementVNode("div", _hoisted_3, [
                        vue.createVNode(vue.unref(index$1.MeSvgIcon), {
                          name: "ri-file-upload-line",
                          size: 24,
                          style: { "color": "#767676" }
                        })
                      ])
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["onClick"])
                ],
                4
                /* STYLE */
              )) : vue.createCommentVNode("v-if", true)
            ],
            64
            /* STABLE_FRAGMENT */
          )) : (vue.openBlock(), vue.createElementBlock(
            vue.Fragment,
            { key: 1 },
            [
              !dragFileSuccess.value ? (vue.openBlock(), vue.createBlock(uploadFileDrag_vue_vue_type_script_setup_true_lang.default, {
                key: 0,
                rules: __props.uploadRules,
                url: __props.url,
                onUploadSuccess: handleUploadSuccess,
                uploadRequest: __props.uploadRequest
              }, null, 8, ["rules", "url", "uploadRequest"])) : vue.createCommentVNode("v-if", true),
              dragFileSuccess.value ? (vue.openBlock(), vue.createBlock(uploadFileItem_vue_vue_type_script_setup_true_lang.default, {
                key: 1,
                index: 0,
                url: vue.unref(fileStore).fileList.value[0].url,
                name: vue.unref(fileStore).fileList.value[0].fileName + "." + vue.unref(fileStore).fileList.value[0].extraName,
                width: props.fileWidth,
                height: props.fileHeight,
                onCloseEvent: removeItem
              }, null, 8, ["url", "name", "width", "height"])) : vue.createCommentVNode("v-if", true)
            ],
            64
            /* STABLE_FRAGMENT */
          )),
          vue.createCommentVNode(" \u901A\u7528\u4E0A\u4F20\u7EC4\u4EF6 "),
          vue.createVNode(vue.unref(index$2.MeUpload), vue.mergeProps({
            modelValue: vue.unref(fileStore).uploadVisible.value,
            "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => vue.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: vue.withCtx(() => [
              vue.renderSlot(_ctx.$slots, "footer")
            ]),
            _: 3
            /* FORWARDED */
          }, 16, ["modelValue", "uploadRequest", "title", "maxCount", "url", "rules"])
        ],
        2
        /* CLASS */
      );
    };
  }
});

exports.default = script;
//# sourceMappingURL=upload-file.vue2.js.map
