"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = FileOrURLInput;

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

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

var _reactModal = _interopRequireDefault(require("react-modal"));

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

// TODO: Where to do this?
//   Modal.setAppElement(document.getElementsByClassName('App')[0]);
// Then remove this:
//   ariaHideApp={false}
function FileOrURLInput(props) {
  var accept = props.accept,
      onChange = props.onChange,
      disabled = props.disabled,
      modalContentClassName = props.modalContentClassName,
      modalOverlayClassName = props.modalOverlayClassName;

  var _React$useState = _react.default.useState("Click to choose..."),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      text = _React$useState2[0],
      setText = _React$useState2[1];

  var _React$useState3 = _react.default.useState(""),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      lastUrl = _React$useState4[0],
      setLastURL = _React$useState4[1];

  var _React$useState5 = _react.default.useState(false),
      _React$useState6 = _slicedToArray(_React$useState5, 2),
      modalIsOpen = _React$useState6[0],
      setModalIsOpen = _React$useState6[1];

  var onMainClick = function onMainClick() {
    setModalIsOpen(true);
  };

  var onInputChange = function onInputChange(event) {
    if (event.target.files) {
      if (event.target.files.length > 0) {
        var file = event.target.files[0];
        setText(file.name);
        setLastURL("");
      }
    } else {
      var url = event.target.value;
      setText(url);
      setLastURL(url);
    }

    setModalIsOpen(false);

    if (onChange) {
      onChange(event);
    }
  };

  var onKeyDown = function onKeyDown(event) {
    if (event.key === 'Enter') {
      onInputChange(event); // Prevent this key press from triggering the main button, and immediately opening
      // the modal again.

      event.preventDefault();
    }
  };

  var onCancelClick = function onCancelClick() {
    setModalIsOpen(false);
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: "FileOrURLInput"
  }, /*#__PURE__*/_react.default.createElement("button", {
    type: "button",
    onClick: onMainClick,
    disabled: disabled,
    className: "FileOrURLInputButton"
  }, text), /*#__PURE__*/_react.default.createElement(_reactModal.default, {
    className: modalContentClassName,
    overlayClassName: modalOverlayClassName,
    isOpen: modalIsOpen,
    ariaHideApp: false
  }, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("input", {
    type: "file",
    accept: accept,
    onChange: onInputChange
  }), /*#__PURE__*/_react.default.createElement("div", null, "Or URL\xA0", /*#__PURE__*/_react.default.createElement("input", {
    type: "url",
    onKeyDown: onKeyDown,
    defaultValue: lastUrl
  }))), "\xA0", /*#__PURE__*/_react.default.createElement("button", {
    type: "button",
    onClick: onCancelClick
  }, "Cancel")));
}

FileOrURLInput.propTypes = {
  accept: _propTypes.default.string,
  onChange: _propTypes.default.func,
  disabled: _propTypes.default.bool,
  modalContentClassName: _propTypes.default.string,
  modalOverlayClassName: _propTypes.default.string
};
FileOrURLInput.defaultProps = {
  accept: "*",
  onChange: null,
  disabled: false,
  modalContentClassName: 'ModalContent',
  modalOverlayClassName: 'ModalOverlay'
};