/**
 * 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 Vue = require('vue');
var tdesignIconsVue = require('tdesign-icons-vue');
var loading_index = require('../loading/index.js');
var config = require('../config.js');
var upload_util = require('./util.js');
var upload_props = require('./props.js');
require('../loading/loading.js');
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');

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

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

var SingleFile = Vue__default["default"].extend({
  name: "TUploadSingleFile",
  components: {
    CloseCircleFilledIcon: tdesignIconsVue.CloseCircleFilledIcon,
    ErrorCircleFilledIcon: tdesignIconsVue.ErrorCircleFilledIcon,
    CheckCircleFilledIcon: tdesignIconsVue.CheckCircleFilledIcon,
    Loading: loading_index.Loading
  },
  data: function data() {
    return {};
  },
  props: {
    showUploadProgress: upload_props["default"].showUploadProgress,
    file: Object,
    loadingFile: Object,
    remove: Function,
    placeholder: String,
    display: {
      type: String,
      validator: function validator(val) {
        return ["file", "file-input"].includes(val);
      }
    }
  },
  computed: {
    percent: function percent() {
      return this.loadingFile && this.loadingFile.percent;
    },
    showPreview: function showPreview() {
      return Boolean(this.file && this.file.name);
    },
    showTextPreview: function showTextPreview() {
      return this.display === "file";
    },
    showInput: function showInput() {
      return this.display === "file-input";
    },
    showProgress: function showProgress() {
      return !!(this.loadingFile && this.loadingFile.status === "progress");
    },
    showDelete: function showDelete() {
      return this.file && this.file.name && !this.loadingFile;
    },
    inputName: function inputName() {
      var fileName = this.file && this.file.name;
      var loadingName = this.loadingFile && this.loadingFile.name;
      return this.showProgress ? loadingName : fileName;
    },
    inputText: function inputText() {
      return this.inputName || this.placeholder;
    },
    inputTextClass: function inputTextClass() {
      return ["".concat(config.prefix, "-input__inner"), _defineProperty__default["default"]({}, "".concat(upload_util.UPLOAD_NAME, "__placeholder"), !this.inputName)];
    },
    classes: function classes() {
      return ["".concat(upload_util.UPLOAD_NAME, "__single"), "".concat(upload_util.UPLOAD_NAME, "__single-").concat(this.display)];
    }
  },
  methods: {
    renderProgress: function renderProgress() {
      var h = this.$createElement;

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

      if (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), "%"])]);
      }
    },
    renderResult: function renderResult() {
      var h = this.$createElement;

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

      if (this.file && this.file.name && !this.loadingFile) {
        return h(tdesignIconsVue.CheckCircleFilledIcon);
      }

      return "";
    },
    renderFilePreviewAsText: function renderFilePreviewAsText() {
      var _this = this;

      var h = this.$createElement;
      if (!this.inputName) return;
      return h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__single-display-text t-upload__display-text--margin")
      }, [h("span", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__single-name")
      }, [this.inputName]), this.showProgress ? this.renderProgress() : h(tdesignIconsVue.CloseCircleFilledIcon, {
        "class": "".concat(upload_util.UPLOAD_NAME, "__icon-delete"),
        "nativeOn": {
          "click": function click(e) {
            return _this.remove(e);
          }
        }
      })]);
    },
    renderFilePreviewAsInput: function renderFilePreviewAsInput() {
      var h = this.$createElement;
      return h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__single-input-preview ").concat(config.prefix, "-input")
      }, [h("div", {
        "class": this.inputTextClass
      }, [h("span", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__single-input-text")
      }, [upload_util.abridgeName(this.inputText, 4, 6)]), this.showProgress && this.renderProgress(), this.renderResult()])]);
    }
  },
  render: function render() {
    var _this2 = this;

    var h = arguments[0];
    return h("div", {
      "class": this.classes
    }, [this.showInput && this.renderFilePreviewAsInput(), this.$scopedSlots["default"] && this.$scopedSlots["default"](null), this.showTextPreview && this.renderFilePreviewAsText(), this.showInput && this.showDelete && h("span", {
      "class": "".concat(upload_util.UPLOAD_NAME, "__single-input-delete"),
      "on": {
        "click": function click(e) {
          return _this2.remove(e);
        }
      }
    }, ["\u5220\u9664"])]);
  }
});

exports["default"] = SingleFile;
//# sourceMappingURL=single-file.js.map
