text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports["default"] = void 0;
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _propTypes = _interopRequireDefault(require("prop-types"));
  
  var _utils = require("../../utils");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
  
  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 _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; }
  
  function addNameToDataURL(dataURL, name) {
    return dataURL.replace(";base64", ";name=".concat(encodeURIComponent(name), ";base64"));
  }
  
  function processFile(file) {
    var name = file.name,
        size = file.size,
        type = file.type;
    return new Promise(function (resolve, reject) {
      var reader = new window.FileReader();
      reader.onerror = reject;
  
      reader.onload = function (event) {
        resolve({
          dataURL: addNameToDataURL(event.target.result, name),
          name: name,
          size: size,
          type: type
        });
      };
  
      reader.readAsDataURL(file);
    });
  }
  
  function processFiles(files) {
    return Promise.all([].map.call(files, processFile));
  }
  
  function FilesInfo(props) {
    var filesInfo = props.filesInfo;
  
    if (filesInfo.length === 0) {
      return null;
    }
  
    return _react["default"].createElement("ul", {
      className: "file-info"
    }, filesInfo.map(function (fileInfo, key) {
      var name = fileInfo.name,
          size = fileInfo.size,
          type = fileInfo.type;
      return _react["default"].createElement("li", {
        key: key
      }, _react["default"].createElement("strong", null, name), " (", type, ", ", size, " bytes)");
    }));
  }
  
  function extractFileInfo(dataURLs) {
    return dataURLs.filter(function (dataURL) {
      return typeof dataURL !== "undefined";
    }).map(function (dataURL) {
      var _dataURItoBlob = (0, _utils.dataURItoBlob)(dataURL),
          blob = _dataURItoBlob.blob,
          name = _dataURItoBlob.name;
  
      return {
        name: name,
        size: blob.size,
        type: blob.type
      };
    });
  }
  
  var FileWidget =
  /*#__PURE__*/
  function (_Component) {
    _inherits(FileWidget, _Component);
  
    function FileWidget(props) {
      var _this;
  
      _classCallCheck(this, FileWidget);
  
      _this = _possibleConstructorReturn(this, _getPrototypeOf(FileWidget).call(this, props));
  
      _defineProperty(_assertThisInitialized(_this), "onChange", function (event) {
        var _this$props = _this.props,
            multiple = _this$props.multiple,
            onChange = _this$props.onChange;
        processFiles(event.target.files).then(function (filesInfo) {
          var state = {
            values: filesInfo.map(function (fileInfo) {
              return fileInfo.dataURL;
            }),
            filesInfo: filesInfo
          };
  
          _this.setState(state, function () {
            if (multiple) {
              onChange(state.values);
            } else {
              onChange(state.values[0]);
            }
          });
        });
      });
  
      var value = props.value;
      var values = Array.isArray(value) ? value : [value];
      _this.state = {
        values: values,
        filesInfo: extractFileInfo(values)
      };
      return _this;
    }
  
    _createClass(FileWidget, [{
      key: "shouldComponentUpdate",
      value: function shouldComponentUpdate(nextProps, nextState) {
        return (0, _utils.shouldRender)(this, nextProps, nextState);
      }
    }, {
      key: "render",
      value: function render() {
        var _this2 = this;
  
        var _this$props2 = this.props,
            multiple = _this$props2.multiple,
            id = _this$props2.id,
            readonly = _this$props2.readonly,
            disabled = _this$props2.disabled,
            autofocus = _this$props2.autofocus,
            options = _this$props2.options;
        var filesInfo = this.state.filesInfo;
        return _react["default"].createElement("div", null, _react["default"].createElement("p", null, _react["default"].createElement("input", {
          ref: function ref(_ref) {
            return _this2.inputRef = _ref;
          },
          id: id,
          type: "file",
          disabled: readonly || disabled,
          onChange: this.onChange,
          defaultValue: "",
          autoFocus: autofocus,
          multiple: multiple,
          accept: options.accept
        })), _react["default"].createElement(FilesInfo, {
          filesInfo: filesInfo
        }));
      }
    }]);
  
    return FileWidget;
  }(_react.Component);
  
  FileWidget.defaultProps = {
    autofocus: false
  };
  
  if (process.env.NODE_ENV !== "production") {
    FileWidget.propTypes = {
      multiple: _propTypes["default"].bool,
      value: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].arrayOf(_propTypes["default"].string)]),
      autofocus: _propTypes["default"].bool
    };
  }
  
  var _default = FileWidget;
  exports["default"] = _default;