/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, toRefs, ref, computed } from '@vue/composition-api';
import { CheckCircleFilledIcon, ErrorCircleFilledIcon } from '@ldesign/icons-vue';
import { abridgeName, getFileSizeText } from '../../_common/js/upload/utils.js';
import { Button } from '../../button/index.js';
import { commonProps } from '../constants.js';
import useCommonClassName from '../../hooks/useCommonClassName.js';
import { Loading } from '../../loading/index.js';
import useDrag from '../hooks/useDrag.js';
import { useGlobalIcon } from '../../hooks/useGlobalIcon.js';
import { ImageViewer } from '../../image-viewer/index.js';
import { renderTNodeJSX } from '../../utils/render-tnode.js';
import '../../_common/js/log/log.js';
import '../../utils/withInstall.js';
import 'vue';
import 'lodash/capitalize';
import '../../button/button.js';
import '../../utils/ripple.js';
import '../../_common/js/utils/set-style.js';
import '../../config.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../../_chunks/dep-2cb61c79.js';
import 'dayjs';
import '../../utils/mixins.js';
import '../../button/props.js';
import '../../loading/directive.js';
import 'lodash/isObject';
import 'lodash/mapKeys';
import 'lodash/isEqual';
import '../../loading/plugin.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../loading/loading.js';
import '../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../_common/js/loading/circle-adapter.js';
import '../../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import 'lodash/isArray';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../config-provider/useConfig.js';
import '../../image-viewer/image-viewer.js';
import '../../hooks/useVModel.js';
import '../../hooks/useDefaultValue.js';
import '../../hooks/useConfig.js';
import '../../utils/helper.js';
import '../../image-viewer/props.js';
import '../../image-viewer/base/Container.js';
import '../../image-viewer/base/ImageModalIcon.js';
import '../../hooks/tnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../hooks/render-tnode.js';
import '../../image-viewer/base/ImageViewerUtils.js';
import '../../tooltip/index.js';
import '../../utils/map-props.js';
import '../../tooltip/tooltip.js';
import '../../_chunks/dep-b6ac6e96.js';
import '../../popup/props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../utils/event.js';
import '../../popup/container.js';
import '../../popup/utils.js';
import '../../popup/trigger.js';
import '../../tooltip/props.js';
import '../../image-viewer/utils.js';
import '../../image-viewer/base/ImageItem.js';
import '../../image-viewer/hooks.js';
import '../../image-viewer/base/ImageViewerModal.js';
import '../../dialog/index.js';
import '../../dialog/dialog.js';
import 'lodash/throttle';
import '../../hooks/useDestroyOnClose.js';
import '../../_common/js/utils/getScrollbarWidth.js';
import '../../dialog/stack.js';
import '../../dialog/props.js';
import '../../dialog/actions.js';
import '../../dialog/plugin.js';
import '../../config-provider/config-provider.js';
import 'lodash/cloneDeep';
import '../../image-viewer/const.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var DraggerFile = defineComponent({
  name: "UploadDraggerFile",
  props: _objectSpread(_objectSpread({}, commonProps), {}, {
    trigger: Function,
    triggerUpload: Function,
    uploadFiles: Function,
    cancelUpload: Function,
    dragEvents: Object
  }),
  setup: function setup(props) {
    var _toRefs = toRefs(props),
      displayFiles = _toRefs.displayFiles;
    var _useCommonClassName = useCommonClassName(),
      sizeClassNames = _useCommonClassName.sizeClassNames;
    var uploadPrefix = "".concat(props.classPrefix, "-upload");
    var drag = useDrag(props.dragEvents);
    var dragActive = drag.dragActive;
    var draggerFileRef = ref();
    var classes = computed(function () {
      var _displayFiles$value$;
      return ["".concat(uploadPrefix, "__dragger"), _defineProperty({}, "".concat(uploadPrefix, "__dragger-center"), !displayFiles.value[0]), _defineProperty({}, "".concat(uploadPrefix, "__dragger-error"), ((_displayFiles$value$ = displayFiles.value[0]) === null || _displayFiles$value$ === void 0 ? void 0 : _displayFiles$value$.status) === "fail")];
    });
    var icons = useGlobalIcon({
      CheckCircleFilledIcon: CheckCircleFilledIcon,
      ErrorCircleFilledIcon: ErrorCircleFilledIcon
    });
    return {
      drag: drag,
      dragActive: dragActive,
      draggerFileRef: draggerFileRef,
      classes: classes,
      sizeClassNames: sizeClassNames,
      uploadPrefix: uploadPrefix,
      icons: icons
    };
  },
  methods: {
    renderImage: function renderImage() {
      var _file$response;
      var h = this.$createElement;
      if (!this.displayFiles.length) return;
      var file = this.displayFiles[0];
      var url = (file === null || file === void 0 ? void 0 : file.url) || (file === null || file === void 0 ? void 0 : (_file$response = file.response) === null || _file$response === void 0 ? void 0 : _file$response.url);
      return h("div", {
        "class": "".concat(this.uploadPrefix, "__dragger-img-wrap")
      }, [url && h(ImageViewer, {
        "attrs": {
          "images": [url],
          "trigger": function trigger(h2, _ref3) {
            var open = _ref3.open;
            return h("img", {
              "attrs": {
                "src": url
              },
              "on": {
                "click": open
              },
              "style": {
                maxWidth: "100%",
                maxHeight: "100%"
              }
            });
          }
        },
        "style": {
          maxWidth: "120px",
          maxHeight: "120px"
        }
      })]);
    },
    renderUploading: function renderUploading() {
      var h = this.$createElement;
      if (!this.displayFiles.length) return;
      var file = this.displayFiles[0];
      if ((file === null || file === void 0 ? void 0 : file.status) === "progress") {
        return h("div", {
          "class": "".concat(this.uploadPrefix, "__single-progress")
        }, [h(Loading), h("span", {
          "class": "".concat(this.uploadPrefix, "__single-percent")
        }, [file.percent, "%"])]);
      }
    },
    renderMainPreview: function renderMainPreview() {
      var _this = this,
        _this$locale;
      var h = this.$createElement;
      var file = this.displayFiles[0];
      var _this$icons = this.icons,
        CheckCircleFilledIcon = _this$icons.CheckCircleFilledIcon,
        ErrorCircleFilledIcon = _this$icons.ErrorCircleFilledIcon;
      var fileName = this.abridgeName ? abridgeName.apply(void 0, [file.name].concat(_toConsumableArray(this.abridgeName))) : file.name;
      var fileInfo = [h("div", {
        "class": "".concat(this.uploadPrefix, "__dragger-text"),
        "key": "info"
      }, [h("span", {
        "class": "".concat(this.uploadPrefix, "__single-name")
      }, [fileName]), file.status === "progress" && this.renderUploading(), file.status === "success" && h(CheckCircleFilledIcon), file.status === "fail" && h(ErrorCircleFilledIcon)]), h("small", {
        "class": "".concat(this.sizeClassNames.small),
        "key": "size"
      }, [this.locale.file.fileSizeText, "\uFF1A", getFileSizeText(file.size)]), h("small", {
        "class": "".concat(this.sizeClassNames.small),
        "key": "time"
      }, [this.locale.file.fileOperationDateText, "\uFF1A", file.uploadTime || "-"])];
      return h("div", {
        "class": "".concat(this.uploadPrefix, "__dragger-progress")
      }, [this.theme === "image" && this.renderImage(), h("div", {
        "class": "".concat(this.uploadPrefix, "__dragger-progress-info")
      }, [renderTNodeJSX(this, "fileListDisplay", {
        params: {
          files: this.displayFiles
        }
      }) || fileInfo, h("div", {
        "class": "".concat(this.uploadPrefix, "__dragger-btns")
      }, [["progress", "waiting"].includes(file.status) && !this.disabled && h(Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "class": "".concat(this.uploadPrefix, "__dragger-progress-cancel"),
        "on": {
          "click": function click(e) {
            var _this$cancelUpload;
            return (_this$cancelUpload = _this.cancelUpload) === null || _this$cancelUpload === void 0 ? void 0 : _this$cancelUpload.call(_this, {
              e: 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": {
          "theme": "primary",
          "variant": "text",
          "disabled": this.disabled
        },
        "on": {
          "click": function click() {
            var _this$uploadFiles;
            return (_this$uploadFiles = _this.uploadFiles) === null || _this$uploadFiles === void 0 ? void 0 : _this$uploadFiles.call(_this);
          }
        },
        "class": "".concat(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": "".concat(this.uploadPrefix, "__dragger-btns")
      }, [h(Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text",
          "disabled": this.disabled
        },
        "class": "".concat(this.uploadPrefix, "__dragger-progress-cancel"),
        "on": {
          "click": this.triggerUpload
        }
      }, [this.locale.triggerUploadText.reupload]), h(Button, {
        "attrs": {
          "theme": "danger",
          "variant": "text",
          "disabled": this.disabled
        },
        "class": "".concat(this.uploadPrefix, "__dragger-delete-btn"),
        "on": {
          "click": function click(e) {
            var _this$onRemove;
            return (_this$onRemove = _this.onRemove) === null || _this$onRemove === void 0 ? void 0 : _this$onRemove.call(_this, {
              e: e,
              index: 0,
              file: file
            });
          }
        }
      }, [this.locale.triggerUploadText["delete"]])])])]);
    },
    renderDefaultDragElement: function renderDefaultDragElement() {
      var _this$locale$triggerU;
      var h = this.$createElement;
      var unActiveElement = h("div", [h("span", {
        "class": "".concat(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])]);
      var activeElement = h("div", [this.locale.dragger.dragDropText]);
      return this.dragActive ? activeElement : unActiveElement;
    },
    getContent: function getContent() {
      var _this$$scopedSlots$de, _this$$scopedSlots;
      var h = this.$createElement;
      var file = this.displayFiles[0];
      if (file && (["progress", "success", "fail", "waiting"].includes(file.status) || !file.status)) return this.renderMainPreview();
      return h("div", {
        "class": "".concat(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: function render() {
    var _this$trigger;
    var 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
