/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 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/use-common-classname.js';
import { Loading } from '../../loading/index.js';
import useDrag from '../hooks/use-drag.js';
import { useGlobalIcon } from '../../../hooks/use-global-icon.js';
import { ImageViewer } from '../../image-viewer/index.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../../config.js';
import '../../button/button.js';
import '../../../utils/render-node.js';
import '../../../utils/ripple.js';
import '../../../chunks/set-style.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
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';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/style';
import '../../button/type.js';
import '../../config-provider/useConfig.js';
import '../../../hooks/use-config.js';
import '../../image-viewer/image-viewer.js';
import 'lodash/kebabCase';
import '../../../hooks/use-default-value.js';
import '../../image-viewer/props.js';
import '../../image-viewer/base/ImageModalIcon.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import 'lodash/camelCase';
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 '../../../utils/helper.js';
import '../../popup/container.js';
import '../../tooltip/props.js';
import '../../tooltip/style';
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/use-destroy-onclose.js';
import '../../dialog/props.js';
import '../../dialog/mixins/action.js';
import 'lodash/isObject';
import '../../dialog/stack.js';
import '../../dialog/style';
import '../../dialog/type.js';
import '../../dialog/plugin.js';
import '../../image-viewer/const.js';
import '../../image-viewer/base/Container.js';
import '../../image-viewer/style';
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(() => {
      var _displayFiles$value$;
      return [`${uploadPrefix}__dragger`, {
        [`${uploadPrefix}__dragger-center`]: !displayFiles.value[0]
      }, {
        [`${uploadPrefix}__dragger-error`]: ((_displayFiles$value$ = displayFiles.value[0]) === null || _displayFiles$value$ === void 0 ? void 0 : _displayFiles$value$.status) === "fail"
      }];
    });
    const icons = useGlobalIcon({
      CheckCircleFilledIcon: CheckCircleFilledIcon,
      ErrorCircleFilledIcon: ErrorCircleFilledIcon
    });
    return {
      drag,
      dragActive,
      draggerFileRef,
      classes,
      sizeClassNames,
      uploadPrefix,
      icons
    };
  },
  methods: {
    renderImage() {
      var _file$response;
      const h = this.$createElement;
      const file = this.displayFiles[0];
      if (!file) return null;
      const url = file.url || ((_file$response = file.response) === null || _file$response === void 0 ? void 0 : _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() {
      var _this$locale;
      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 => {
            var _this$cancelUpload;
            return (_this$cancelUpload = this.cancelUpload) === null || _this$cancelUpload === void 0 ? void 0 : _this$cancelUpload.call(this, {
              e,
              file: this.toUploadFiles[0] || this.files[0]
            });
          }
        }
      }, [(_this$locale = this.locale) === null || _this$locale === void 0 ? void 0 : _this$locale.cancelUploadText]), !this.autoUpload && file.status === "waiting" && h(Button, {
        "attrs": {
          "variant": "text",
          "theme": "primary",
          "disabled": this.disabled
        },
        "on": {
          "click": () => {
            var _this$uploadFiles;
            return (_this$uploadFiles = this.uploadFiles) === null || _this$uploadFiles === void 0 ? void 0 : _this$uploadFiles.call(this);
          }
        },
        "class": `${this.uploadPrefix}__dragger-upload-btn`
      }, [this.locale.triggerUploadText.normal])]), ["fail", "success"].includes(file === null || file === void 0 ? void 0 : 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() {
      var _this$locale$triggerU;
      const h = this.$createElement;
      const unActiveElement = h("div", [h("span", {
        "class": `${this.uploadPrefix}--highlight`
      }, [(_this$locale$triggerU = this.locale.triggerUploadText) === null || _this$locale$triggerU === void 0 ? void 0 : _this$locale$triggerU.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() {
      var _this$$scopedSlots$de, _this$$scopedSlots;
      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$de = (_this$$scopedSlots = this.$scopedSlots).default) === null || _this$$scopedSlots$de === void 0 ? void 0 : _this$$scopedSlots$de.call(_this$$scopedSlots, null)) || this.renderDefaultDragElement()]);
    }
  },
  render() {
    var _this$trigger;
    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 = this.trigger) === null || _this$trigger === void 0 ? void 0 : _this$trigger.call(this, h, {
      files: this.displayFiles,
      dragActive: this.dragActive
    })) || this.getContent()]);
  }
});

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