function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { forwardRef } from 'react';
import uniqid from 'uniqid';
import { Uploader } from '@tecsinapse/uploader';
export var CustomUploader = forwardRef(function (_ref, ref) {
  var files = _ref.files,
      setFiles = _ref.setFiles,
      mediaType = _ref.mediaType,
      focusRef = _ref.focusRef,
      maxFileUploadSize = _ref.maxFileUploadSize;

  var onAccept = function onAccept(newFiles) {
    if (focusRef) {
      focusRef.focus();
    }

    var copyFiles = _objectSpread({}, files);

    newFiles.forEach(function (file) {
      var reader = new FileReader();
      var uid = uniqid(); // convert all no image, no video and no application to application unknown mime type

      var mediaTypeFile = file.type.startsWith('image') || file.type.startsWith('video') || file.type.startsWith('application') ? file.type : 'application/octet-stream'; // Create preview tag

      reader.onload = function (event) {
        copyFiles[uid] = {
          file: file,
          mediaType: mediaTypeFile,
          data: event.target.result,
          name: file.name,
          size: Math.round(file.size / 1024)
        };
        setFiles(copyFiles);
      };

      reader.readAsDataURL(file);
    });
  }; // TODO: export as a chat props


  var maxFilesPerMessage = 3;
  var messages = {
    maximumFileLimitMessage: function maximumFileLimitMessage(limit) {
      return "Apenas ".concat(limit, " arquivos podem ser carregados por mensagem.");
    },
    maximumFileNumberMessage: 'Número máximo de arquivos',
    filenameFailedMessage: function filenameFailedMessage(name) {
      return "".concat(name, " falhou. ");
    },
    filetypeNotSupportedMessage: 'Arquivo não suportado. ',
    sizeLimitErrorMessage: function sizeLimitErrorMessage(size) {
      return "Arquivo deve ter tamanho menor que ".concat(size / 1024, " KB.");
    },
    undefinedErrorMessage: 'Erro interno'
  };
  return React.createElement(React.Fragment, null, React.createElement(Uploader, {
    value: files,
    onAccept: onAccept,
    filesLimit: maxFilesPerMessage,
    silent: true,
    messages: messages,
    ref: ref,
    acceptedFormat: mediaType ? [mediaType] : undefined,
    maxFileSize: maxFileUploadSize
  }));
});