/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var tdesignIconsVue = require('tdesign-icons-vue');
var config = require('../config.js');
var loading_index = require('../loading/index.js');
var button_index = require('../button/index.js');
var upload_util = require('./util.js');
var upload_props = require('./props.js');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
require('../loading/loading.js');
require('vue');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/set-style.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../utils/classnames.js');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('lodash/isObject');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('../button/button.js');
require('../_chunks/dep-33ee4bce.js');
require('../button/props.js');
require('../utils/ripple.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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__default["default"](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 name = "".concat(config.prefix, "-upload-dragger");
var Dragger = utils_mixins["default"](configProvider_configReceiver["default"]("upload")).extend({
  name: name,
  components: {
    TLoading: loading_index.Loading,
    TButton: button_index.Button,
    CheckCircleFilledIcon: tdesignIconsVue.CheckCircleFilledIcon,
    ErrorCircleFilledIcon: tdesignIconsVue.ErrorCircleFilledIcon
  },
  props: {
    showUploadProgress: upload_props["default"].showUploadProgress,
    file: {
      type: Object
    },
    loadingFile: {
      type: Object
    },
    display: {
      type: String,
      validator: function validator(val) {
        return ["file", "file-input", "image", "custom"].includes(val);
      }
    },
    cancel: Function,
    trigger: Function,
    remove: Function,
    upload: Function,
    autoUpload: Boolean
  },
  data: function data() {
    return {
      target: null,
      dragActive: false
    };
  },
  computed: {
    isImage: function isImage() {
      return this.display === "image";
    },
    imageUrl: function imageUrl() {
      return this.loadingFile && this.loadingFile.url || this.file && this.file.url;
    },
    percent: function percent() {
      return this.loadingFile && this.loadingFile.percent;
    },
    inputName: function inputName() {
      return this.loadingFile && this.loadingFile.name || this.file && this.file.name;
    },
    classes: function classes() {
      return ["".concat(upload_util.UPLOAD_NAME, "__dragger"), _defineProperty__default["default"]({}, "".concat(upload_util.UPLOAD_NAME, "__dragger-center"), !this.loadingFile && !this.file), _defineProperty__default["default"]({}, "".concat(upload_util.UPLOAD_NAME, "__dragger-error"), this.loadingFile && this.loadingFile.status === "fail")];
    },
    size: function size() {
      return this.loadingFile && this.loadingFile.size || this.file && this.file.size;
    },
    showResultOperate: function showResultOperate() {
      var _this$file, _this$loadingFile;

      return Boolean(!this.loadingFile && ((_this$file = this.file) === null || _this$file === void 0 ? void 0 : _this$file.name)) || ["success", "fail"].includes((_this$loadingFile = this.loadingFile) === null || _this$loadingFile === void 0 ? void 0 : _this$loadingFile.status);
    }
  },
  methods: {
    handleDrop: function handleDrop(event) {
      event.preventDefault();
      this.$emit("change", event.dataTransfer.files);
      this.$emit("dragleave", event);
      this.dragActive = false;
    },
    handleDragenter: function handleDragenter(event) {
      this.target = event.target;
      event.preventDefault();
      this.$emit("dragenter", event);
      this.dragActive = true;
    },
    handleDragleave: function handleDragleave(event) {
      if (this.target !== event.target) return;
      event.preventDefault();
      this.$emit("dragleave", event);
      this.dragActive = false;
    },
    handleDragover: function handleDragover(event) {
      event.preventDefault();
    },
    renderDefaultDragElement: function renderDefaultDragElement() {
      var h = this.$createElement;
      var unActiveElement = h("div", [h("span", {
        "class": "".concat(config.prefix, "-upload--highlight")
      }, [this.global.triggerUploadText.normal]), h("span", ["\xA0\xA0/\xA0\xA0", this.global.dragger.draggingText])]);
      var activeElement = h("div", [this.global.dragger.dragDropText]);
      return this.dragActive ? activeElement : unActiveElement;
    },
    renderImage: function renderImage() {
      var h = this.$createElement;
      return h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-img-wrap")
      }, [this.imageUrl && h("img", {
        "attrs": {
          "src": this.imageUrl || "default.png"
        }
      })]);
    },
    renderUploading: function renderUploading() {
      var h = this.$createElement;

      if (this.loadingFile.status === "fail") {
        return h(tdesignIconsVue.ErrorCircleFilledIcon);
      }

      if (this.loadingFile.status === "progress" && this.showUploadProgress) {
        return h("div", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__single-progress")
        }, [h(loading_index.Loading), h("span", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__single-percent")
        }, [Math.min(this.loadingFile.percent, 99), "%"])]);
      }
    },
    reupload: function reupload(e) {
      this.remove(e);
      this.trigger(e);
    },
    renderProgress: function renderProgress() {
      var _this$loadingFile2,
          _this$loadingFile3,
          _this = this;

      var h = this.$createElement;
      return h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-progress")
      }, [this.isImage && this.renderImage(), h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-progress-info")
      }, [h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-text")
      }, [h("span", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__single-name")
      }, [upload_util.abridgeName(this.inputName)]), this.loadingFile && this.renderUploading(), !this.loadingFile && !!this.file && h(tdesignIconsVue.CheckCircleFilledIcon)]), h("small", {
        "class": "".concat(config.prefix, "-size-s")
      }, [this.global.file.fileSizeText, "\uFF1A", upload_util.returnFileSize(this.size)]), h("small", {
        "class": "".concat(config.prefix, "-size-s")
      }, [this.global.file.fileOperationDateText, "\uFF1A", upload_util.getCurrentDate()]), h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-btns")
      }, [["progress", "waiting"].includes((_this$loadingFile2 = this.loadingFile) === null || _this$loadingFile2 === void 0 ? void 0 : _this$loadingFile2.status) && h(button_index.Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-progress-cancel"),
        "on": {
          "click": this.cancel
        }
      }, [this.global.cancelUploadText]), !this.autoUpload && ((_this$loadingFile3 = this.loadingFile) === null || _this$loadingFile3 === void 0 ? void 0 : _this$loadingFile3.status) === "waiting" && h(button_index.Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "on": {
          "click": function click(e) {
            return _this.upload(_objectSpread({}, _this.loadingFile), e);
          }
        }
      }, [this.global.triggerUploadText.normal])]), this.showResultOperate && h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-btns")
      }, [h(button_index.Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "class": "".concat(upload_util.UPLOAD_NAME, "__dragger-progress-cancel"),
        "on": {
          "click": this.reupload
        }
      }, [this.global.triggerUploadText.reupload]), h(button_index.Button, {
        "attrs": {
          "theme": "primary",
          "variant": "text"
        },
        "on": {
          "click": this.remove
        }
      }, [this.global.triggerUploadText["delete"]])])])]);
    }
  },
  render: function render() {
    var h = arguments[0];
    var content = null;

    if ((this.loadingFile || this.file) && this.display !== "custom") {
      content = this.renderProgress();
    } else {
      content = h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__trigger"),
        "on": {
          "click": this.trigger
        }
      }, [this.$scopedSlots["default"] && this.$scopedSlots["default"](null) || this.renderDefaultDragElement()]);
    }

    return h("div", {
      "class": this.classes,
      "on": {
        "drop": this.handleDrop,
        "dragenter": this.handleDragenter,
        "dragover": this.handleDragover,
        "dragleave": this.handleDragleave
      }
    }, [content]);
  }
});

exports["default"] = Dragger;
//# sourceMappingURL=dragger.js.map
