/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { defineComponent, toRefs, ref, computed } from '@vue/composition-api';
import { CheckCircleFilledIcon, ErrorCircleFilledIcon } from '@ldesign/icons-vue';
import { a as abridgeName, g as getFileSizeText } from '../../../chunks/utils2.js';
import { Button } from '../../button/index.js';
import { commonProps } from '../const.js';
import useCommonClassName from '../../../hooks/useCommonClassName.js';
import { Loading } from '../../loading/index.js';
import useDrag from '../hooks/use-drag.js';
import { useGlobalIcon } from '../../../hooks/useGlobalIcon.js';
import { ImageViewer } from '../../image-viewer/index.js';
import '../../../chunks/defineProperty-19da3d4c.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-fe796db6.js';
import '../../../utils/withInstall.js';
import 'vue';
import 'lodash/capitalize';
import '../../../config.js';
import '../../button/button.js';
import '../../../utils/render-node.js';
import 'lodash';
import '../../../utils/ripple.js';
import '../../../chunks/set-style.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../button/props.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../chunks/index.js';
import '../../../utils/easing.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/helper2.js';
import '../../loading/style/css.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/style/css.js';
import '../../button/type.js';
import '../../config-provider/useConfig.js';
import '../../../hooks/useConfig.js';
import '../../image-viewer/image-viewer.js';
import '../../../hooks/useVModel.js';
import 'lodash/kebabCase';
import '../../../hooks/useDefaultValue.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../image-viewer/props.js';
import '../../image-viewer/base/ImageModalIcon.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../../hooks/render-lnode.js';
import '../../image-viewer/base/ImageViewerUtils.js';
import '../../tooltip/index.js';
import '../../../utils/map-props.js';
import '../../tooltip/tooltip.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import '../../popup/props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../popup/container.js';
import '../../tooltip/props.js';
import '../../tooltip/style/css.js';
import '../../tooltip/type.js';
import '../../image-viewer/utils.js';
import '../../image-viewer/base/ImageItem.js';
import '../../image-viewer/hooks/index.js';
import '../../image-viewer/base/ImageViewerModal.js';
import '../../dialog/index.js';
import '../../dialog/dialog.js';
import 'lodash/isNumber';
import 'lodash/throttle';
import '../../../utils/event.js';
import '../../../hooks/useDestroyOnClose.js';
import '../../dialog/props.js';
import '../../dialog/actions.js';
import 'lodash/isObject';
import '../../dialog/stack.js';
import '../../dialog/style/css.js';
import '../../dialog/type.js';
import '../../dialog/plugin.js';
import '../../image-viewer/const.js';
import '../../image-viewer/base/Container.js';
import '../../image-viewer/style/css.js';
import '../../image-viewer/type.js';

