import { pushScopeId, popScopeId, createElementVNode, createTextVNode, defineComponent, useModel, ref, resolveComponent, createElementBlock, openBlock, normalizeClass, createVNode, withCtx, mergeProps, createSlots, renderSlot, createCommentVNode, createBlock, normalizeStyle, unref, mergeModels } from 'vue';
import { ElMessageBox } from 'element-plus';
import { useUploadTaskStore } from '../../../../hooks/store/useUploadTask/index.mjs';
import { usePasteFile } from '../../../../hooks/base/usePasteFile/index.mjs';
import { GmConfirmBox } from '../../plugins/confirmBox/index.mjs';
import { UploadFilled, View, Delete } from '@element-plus/icons-vue';
import { GmFileComponent } from '../fileComponent/index.mjs';
import { GmPreviewFile } from '../previewFile/index.mjs';
import { uploadFileProps } from './uploadFile.mjs';

const _withScopeId = (n) => (pushScopeId("data-v-c615121c"), n = n(), popScopeId(), n);
const _hoisted_1 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ createElementVNode(
  "div",
  { class: "el-upload__text gm-text-center !gm-text-xs gm-text-gray-500" },
  [
    /* @__PURE__ */ createTextVNode(" \u5C06\u6587\u4EF6\u62D6\u5230\u6B64\u5904\uFF0C\u590D\u5236\u7C98\u8D34\uFF0C\u6216 "),
    /* @__PURE__ */ 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__ */ defineComponent({
  ...{
    name: "GmUploadFile"
  },
  __name: "UploadFile",
  props: /* @__PURE__ */ mergeModels(uploadFileProps, {
    "fileList": { required: true, default: () => [] },
    "fileListModifiers": {},
    "checkoutFiles": { default: () => [] },
    "checkoutFilesModifiers": {}
  }),
  emits: /* @__PURE__ */ mergeModels(["handleRemove"], ["update:fileList", "update:checkoutFiles"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { uploadFileTask } = useUploadTaskStore();
    const fileList = useModel(__props, "fileList");
    const checkoutFiles = useModel(__props, "checkoutFiles");
    const loadingUids = 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 GmConfirmBox({ message: "\u786E\u5B9A\u5220\u9664\u8BE5\u6587\u4EF6\u5417\uFF1F" }, async () => {
        });
      }
      removeFile(file, uploadFiles);
    };
    const uploadFileRef = ref();
    const pasteFileRef = 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 = ref(false);
    const fileIndex = ref(0);
    const handlePreview = (file) => {
      fileIndex.value = fileList.value.indexOf(file);
      dialogVisible.value = true;
    };
    const { onSuccess, onError } = 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);
      ElMessageBox.alert(error.message, "\u63D0\u793A", {
        type: "error"
      });
    });
    __expose({
      submit,
      abort,
      clearFiles,
      handleStart,
      uploadFileRef
    });
    return (_ctx, _cache) => {
      const _component_el_icon = resolveComponent("el-icon");
      const _component_el_button = resolveComponent("el-button");
      const _component_el_checkbox = resolveComponent("el-checkbox");
      const _component_el_upload = resolveComponent("el-upload");
      const _component_el_checkbox_group = resolveComponent("el-checkbox-group");
      return openBlock(), createElementBlock("div", null, [
        createElementVNode(
          "div",
          {
            ref_key: "pasteFileRef",
            ref: pasteFileRef,
            class: normalizeClass([{ "gm-upload-file-picture-card": _ctx.listType === "picture-card", "gm-upload-file-disabled": _ctx.disabled }, "gm-upload-file"])
          },
          [
            createVNode(_component_el_checkbox_group, {
              modelValue: checkoutFiles.value,
              "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => checkoutFiles.value = $event)
            }, {
              default: withCtx(() => [
                createVNode(_component_el_upload, 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
                }), createSlots({
                  default: withCtx(() => [
                    !_ctx.disabled ? renderSlot(_ctx.$slots, "default", { key: 0 }, () => [
                      _ctx.listType === "picture-card" ? (openBlock(), 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: normalizeStyle([{ "flex-direction": "column" }, { width: _ctx.width + "px", height: _ctx.height + "px" }])
                        },
                        [
                          createVNode(_component_el_icon, {
                            size: 30,
                            color: "#999"
                          }, {
                            default: withCtx(() => [
                              createVNode(unref(UploadFilled))
                            ]),
                            _: 1
                            /* STABLE */
                          }),
                          _hoisted_1
                        ],
                        4
                        /* STYLE */
                      )) : (openBlock(), createBlock(_component_el_button, {
                        key: 1,
                        type: "primary",
                        size: "default"
                      }, {
                        default: withCtx(() => [
                          createTextVNode("\u70B9\u51FB\u4E0A\u4F20")
                        ]),
                        _: 1
                        /* STABLE */
                      }))
                    ], true) : createCommentVNode("v-if", true)
                  ]),
                  _: 2
                  /* DYNAMIC */
                }, [
                  _ctx.showFileList && _ctx.listType === "picture-card" ? {
                    name: "file",
                    fn: withCtx(({ file }) => [
                      _ctx.checkbox ? (openBlock(), createBlock(_component_el_checkbox, {
                        key: 0,
                        value: file
                      }, null, 8, ["value"])) : createCommentVNode("v-if", true),
                      renderSlot(_ctx.$slots, "file", { file }, () => [
                        createElementVNode(
                          "div",
                          {
                            style: normalizeStyle({ width: _ctx.width + "px" })
                          },
                          [
                            createElementVNode("div", _hoisted_2, [
                              createVNode(unref(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"]),
                              createElementVNode("div", _hoisted_3, [
                                createElementVNode("span", {
                                  class: "el-upload-list__item-preview",
                                  onClick: ($event) => handlePreview(file)
                                }, [
                                  createVNode(_component_el_icon, null, {
                                    default: withCtx(() => [
                                      createVNode(unref(View))
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  })
                                ], 8, _hoisted_4),
                                _ctx.showDelete ? (openBlock(), createElementBlock("span", {
                                  key: 0,
                                  class: "el-upload-list__item-delete",
                                  onClick: ($event) => handleRemove(file, fileList.value)
                                }, [
                                  createVNode(_component_el_icon, null, {
                                    default: withCtx(() => [
                                      createVNode(unref(Delete))
                                    ]),
                                    _: 1
                                    /* STABLE */
                                  })
                                ], 8, _hoisted_5)) : createCommentVNode("v-if", true)
                              ])
                            ]),
                            renderSlot(_ctx.$slots, "fileBottom", { file }, void 0, true)
                          ],
                          4
                          /* STYLE */
                        )
                      ], true)
                    ]),
                    key: "0"
                  } : void 0
                ]), 1040, ["file-list"])
              ]),
              _: 3
              /* FORWARDED */
            }, 8, ["modelValue"]),
            createVNode(unref(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 */
        )
      ]);
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=UploadFile.vue2.mjs.map
