'use strict';

import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { default as BSFormControl } from 'react-bootstrap/lib/FormControl';
import { FormControl } from './FormControl';
import { Button } from '../Button';
import { Icon } from '../Icon';
import { DangerousHTML } from '../DangerousHTML';
export var FileUploadControl =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(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.createElement(Icon, {
        name: uploadIcon,
        "aria-hidden": "true"
      });
    } else {
      return null;
    }
  };

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

    if (uploadText) {
      return React.createElement(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.createElement(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.findDOMNode(this.refs.realForm).reset();
  };

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

    return React.createElement("form", {
      ref: "realForm"
    }, React.createElement(BSFormControl, {
      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;
}(FormControl);

_defineProperty(FileUploadControl, "propTypes", Object.assign({}, FormControl.propTypes, {
  accept: PropTypes.string,
  multiple: PropTypes.bool,
  uploadIcon: PropTypes.string,
  uploadText: PropTypes.string,
  onCancelSelect: PropTypes.func
}));

_defineProperty(FileUploadControl, "defaultProps", Object.assign({}, FormControl.defaultProps, {
  multiple: false,
  accept: '',
  uploadIcon: 'upload',
  onCancelSelect: function onCancelSelect(e) {
    return e;
  }
}));

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