/**
 * tdesign v0.46.4
 * (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 loading_index = require('../loading/index.js');
var _common_js_upload_utils = require('../_common/js/upload/utils.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('../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/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('vue');
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');
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/camelCase');
require('lodash/kebabCase');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('../config.js');

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

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

var SingleFile = utils_mixins["default"](configProvider_configReceiver["default"]("upload"), configProvider_configReceiver.getGlobalIconMixins()).extend({
  name: "TUploadSingleFile",
  components: {
    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);
      }
    },
    fileListDisplay: {
      type: Function
    },
    locale: upload_props["default"].locale
  },
  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(this.classPrefix, "-input__inner"), _defineProperty__default["default"]({}, "".concat(this.componentName, "__placeholder"), !this.inputName)];
    },
    classes: function classes() {
      return ["".concat(this.componentName, "__single"), "".concat(this.componentName, "__single-").concat(this.display)];
    }
  },
  methods: {
    renderProgress: function renderProgress() {
      var h = this.$createElement;

      if (this.loadingFile.status === "fail") {
        var _this$useGlobalIcon = this.useGlobalIcon({
          ErrorCircleFilledIcon: tdesignIconsVue.ErrorCircleFilledIcon
        }),
            ErrorCircleFilledIcon = _this$useGlobalIcon.ErrorCircleFilledIcon;

        return h(ErrorCircleFilledIcon);
      }

      if (this.showUploadProgress) {
        return h("div", {
          "class": "".concat(this.componentName, "__single-progress")
        }, [h(loading_index.Loading), h("span", {
          "class": "".concat(this.componentName, "__single-percent")
        }, [Math.min(this.loadingFile.percent, 99), "%"])]);
      }
    },
    renderResult: function renderResult() {
      var h = this.$createElement;

      if (!!this.loadingFile && this.loadingFile.status === "fail") {
        var _this$useGlobalIcon2 = this.useGlobalIcon({
          ErrorCircleFilledIcon: tdesignIconsVue.ErrorCircleFilledIcon
        }),
            ErrorCircleFilledIcon = _this$useGlobalIcon2.ErrorCircleFilledIcon;

        return h(ErrorCircleFilledIcon);
      }

      if (this.file && this.file.name && !this.loadingFile) {
        var _this$useGlobalIcon3 = this.useGlobalIcon({
          CheckCircleFilledIcon: tdesignIconsVue.CheckCircleFilledIcon
        }),
            CheckCircleFilledIcon = _this$useGlobalIcon3.CheckCircleFilledIcon;

        return h(CheckCircleFilledIcon);
      }

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

      var h = this.$createElement;
      if (!this.inputName) return;

      var _this$useGlobalIcon4 = this.useGlobalIcon({
        CloseCircleFilledIcon: tdesignIconsVue.CloseCircleFilledIcon
      }),
          CloseCircleFilledIcon = _this$useGlobalIcon4.CloseCircleFilledIcon;

      return h("div", {
        "class": ["".concat(this.componentName, "__single-display-text"), "".concat(this.componentName, "__display-text--margin")]
      }, [this.fileListDisplay() || h("span", {
        "class": "".concat(this.componentName, "__single-name")
      }, [this.inputName]), this.showProgress ? this.renderProgress() : h(CloseCircleFilledIcon, {
        "class": "".concat(this.componentName, "__icon-delete"),
        "nativeOn": {
          "click": function click(e) {
            return _this.remove({
              e: e,
              file: _this.file
            });
          }
        }
      })]);
    },
    renderFilePreviewAsInput: function renderFilePreviewAsInput() {
      var h = this.$createElement;
      return h("div", {
        "class": "".concat(this.componentName, "__single-input-preview ").concat(this.classPrefix, "-input")
      }, [h("div", {
        "class": this.inputTextClass
      }, [h("span", {
        "class": "".concat(this.componentName, "__single-input-text")
      }, [_common_js_upload_utils.abridgeName(this.inputText, 4, 6)]), this.showProgress && this.renderProgress(), this.renderResult()])]);
    }
  },
  render: function render() {
    var _this2 = this,
        _this$locale,
        _this$locale$triggerU;

    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(this.componentName, "__single-input-delete"),
      "on": {
        "click": function click(e) {
          return _this2.remove({
            e: e,
            file: _this2.file
          });
        }
      }
    }, [((_this$locale = this.locale) === null || _this$locale === void 0 ? void 0 : (_this$locale$triggerU = _this$locale.triggerUploadText) === null || _this$locale$triggerU === void 0 ? void 0 : _this$locale$triggerU["delete"]) || this.t(this.global.triggerUploadText["delete"])])]);
  }
});

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