'use strict';

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

var _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; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactDropzone = require('react-dropzone');

var _reactDropzone2 = _interopRequireDefault(_reactDropzone);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

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

var _Icon2 = _interopRequireDefault(_Icon);

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

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var DropArea = _styledComponents2.default.div.withConfig({
  displayName: 'FileInput__DropArea'
})(['border: ', ';padding: ', ';font-size: ', ';font-family: ', ';line-height: ', ';background: ', ';color: ', ';width: 100%;display: flex;flex-direction: row;cursor: pointer;'], function (_ref) {
  var theme = _ref.theme;
  return theme.input.border;
}, function (_ref2) {
  var theme = _ref2.theme;
  return theme.input.padding;
}, function (_ref3) {
  var theme = _ref3.theme;
  return theme.input.fontSize;
}, function (_ref4) {
  var theme = _ref4.theme;
  return theme.input.fontFamily;
}, function (_ref5) {
  var theme = _ref5.theme;
  return theme.input.lineHeight;
}, function (_ref6) {
  var theme = _ref6.theme;
  return theme.input.bg;
}, function (_ref7) {
  var theme = _ref7.theme;
  return theme.input.fg;
});

var Preview = _styledComponents2.default.div.withConfig({
  displayName: 'FileInput__Preview'
})(['margin: auto auto auto 16px;']);

var FileInput = function (_React$Component) {
  _inherits(FileInput, _React$Component);

  function FileInput() {
    var _ref8;

    var _temp, _this, _ret;

    _classCallCheck(this, FileInput);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref8 = FileInput.__proto__ || Object.getPrototypeOf(FileInput)).call.apply(_ref8, [this].concat(args))), _this), _this.renderFiles = function () {
      var value = _this.props.value;

      if (value && value[0]) {
        return _react2.default.createElement(
          DropArea,
          null,
          _react2.default.createElement(_Icon2.default, { name: 'fileFilled' }),
          _react2.default.createElement(
            Preview,
            { active: true },
            value[0].name
          )
        );
      } else {
        return _react2.default.createElement(
          DropArea,
          null,
          _react2.default.createElement(_Icon2.default, { name: 'file' }),
          _react2.default.createElement(
            Preview,
            null,
            'Drop a file here, or click to browse.'
          )
        );
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(FileInput, [{
    key: 'render',
    value: function render() {
      var onChange = this.props.onChange;


      return _react2.default.createElement(
        _reactDropzone2.default,
        _extends({}, this.props, {
          multiple: false,
          onDrop: function onDrop(files) {
            return onChange(files);
          },
          style: { width: '100%' },
          activeStyle: { color: '#00bcec' },
          rejectStyle: {}
        }),
        this.renderFiles()
      );
    }
  }]);

  return FileInput;
}(_react2.default.Component);

FileInput.propTypes = {
  value: _propTypes2.default.instanceOf(FileList).isRequired,
  onChange: _propTypes2.default.func.isRequired,

  required: _propTypes2.default.bool,
  disabled: _propTypes2.default.bool
};
FileInput.defaultProps = {
  required: false,
  disabled: false
};


FileInput.usage = '\n# FileInput\n\nA file input component which accepts drag-and-drop.\n';

exports.default = FileInput;