var DraggerFile = defineComponent({
  name: "UploadDraggerFile",
  props: {
    ...commonProps,
    trigger: Function,
    triggerUpload: Function,
    uploadFiles: Function,
    cancelUpload: Function,
    dragEvents: Object
  },
  setup(props) {
    const {
      displayFiles
    } = toRefs(props);
    const {
      sizeClassNames
    } = useCommonClassName();
    const uploadPrefix = `${props.classPrefix}-upload`;
    const drag = useDrag(props.dragEvents);
    const {
      dragActive
    } = drag;
    const draggerFileRef = ref();
    const classes = computed(() => [`${uploadPrefix}__dragger`, {
      [`${uploadPrefix}__dragger-center`]: !displayFiles.value[0]
    }, {
      [`${uploadPrefix}__dragger-error`]: displayFiles.value[0]?.status === "fail"
    }]);
    const icons = useGlobalIcon({
      CheckCircleFilledIcon: CheckCircleFilledIcon,
      ErrorCircleFilledIcon: ErrorCircleFilledIcon
    });
    return {
      drag,
      dragActive,
      draggerFileRef,
      classes,
      sizeClassNames,
      uploadPrefix,
      icons
    };
  },
  methods: {
    renderImage() {
      const h = this.$createElement;
      const file = this.displayFiles[0];
      if (!file) return null;
      const url = file.url || file.response?.url;
      return h("div", {
        "class": `${this.uploadPrefix}__dragger-img-wrap`
      }, [url && h(ImageViewer, {
        "attrs": {
          "images": [url],
          "trigger": (h2, _ref) => {
            let {
              open
            } = _ref;
            return h("img", {
              "attrs": {
                "src": url
              },
              "on": {
                "click": open
              },
              "style": {
                maxWidth: "100%",
                maxHeight: "100%"
              }
            });
          }
        },
        "style": {
          maxWidth: "120px",
          maxHeight: "120px"
        }
      })]);
    },
    renderUploading() {
      const h = this.$createElement;
      const file = this.displayFiles[0];
      if (!file) return null;
      if (file.status === "progress") {
        return h("div", {
          "class": `${this.uploadPrefix}__single-progress`
        }, [h(Loading), h("span", {
          "class": `${this.uploadPrefix}__single-percent`
        }, [file.percent, "%"])]);
      }
    },
    renderMainPreview() {
      const h = this.$createElement;
      const file = this.displayFiles[0];
      if (!file) return null;
      const {
        CheckCircleFilledIcon,
        ErrorCircleFilledIcon
      } = this.icons;
      const fileName = this.abridgeName ? abridgeName(file.name, ...this.abridgeName) : file.name;
      return h("div", {
        "class": `${this.uploadPrefix}__dragger-progress`
      }, [this.theme === "image" && this.renderImage(), h("div", {
        "class": `${this.uploadPrefix}__dragger-progress-info`
      }, [h("div", {
        "class": `${this.uploadPrefix}__dragger-text`
      }, [h("span", {
        "class": `${this.uploadPrefix}__single-name`
      }, [fileName]), file.status === "progress" && this.renderUploading(), file.status === "success" && h(CheckCircleFilledIcon), file.status === "fail" && h(ErrorCircleFilledIcon)]), h("small", {
        "class": `${this.sizeClassNames.small}`
      }, [this.locale.file.fileSizeText, "\uFF1A", getFileSizeText(file.size)]), h("small", {
        "class": `${this.sizeClassNames.small}`
      }, [this.locale.file.fileOperationDateText, "\uFF1A", file.uploadTime || "-"]), h("div", {
        "class": `${this.uploadPrefix}__dragger-btns`
      }, [["progress", "waiting"].includes(file.status) && !this.disabled && h(Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "class": `${this.uploadPrefix}__dragger-progress-cancel`,
        "on": {
          "click": e => this.cancelUpload?.({
            e,
            file: this.toUploadFiles[0] || this.files[0]
          })
        }
      }, [this.locale?.cancelUploadText]), !this.autoUpload && file.status === "waiting" && h(Button, {
        "attrs": {
          "variant": "text",
          "theme": "primary",
          "disabled": this.disabled
        },
        "on": {
          "click": () => this.uploadFiles?.()
        },
        "class": `${this.uploadPrefix}__dragger-upload-btn`
      }, [this.locale.triggerUploadText.normal])]), ["fail", "success"].includes(file?.status) && !this.disabled && h("div", {
        "class": `${this.uploadPrefix}__dragger-btns`
      }, [h(Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text",
          "disabled": this.disabled
        },
        "class": `${this.uploadPrefix}__dragger-progress-cancel`,
        "on": {
          "click": this.triggerUpload
        }
      }, [this.locale.triggerUploadText.reupload]), h(Button, {
        "attrs": {
          "theme": "danger",
          "variant": "text",
          "disabled": this.disabled
        },
        "class": `${this.uploadPrefix}__dragger-delete-btn`,
        "on": {
          "click": e => this.onRemove({
            e,
            index: 0,
            file
          })
        }
      }, [this.locale.triggerUploadText.delete])])])]);
    },
    renderDefaultDragElement() {
      const h = this.$createElement;
      const unActiveElement = h("div", [h("span", {
        "class": `${this.uploadPrefix}--highlight`
      }, [this.locale.triggerUploadText?.normal]), h("span", ["\xA0\xA0/\xA0\xA0", this.locale.dragger.draggingText])]);
      const activeElement = h("div", [this.locale.dragger.dragDropText]);
      return this.dragActive ? activeElement : unActiveElement;
    },
    getContent() {
      const h = this.$createElement;
      const file = this.displayFiles[0];
      if (file && ["progress", "success", "fail", "waiting"].includes(file.status)) return this.renderMainPreview();
      return h("div", {
        "class": `${this.uploadPrefix}__trigger`,
        "on": {
          "click": this.triggerUpload
        }
      }, [this.$scopedSlots.default?.(null) || this.renderDefaultDragElement()]);
    }
  },
  render() {
    const h = arguments[0];
    return h("div", {
      "ref": "draggerFileRef",
      "class": this.classes,
      "on": {
        "drop": this.drag.handleDrop,
        "dragenter": this.drag.handleDragenter,
        "dragover": this.drag.handleDragover,
        "dragleave": this.drag.handleDragleave
      }
    }, [this.trigger?.(h, {
      files: this.displayFiles,
      dragActive: this.dragActive
    }) || this.getContent()]);
  }
});

export { DraggerFile as default };
//# sourceMappingURL=dragger-file.js.map
