'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 _FieldControl = require('../FieldControl');

var _FieldControl2 = _interopRequireDefault(_FieldControl);

var _Alert = require('../Alert');

var _Alert2 = _interopRequireDefault(_Alert);

var _ListFiles = require('./ListFiles');

var _ListFiles2 = _interopRequireDefault(_ListFiles);

var _tools = require('../_helpers/tools');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }

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 propTypes = {
  className: _propTypes2.default.string,
  children: _propTypes2.default.func,
  onDrop: _propTypes2.default.func,
  onDelete: _propTypes2.default.func,
  accept: _propTypes2.default.string,
  maxSize: _propTypes2.default.number,
  minSize: _propTypes2.default.number,
  input: _propTypes2.default.object,
  label: _propTypes2.default.string,
  mandatory: _propTypes2.default.bool,
  multiple: _propTypes2.default.bool,
  placeHolder: _propTypes2.default.string
};

var defaultProps = {
  className: null,
  children: undefined,
  onDrop: undefined,
  onDelete: undefined,
  accept: undefined,
  maxSize: Infinity,
  minSize: 0,
  input: null,
  label: null,
  mandatory: false,
  multiple: false,
  placeHolder: null
};

var FileInput = function (_Component) {
  _inherits(FileInput, _Component);

  function FileInput() {
    var _ref;

    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, (_ref = FileInput.__proto__ || Object.getPrototypeOf(FileInput)).call.apply(_ref, [this].concat(args))), _this), _this.handleDelete = function (file, row) {
      var onDelete = _this.props.onDelete;

      var _getInputProps = (0, _tools.getInputProps)(_this.props),
          _getInputProps$value = _toArray(_getInputProps.value),
          value = _getInputProps$value.slice(0),
          onChange = _getInputProps.onChange;

      value.splice(row, 1);
      if (onDelete) onDelete(value);
      if (onChange) onChange(value);
    }, _this.handleDrop = function (filesToUpload) {
      var _this$props = _this.props,
          onDrop = _this$props.onDrop,
          multiple = _this$props.multiple;

      var _getInputProps2 = (0, _tools.getInputProps)(_this.props),
          onChange = _getInputProps2.onChange,
          _getInputProps2$value = _getInputProps2.value,
          value = _getInputProps2$value === undefined ? [] : _getInputProps2$value;

      if (onChange && filesToUpload.length) {
        onChange(multiple ? [].concat(_toConsumableArray(filesToUpload), _toConsumableArray(value)) : filesToUpload);
      }
      if (onDrop && filesToUpload.length) {
        onDrop(filesToUpload);
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(FileInput, [{
    key: 'renderWithoutChild',
    value: function renderWithoutChild() {
      var _getInputProps3 = (0, _tools.getInputProps)(this.props),
          value = _getInputProps3.value;

      return value && !!value.length && _react2.default.createElement(_ListFiles2.default, { key: 'list-files', value: value, handleDelete: this.handleDelete });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          children = _props.children,
          multiple = _props.multiple,
          maxSize = _props.maxSize,
          minSize = _props.minSize,
          accept = _props.accept,
          placeHolder = _props.placeHolder,
          onDrop = _props.onDrop,
          props = _objectWithoutProperties(_props, ['children', 'multiple', 'maxSize', 'minSize', 'accept', 'placeHolder', 'onDrop']);

      return _react2.default.createElement(
        _FieldControl2.default,
        props,
        function (_ref2) {
          var restProps = _objectWithoutProperties(_ref2, []);

          var _getInputProps4 = (0, _tools.getInputProps)(restProps),
              name = _getInputProps4.name;

          return [_react2.default.createElement(
            _reactDropzone2.default,
            {
              name: name,
              onDrop: _this2.handleDrop,
              className: 'flive_file-input_drop-zone',
              multiple: multiple,
              maxSize: maxSize,
              minSize: minSize,
              accept: accept,
              key: 'drop-zone'
            },
            function (_ref3) {
              var isDragActive = _ref3.isDragActive,
                  isDragReject = _ref3.isDragReject;

              var dropWording = void 0;
              switch (true) {
                case isDragReject:
                  dropWording = _react2.default.createElement(
                    _Alert2.default,
                    { type: 'warning' },
                    'This file is not authorized'
                  );
                  break;
                case isDragActive:
                  dropWording = _react2.default.createElement(
                    _Alert2.default,
                    { type: 'info' },
                    'This file is authorized'
                  );
                  break;
                default:
                  dropWording = _react2.default.createElement(
                    'p',
                    { className: 'flive_file-input_placeholder' },
                    ' ',
                    placeHolder || 'You can drop your file(s) here'
                  );
              }

              return _react2.default.createElement(
                'div',
                { key: 'files-zone' },
                dropWording
              );
            }
          ), children ? _react2.default.createElement(
            'div',
            { key: 'children' },
            children(_extends({}, (0, _tools.getInputProps)(restProps), { handleDelete: _this2.handleDelete }))
          ) : _this2.renderWithoutChild()];
        }
      );
    }
  }]);

  return FileInput;
}(_react.Component);

FileInput.propTypes = propTypes;
FileInput.defaultProps = defaultProps;

FileInput.ListFiles = _ListFiles2.default;

exports.default = FileInput;
module.exports = exports['default'];