import { defineComponent, ref, computed, watch, resolveComponent, openBlock, createBlock, mergeProps, withCtx, createCommentVNode } from 'vue';
import { NOOP } from '@vue/shared';
import { ElUpload } from '../../upload/index2.mjs';
import ElUploadList from '../../upload/src/upload-list4.mjs';
import { uploadListTypes } from '../../upload/src/upload3.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';

const _sfc_main = defineComponent({
  name: "ElUploadV2",
  components: {
    ElUpload,
    ElUploadList
  },
  props: {
    value: {
      type: [Array, String],
      default: () => {
        return "";
      }
    },
    maxFileSize: {
      type: Number,
      default: 3e6
    },
    width: {
      type: Number,
      default: 400
    },
    height: {
      type: Number,
      default: 400
    },
    size: {
      type: Number,
      default: 3
    },
    action: {
      type: String,
      default: "/v2/file/file/batchUploadImg"
    },
    headers: {
      type: Object,
      default: () => ({})
    },
    method: {
      type: String,
      default: "post"
    },
    data: {
      type: Object,
      default: () => ({
        token: localStorage.getItem("token")
      })
    },
    multiple: {
      type: Boolean,
      default: false
    },
    showCropper: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: "file"
    },
    drag: {
      type: Boolean,
      default: false
    },
    withCredentials: Boolean,
    showFileList: {
      type: Boolean,
      default: true
    },
    accept: {
      type: String,
      default: ""
    },
    type: {
      type: String,
      default: "select"
    },
    responseHandle: {
      type: Function,
      default: (file) => file.data.list[0].raw
    },
    autoUpload: {
      type: Boolean,
      default: true
    },
    listType: {
      type: String,
      values: uploadListTypes,
      default: "picture-card"
    },
    httpRequest: {
      type: Function
    },
    disabled: Boolean,
    limit: {
      type: Number,
      default: null
    },
    onExceed: {
      type: Function,
      default: () => NOOP
    }
  },
  emits: ["update:value"],
  setup(props, { emit }) {
    const fileList = ref([]);
    const isSingleUpload = computed(() => {
      return props.type === "single";
    });
    watch(() => props.value, (value) => {
      if (props.type === "single") {
        fileList.value = value ? [value] : [];
      } else {
        fileList.value = value;
      }
    });
    const fileUrlList = computed(() => {
      if (isSingleUpload.value) {
        return [props.value || ""];
      } else {
        return props.value.map((e) => ({
          url: e
        }));
      }
    });
    const getEmitValue = (fileList2) => {
      if (isSingleUpload.value) {
        return fileList2[0];
      } else {
        return fileList2;
      }
    };
    const onSuccess = (res) => {
      emit("update:value", isSingleUpload.value ? res : props.value.concat([res]));
    };
    const onRemove = (file, fileList2) => {
      if (!fileList2) {
        emit("update:value", isSingleUpload.value ? "" : []);
        return;
      }
      emit("update:value", getEmitValue(fileList2.map((e) => e.url)));
    };
    const onChange = (file, fileList2) => {
      const list = (isSingleUpload.value ? [file] : fileList2).map((e) => {
        if (e.response) {
          if (props.responseHandle) {
            return props.responseHandle(e.response);
          }
          return e.response;
        } else {
          return e.url;
        }
      });
      emit("update:value", getEmitValue(list));
    };
    const getFileList = (file) => {
      return [{ url: file }];
    };
    return {
      uploadProps: props,
      fileUrlList,
      onSuccess,
      onRemove,
      onChange,
      isSingleUpload,
      getFileList
    };
  }
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  const _component_ElUploadList = resolveComponent("ElUploadList");
  const _component_ElUpload = resolveComponent("ElUpload");
  return openBlock(), createBlock(_component_ElUpload, mergeProps(_ctx.uploadProps, {
    "file-list": _ctx.fileUrlList,
    "show-file-list": !_ctx.isSingleUpload,
    "on-remove": _ctx.onRemove,
    "on-change": _ctx.onChange
  }), {
    default: withCtx(() => [
      _ctx.isSingleUpload && _ctx.uploadProps.value ? (openBlock(), createBlock(_component_ElUploadList, {
        key: 0,
        "list-type": "picture-card",
        files: _ctx.getFileList(_ctx.uploadProps.value),
        "customer-class": "qd-upload-list--single",
        onRemove: _ctx.onRemove
      }, null, 8, ["files", "onRemove"])) : createCommentVNode("v-if", true)
    ]),
    _: 1
  }, 16, ["file-list", "show-file-list", "on-remove", "on-change"]);
}
var UploadV2 = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/Users/wyf/work/element-plus/packages/components/upload-v2/src/index.vue"]]);

export { UploadV2 as default };
//# sourceMappingURL=index2.mjs.map
