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

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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; }

import React, { useState } from 'react';
import { FixedWrapper } from '@livechat/ui-kit';
import PropTypes from 'prop-types';
import { useTheme } from '@material-ui/styles';
import Maximized from './Maximized';
import Minimized from './Minimized';
import ChatTheme from './ChatTheme';
import { ChatLocations } from './ChatLocations';
export var Chat = function Chat(_ref) {
  var messages = _ref.messages,
      onMessageSend = _ref.onMessageSend,
      messagesEndRef = _ref.messagesEndRef,
      disabled = _ref.disabled,
      isMaximizedOnly = _ref.isMaximizedOnly,
      onAudio = _ref.onAudio,
      title = _ref.title,
      subtitle = _ref.subtitle,
      onCloseChat = _ref.onCloseChat,
      error = _ref.error,
      onMediaSend = _ref.onMediaSend,
      notifyNumber = _ref.notifyNumber,
      isLoading = _ref.isLoading,
      loadMore = _ref.loadMore,
      maxFileUploadSize = _ref.maxFileUploadSize,
      onMessageResend = _ref.onMessageResend,
      isBlocked = _ref.isBlocked,
      blockedMessage = _ref.blockedMessage,
      roundedCorners = _ref.roundedCorners,
      chatList = _ref.chatList,
      onBackToChatList = _ref.onBackToChatList,
      onSelectedChat = _ref.onSelectedChat,
      notificationNumber = _ref.notificationNumber,
      containerHeight = _ref.containerHeight,
      chatOptions = _ref.chatOptions,
      customHeader = _ref.customHeader,
      warningMessage = _ref.warningMessage;
  var theme = useTheme();

  var _useState = useState(chatList ? ChatLocations.CHAT_LIST : ChatLocations.MESSAGES),
      _useState2 = _slicedToArray(_useState, 2),
      location = _useState2[0],
      setLocation = _useState2[1];

  var headerBackground = customHeader.headerBackground,
      headerText = customHeader.headerText,
      headerLabel = customHeader.headerLabel;
  return React.createElement("div", null, React.createElement(ChatTheme, {
    materialTheme: theme,
    roundedCorners: roundedCorners,
    containerHeight: containerHeight,
    headerBackground: headerBackground
  }, React.createElement("div", null, React.createElement(FixedWrapper.Root, {
    maximizedOnInit: isMaximizedOnly
  }, React.createElement(FixedWrapper.Maximized, null, React.createElement(Maximized, {
    messages: messages,
    onMessageSend: onMessageSend,
    messagesEndRef: messagesEndRef,
    onAudio: onAudio,
    disabled: disabled,
    isMaximizedOnly: isMaximizedOnly,
    hasCloseButton: !isMaximizedOnly,
    title: title,
    subtitle: subtitle,
    onCloseChat: onCloseChat,
    error: error,
    onMediaSend: onMediaSend,
    isLoading: isLoading,
    loadMore: loadMore,
    maxFileUploadSize: maxFileUploadSize,
    onMessageResend: onMessageResend,
    isBlocked: isBlocked,
    blockedMessage: blockedMessage,
    chatList: chatList,
    onBackToChatList: onBackToChatList,
    location: location,
    setLocation: setLocation,
    onSelectedChat: onSelectedChat,
    notificationNumber: notificationNumber,
    chatOptions: chatOptions,
    headerLabel: headerLabel,
    headerText: headerText,
    warningMessage: warningMessage
  })), React.createElement(FixedWrapper.Minimized, null, !isMaximizedOnly && React.createElement(Minimized, {
    notifyNumber: notifyNumber
  }))))));
};
Chat.defaultProps = {
  onAudio: undefined,
  disabled: false,
  isMaximizedOnly: false,
  title: '',
  subtitle: '',
  onCloseChat: undefined,
  error: undefined,
  onMediaSend: undefined,
  notifyNumber: 0,
  isLoading: false,
  loadMore: undefined,
  maxFileUploadSize: 20971520,
  // 20 MB
  onMessageResend: undefined,
  isBlocked: undefined,
  blockedMessage: 'The chat is blocked',
  chatList: undefined,
  onBackToChatList: undefined,
  onSelectedChat: undefined,
  notificationNumber: 0,
  chatOptions: {
    show: false,
    color: '#fff',
    handleFunc: function handleFunc() {}
  },
  customHeader: {
    headerLabel: undefined,
    headerBackground: undefined,
    headerText: undefined
  },
  warningMessage: undefined,
  roundedCorners: true,
  containerHeight: '500px'
};
Chat.propTypes = {
  /** Messages array */
  messages: PropTypes.arrayOf(PropTypes.shape({
    localId: PropTypes.string,
    at: PropTypes.string,
    own: PropTypes.bool,
    id: PropTypes.string,
    text: PropTypes.string,
    title: PropTypes.string,
    authorName: PropTypes.string,
    medias: PropTypes.arrayOf(PropTypes.shape({
      url: PropTypes.string,
      mediaType: PropTypes.oneOf(['image', 'video', 'audio', 'application']),
      name: PropTypes.string,
      size: PropTypes.number,
      data: PropTypes.object
    })),
    status: PropTypes.oneOf(['sending', 'error', 'delivered'])
  })).isRequired,

  /** Message sender */
  onMessageSend: PropTypes.func.isRequired,

  /** Message resend */
  onMessageResend: PropTypes.func,

  /** onAudio is not required, when it is not informed the chat doesn't support audio */
  onAudio: PropTypes.func,

  /** Event handler closing the chat */
  onCloseChat: PropTypes.func,

  /** onMedia is not required, when it is not informed the chat doesn't support media */
  onMediaSend: PropTypes.func,

  /** Disable the chat */
  disabled: PropTypes.bool,

  /** Chat stays maximized */
  isMaximizedOnly: PropTypes.bool,

  /** Chat title */
  title: PropTypes.string,

  /** Chat subtitle */
  subtitle: PropTypes.string,

  /** Display error message */
  error: PropTypes.string,

  /** Number of notifications */
  notifyNumber: PropTypes.number,

  /** Loading state */
  isLoading: PropTypes.bool,

  /** Loader when more content is requested */
  loadMore: PropTypes.func,

  /** Max file size upload */
  maxFileUploadSize: PropTypes.number,

  /** Chat blocked state */
  isBlocked: PropTypes.bool,

  /** Message displayed when chat is blocked */
  blockedMessage: PropTypes.string,

  /** Callback called after the ChatList view is rendered */
  onBackToChatList: PropTypes.func,

  /** ChatList with recipients */
  chatList: PropTypes.arrayOf(PropTypes.shape({
    name: PropTypes.string,
    phone: PropTypes.string,
    lastMessage: PropTypes.string,
    unread: PropTypes.number,
    type: PropTypes.oneOf(['WHATSAPP', 'TELEGRAM', 'SKYPE' // TODO: Add other channels
    ]),
    chatId: PropTypes.string
  })),

  /** Action performed when chat is selected */
  onSelectedChat: PropTypes.func,

  /** Number of notification */
  notificationNumber: PropTypes.number,

  /** Disable default rounded corners of divs */
  roundedCorners: PropTypes.bool,

  /** Message Chat Container height */
  containerHeight: PropTypes.string,

  /** Show vertical dots on chat window for options */
  chatOptions: PropTypes.shape({
    show: PropTypes.bool,
    color: PropTypes.string,
    handleFunc: PropTypes.func
  }),

  /** Change header label (text), background color and text color */
  customHeader: PropTypes.shape({
    headerLabel: PropTypes.string,
    headerBackground: PropTypes.string,
    headerText: PropTypes.string
  }),

  /** Display a message in warning format */
  warningMessage: PropTypes.string
};
export default Chat;