"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _zhCn = _interopRequireDefault(require("../locale/zh-cn.js"));

var _util = require("../util");

var _base = _interopRequireDefault(require("./base"));

var _list = _interopRequireDefault(require("./list"));

var _upload = _interopRequireDefault(require("./upload"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * Upload.Card
 * @description 继承 Upload 的 API，除非特别说明
 */
var Card =
/*#__PURE__*/
function (_Base) {
  _inherits(Card, _Base);

  function Card(props) {
    var _this;

    _classCallCheck(this, Card);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Card).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onProgress", function (value) {
      _this.setState({
        value: value
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onChange", function (value, file) {
      if (!('value' in _this.props)) {
        _this.setState({
          value: value
        });
      }

      _this.props.onChange(value, file);
    });

    var _value;
    /* istanbul ignore else */


    if ('value' in props) {
      _value = props.value;
    } else {
      _value = props.defaultValue;
    }

    _this.state = {
      value: typeof _value === 'undefined' ?
      /* istanbul ignore next */
      [] : _value,
      uploaderRef: _this.uploaderRef
    };
    return _this;
  }
  /* eslint react/no-did-mount-set-state: [0] */


  _createClass(Card, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setState({
        uploaderRef: this.uploaderRef
      });
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      /* istanbul ignore if */
      if ('value' in nextProps) {
        this.setState({
          value: typeof nextProps.value === 'undefined' ? [] : nextProps.value
        });
      }
    }
  }, {
    key: "isUploading",
    value: function isUploading() {
      return this.state.uploaderRef.isUploading();
    }
  }, {
    key: "saveRef",
    value: function saveRef(ref) {
      this.saveUploaderRef(ref);
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames,
          _this2 = this;

      var _this$props = this.props,
          action = _this$props.action,
          disabled = _this$props.disabled,
          prefix = _this$props.prefix,
          locale = _this$props.locale,
          className = _this$props.className,
          style = _this$props.style,
          limit = _this$props.limit,
          onPreview = _this$props.onPreview,
          onRemove = _this$props.onRemove,
          onCancel = _this$props.onCancel,
          timeout = _this$props.timeout;
      var isExceedLimit = this.state.value.length >= limit;
      var uploadButtonCls = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "upload-list-item"), true), _defineProperty(_classNames, "".concat(prefix, "hidden"), isExceedLimit), _classNames));
      var children = this.props.children || locale.card.addPhoto;
      var onRemoveFunc = disabled ? _util.func.prevent : onRemove;

      var othersForList = _util.obj.pickOthers(Card.propTypes, this.props);

      var othersForUpload = _util.obj.pickOthers(_list["default"].propTypes, othersForList);

      return _react["default"].createElement(_list["default"], _extends({
        className: className,
        style: style,
        listType: "card",
        closable: true,
        locale: locale,
        value: this.state.value,
        onRemove: onRemoveFunc,
        onCancel: onCancel,
        onPreview: onPreview,
        uploader: this.state.uploaderRef
      }, othersForList), _react["default"].createElement(_upload["default"], _extends({}, othersForUpload, {
        shape: "card",
        disabled: disabled,
        action: action,
        timeout: timeout,
        value: this.state.value,
        onProgress: this.onProgress,
        onChange: this.onChange,
        ref: function ref(_ref) {
          return _this2.saveRef(_ref);
        },
        className: uploadButtonCls
      }), children));
    }
  }]);

  return Card;
}(_base["default"]);

_defineProperty(Card, "displayName", 'Card');

_defineProperty(Card, "propTypes", {
  prefix: _propTypes["default"].string,
  locale: _propTypes["default"].object,
  children: _propTypes["default"].object,
  value: _propTypes["default"].oneOfType([_propTypes["default"].array, _propTypes["default"].object]),
  defaultValue: _propTypes["default"].oneOfType([_propTypes["default"].array, _propTypes["default"].object]),

  /**
   * 点击图片回调
   */
  onPreview: _propTypes["default"].func,

  /**
   * 改变时候的回调
   */
  onChange: _propTypes["default"].func,

  /**
   * 点击移除的回调
   */
  onRemove: _propTypes["default"].func,

  /**
   * 取消上传的回调
   */
  onCancel: _propTypes["default"].func
});

_defineProperty(Card, "defaultProps", {
  prefix: 'next-',
  locale: _zhCn["default"].Upload,
  onChange: _util.func.noop,
  onPreview: _util.func.noop
});

var _default = Card;
exports["default"] = _default;