'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.FileUploadControl = void 0;

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _reactDom = _interopRequireDefault(require("react-dom"));

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

var _FormControl2 = _interopRequireDefault(require("react-bootstrap/lib/FormControl"));

var _FormControl3 = require("./FormControl");

var _Button = require("../Button");

var _Icon = require("../Icon");

var _DangerousHTML = require("../DangerousHTML");

var FileUploadControl =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(FileUploadControl, _FormControl);

  function FileUploadControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = FileUploadControl.prototype;

  _proto.getValue = function getValue() {
    var node = this.getInputDOMNode();
    return node && node.files;
  };

  _proto.outputConverter = function outputConverter(e) {
    return e.target ? e.target.files : e;
  };

  _proto.getUploadIcon = function getUploadIcon() {
    var uploadIcon = this.props.uploadIcon;

    if (uploadIcon) {
      return _react.default.createElement(_Icon.Icon, {
        name: uploadIcon,
        "aria-hidden": "true"
      });
    } else {
      return null;
    }
  };

  _proto.getUploadText = function getUploadText() {
    var uploadText = this.props.uploadText;

    if (uploadText) {
      return _react.default.createElement(_DangerousHTML.DangerousHTML, null, uploadText);
    } else {
      return null;
    }
  };

  _proto.renderUploadButton = function renderUploadButton() {
    var _this = this;

    var _this$props = this.props,
        id = _this$props.id,
        bsStyle = _this$props.bsStyle;
    var uploadIcon = this.getUploadIcon();
    var uploadText = this.getUploadText();
    var withIcon = !!uploadIcon;
    var iconOnly = withIcon && !uploadText;
    var ariaDescId = this.props['aria-describedby'] ? this.props['aria-describedby'] : null;
    return _react.default.createElement(_Button.Button, {
      withIcon: withIcon,
      iconOnly: iconOnly,
      bsStyle: bsStyle,
      ref: "uploadButton",
      id: getUploadBtnId(id),
      testAutomationId: "uploadButton",
      "aria-describedby": ariaDescId,
      onClick: function onClick() {
        _this.reset();

        _this.props.onCancelSelect();

        _this.click();
      }
    }, uploadIcon, uploadText);
  } //file-Input would not trigger onChange, if select same files next time
  ;

  _proto.reset = function reset() {
    _reactDom.default.findDOMNode(this.refs.realForm).reset();
  };

  _proto.render = function render() {
    var _this$getPropsFromCon = this.getPropsFromContext(),
        value = _this$getPropsFromCon.value,
        id = _this$getPropsFromCon.id,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$getPropsFromCon, ["value", "id"]);

    return _react.default.createElement("form", {
      ref: "realForm"
    }, _react.default.createElement(_FormControl2.default, {
      onChange: props.onChange,
      "data-test-automation-id": "FileUploadControl",
      "aria-labelledby": getUploadBtnId(id),
      "aria-hidden": true //hidden for screen reader
      ,
      tabIndex: -1,
      className: "sr-only",
      type: "file",
      ref: "input",
      multiple: props.multiple,
      accept: this.props.accept
    }), this.renderUploadButton());
  };

  return FileUploadControl;
}(_FormControl3.FormControl);

exports.FileUploadControl = FileUploadControl;
(0, _defineProperty2.default)(FileUploadControl, "propTypes", Object.assign({}, _FormControl3.FormControl.propTypes, {
  accept: _propTypes.default.string,
  multiple: _propTypes.default.bool,
  uploadIcon: _propTypes.default.string,
  uploadText: _propTypes.default.string,
  onCancelSelect: _propTypes.default.func
}));
(0, _defineProperty2.default)(FileUploadControl, "defaultProps", Object.assign({}, _FormControl3.FormControl.defaultProps, {
  multiple: false,
  accept: '',
  uploadIcon: 'upload',
  onCancelSelect: function onCancelSelect(e) {
    return e;
  }
}));

function getUploadBtnId(id) {
  return (id || '') + "UploadButton";
}