'use strict';

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

var vue = require('vue');
var elementPlus = require('element-plus');
var index = require('../../../../hooks/store/useUploadTask/index.js');
var index$2 = require('../../../../hooks/base/usePasteFile/index.js');
var index$1 = require('../../plugins/confirmBox/index.js');
var iconsVue = require('@element-plus/icons-vue');
var index$3 = require('../fileComponent/index.js');
var index$4 = require('../previewFile/index.js');
var uploadFile = require('./uploadFile.js');

const _withScopeId = (n) => (vue.pushScopeId("data-v-c615121c"), n = n(), vue.popScopeId(), n);
const _hoisted_1 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ vue.createElementVNode(
  "div",
  { class: "el-upload__text gm-text-center !gm-text-xs gm-text-gray-500" },
  [
    /* @__PURE__ */ vue.createTextVNode(" \u5C06\u6587\u4EF6\u62D6\u5230\u6B64\u5904\uFF0C\u590D\u5236\u7C98\u8D34\uFF0C\u6216 "),
    /* @__PURE__ */ vue.createElementVNode("em", { class: "gm-text-blue-700" }, "\u70B9\u51FB\u4E0A\u4F20")
  ],
  -1
  /* HOISTED */
));
const _hoisted_2 = { class: "gm-relative gm-flex gm-items-center gm-justify-center gm-overflow-hidden gm-rounded-md" };
const _hoisted_3 = { class: "el-upload-list__item-actions" };
const _hoisted_4 = ["onClick"];
const _hoisted_5 = ["onClick"];
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "GmUploadFile"
  },
  __name: "UploadFile",
  props: /* @__PURE__ */ vue.mergeModels(uploadFile.uploadFileProps, {
    "fileList": { required: true, default: () => [] },
    "fileListModifiers": {},
    "checkoutFiles": { default: () => [] },
    "checkoutFilesModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(["handleRemove"], ["update:fileList", "update:checkoutFiles"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { uploadFileTask } = index.useUploadTaskStore();
    const fileList = vue.useModel(__props, "fileList");
    const checkoutFiles = vue.useModel(__props, "checkoutFiles");
    const loadingUids = vue.ref(/* @__PURE__ */ new Set());
    const httpRequest = async (options) => {
      loadingUids.value.add(options.file.uid);
      const { file } = options;
      const res = await uploadFileTask(file, {
        domainCode: props.domainCode,
        sceneCode: props.sceneCode,
        accept: props.accept,
        acl: props.acl,
        elOptions: options
      });
      return { ...res, file };
    };
    const checkAllUploadComplete = () => {
      if (loadingUids.value.size === 0) {
        props.onAllComplete?.();
      }
    };
    const coverFileSuccess = (response, uploadFile, uploadFiles) => {
      loadingUids.value.delete(uploadFile.uid);
      uploadFile.url = response.url;
      props.onSuccess?.(response, uploadFile, uploadFiles);
      checkAllUploadComplete();
    };
    const coverFileError = (error, uploadFile, uploadFiles) => {
      loadingUids.value.delete(uploadFile.uid);
      props.onError?.(error, uploadFile, uploadFiles);
      checkAllUploadComplete();
    };
    const beforeRemove = (file, files) => {
      if (props.disabled) {
        return false;
      }
      return props.beforeRemove ? props.beforeRemove(file, files) : true;
    };
    const removeFile = (file, uploadFiles) => {
      const index = fileList.value.indexOf(file);
      if (index > -1) {
        fileList.value.splice(index, 1);
        emit("handleRemove", { file, index });
        return props.onRemove?.(file, uploadFiles);
      }
    };
    const handleRemove = async (file, uploadFiles) => {
      if (props.deleteTip) {
        await index$1.GmConfirmBox({ message: "\u786E\u5B9A\u5220\u9664\u8BE5\u6587\u4EF6\u5417\uFF1F" }, async () => {
        });
      }
      removeFile(file, uploadFiles);
    };
    const uploadFileRef = vue.ref();
    const pasteFileRef = vue.ref();
    const submit = () => {
      uploadFileRef.value.submit();
    };
    const abort = (file) => {
      uploadFileRef.value.abort(file);
    };
    const clearFiles = (status) => {
      uploadFileRef.value.clearFiles(status);
    };
    const handleStart = (rawFile) => {
      uploadFileRef.value.handleStart(rawFile);
    };
    const dialogVisible = vue.ref(false);
    const fileIndex = vue.ref(0);
    const handlePreview = (file) => {
      fileIndex.value = fileList.value.indexOf(file);
      dialogVisible.value = true;
    };
    const { onSuccess, onError } = index$2.usePasteFile(pasteFileRef, { accept: props.accept, disabled: props.disabled });
    onSuccess((files) => {
      if (Array.isArray(files) && files.length > 0) {
        files.forEach((file) => {
          if (file instanceof File) {
            const rawFile = file;
            handleStart(rawFile);
            if (props.autoUpload) {
              submit();
            }
          }
        });
      }
    });
    onError((error) => {
      console.error("\u7C98\u8D34\u9519\u8BEF:", error);
      elementPlus.ElMessageBox.alert(error.message, "\u63D0\u793A", {
        type: "error"
      });
    });
    __expose({
      submit,
      abort,
      clearFiles,
      handleStart,
      uploadFileRef
    });
    return (_ctx, _cache) => {
      const _component_el_icon = vue.resolveComponent("el-icon");
      const _component_el_button = vue.resolveComponent("el-button");
      const _component_el_checkbox = vue.resolveComponent("el-checkbox");
      const _component_el_upload = vue.resolveComponent("el-upload");
      const _component_el_checkbox_group = vue.resolveComponent("el-checkbox-group");
      return vue.openBlock(), vue.createElementBlock("div", null, [
        vue.createElementVNode(
          "div",
          {
            ref_key: "pasteFileRef",
            ref: pasteFileRef,
            class: vue.normalizeClass([{ "gm-upload-file-picture-card": _ctx.listType === "picture-card", "gm-upload-file-disabled": _ctx.disabled }, "gm-upload-file"])
          },
          [
            vue.createVNode(_component_el_checkbox_group, {
              modelValue: checkoutFiles.value,
              "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => checkoutFiles.value = $event)
            }, {
              default: vue.withCtx(() => [
                vue.createVNode(_component_el_upload, vue.mergeProps({
                  ref_key: "uploadFileRef",
                  ref: uploadFileRef
                }, props, {
                  "file-list": fileList.value,
                  "onUpdate:fileList": _cache[0] || (_cache[0] = ($event) => fileList.value = $event),
                  "before-remove": beforeRemove,
                  action: "",
                  "http-request": httpRequest,
                  "on-success": coverFileSuccess,
                  "on-error": coverFileError
                }), vue.createSlots({
                  default: vue.withCtx(() => [
                    !_ctx.disabled ? vue.renderSlot(_ctx.$slots, "default", { key: 0 }, () => [
                      _ctx.listType === "picture-card" ? (vue.openBlock(), vue.createElementBlock(
                        "div",
                        {
                          key: 0,
                          class: "gm-flex gm-flex-col gm-items-center gm-justify-center gm-rounded-md gm-border gm-border-dashed gm-p-1",
                          style: vue.normalizeStyle([{ "flex-direction": "column" }, { width: _ctx.width + "px", height: _ctx.height + "px" }])
                        },
                        [
                          vue.createVNode(_component_el_icon, {
                            size: 30,
                            color: "#999"
                          }, {
                            default: vue.withCtx(() => [
                              vue.createVNode(vue.unref(iconsVue.UploadFilled))
                            ]),
                            _: 1
                            /* STABLE */
                          }),
                          _hoisted_1
                        ],
                        4
                        /* STYLE */
                      )) : (vue.openBlock(), vue.createBlock(_component_el_button, {
                        key: 1,
                        type: "primary",
                        size: "default"
                      }, {
                        default: vue.withCtx(() => [
                          vue.createTextVNode("\u70B9\u51FB\u4E0A\u4F20")
                        ]),
                        _: 1
                        /* STABLE */
                      }))
                    ], true) : vue.createCommentVNode("v-if", true)
                  ]),
                  _: 2
                  /* DYNAMIC */
                }, [
                  _ctx.showFileList && _ctx.listType === "picture-card" ? {
                    name: "file",
                    fn: vue.withCtx(({ file }) => [
                      _ctx.checkbox ? (vue.openBlock(), vue.createBlock(_component_el_checkbox, {
                        key: 0,
                        value: file
                      }, null, 8, ["value"])) : vue.createCommentVNode("v-if", true),
                      vue.renderSlot(_ctx.$slots, "file", { file }, () => [
                        vue.createElementVNode(
                          "div",
                          {
                            style: vue.normalizeStyle({ width: _ctx.width + "px" })
                          },
                          [
                            vue.createElementVNode("div", _hoisted_2, [
                              vue.createVNode(vue.unref(index$3.GmFileComponent), {
                                file,
                                size: _ctx.width - 40,
                                url: _ctx.url,
                                controls: false,
                                "show-icon": true,
                                field: _ctx.field,
                                fileWidth: _ctx.width + "px",
                                fileHeight: _ctx.height + "px",
                                fileStyle: { border: "1px solid #dcdfe6", overflow: "hidden", borderRadius: "5px" }
                              }, null, 8, ["file", "size", "url", "field", "fileWidth", "fileHeight"]),
                              vue.createElementVNode("div", _hoisted_3, [
                                vue.createElementVNode("span", {
                                  class: "el-upload-list__item-preview",
                                  onClick: ($event) => handlePreview(file)
                                }, [
                                  vue.createVNode(_component_el_icon, null, {
                                    default: vue.withCtx(() => [
                                      vue.createVNode(vue.unref(iconsVue.View))
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  })
                                ], 8, _hoisted_4),
                                _ctx.showDelete ? (vue.openBlock(), vue.createElementBlock("span", {
                                  key: 0,
                                  class: "el-upload-list__item-delete",
                                  onClick: ($event) => handleRemove(file, fileList.value)
                                }, [
                                  vue.createVNode(_component_el_icon, null, {
                                    default: vue.withCtx(() => [
                                      vue.createVNode(vue.unref(iconsVue.Delete))
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  })
                                ], 8, _hoisted_5)) : vue.createCommentVNode("v-if", true)
                              ])
                            ]),
                            vue.renderSlot(_ctx.$slots, "fileBottom", { file }, void 0, true)
                          ],
                          4
                          /* STYLE */
                        )
                      ], true)
                    ]),
                    key: "0"
                  } : void 0
                ]), 1040, ["file-list"])
              ]),
              _: 3
              /* FORWARDED */
            }, 8, ["modelValue"]),
            vue.createVNode(vue.unref(index$4.GmPreviewFile), {
              dialogVisible: dialogVisible.value,
              "onUpdate:dialogVisible": _cache[2] || (_cache[2] = ($event) => dialogVisible.value = $event),
              "file-index": fileIndex.value,
              "onUpdate:fileIndex": _cache[3] || (_cache[3] = ($event) => fileIndex.value = $event),
              "file-list": fileList.value,
              controls: _ctx.controls,
              oncontextmenu: _ctx.oncontextmenu,
              controlslist: _ctx.controlslist,
              field: _ctx.field
            }, null, 8, ["dialogVisible", "file-index", "file-list", "controls", "oncontextmenu", "controlslist", "field"])
          ],
          2
          /* CLASS */
        )
      ]);
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=UploadFile.vue2.js.map
