'use strict';

var React = require('react');
var message = require('@sendbird/chat/message');
var utils = require('./bundle-CqFqMb6S.js');
require('./bundle-kPv1Olqr.js');
var consts = require('./bundle-yU9Mr2pS.js');

// For legacy
// These are not used for collections(GroupChannel)
var PREV_RESULT_SIZE = 30;
var NEXT_RESULT_SIZE = 15;

var RESET_MESSAGES = 'RESET_MESSAGES';
var FETCH_INITIAL_MESSAGES_START = 'FETCH_INITIAL_MESSAGES_START';
var FETCH_INITIAL_MESSAGES_SUCCESS = 'FETCH_INITIAL_MESSAGES_SUCCESS';
var FETCH_INITIAL_MESSAGES_FAILURE = 'FETCH_INITIAL_MESSAGES_FAILURE';
var FETCH_PREV_MESSAGES_SUCCESS = 'FETCH_PREV_MESSAGES_SUCCESS';
var FETCH_PREV_MESSAGES_FAILURE = 'FETCH_PREV_MESSAGES_FAILURE';
var FETCH_NEXT_MESSAGES_SUCCESS = 'FETCH_NEXT_MESSAGES_SUCCESS';
var FETCH_NEXT_MESSAGES_FAILURE = 'FETCH_NEXT_MESSAGES_FAILURE';
var SEND_MESSAGE_START = 'SEND_MESSAGE_START';
var SEND_MESSAGE_SUCCESS = 'SEND_MESSAGE_SUCCESS';
var SEND_MESSAGE_FAILURE = 'SEND_MESSAGE_FAILURE';
var RESEND_MESSAGE_START = 'RESEND_MESSAGE_START';
var ON_MESSAGE_RECEIVED = 'ON_MESSAGE_RECEIVED';
var ON_MESSAGE_UPDATED = 'ON_MESSAGE_UPDATED';
var ON_MESSAGE_THREAD_INFO_UPDATED = 'ON_MESSAGE_THREAD_INFO_UPDATED';
var ON_MESSAGE_DELETED = 'ON_MESSAGE_DELETED';
var ON_MESSAGE_DELETED_BY_REQ_ID = 'ON_MESSAGE_DELETED_BY_REQ_ID';
var SET_CURRENT_CHANNEL = 'SET_CURRENT_CHANNEL';
var SET_CHANNEL_INVALID = 'SET_CHANNEL_INVALID';
var MARK_AS_READ = 'MARK_AS_READ';
var ON_REACTION_UPDATED = 'ON_REACTION_UPDATED';
var SET_EMOJI_CONTAINER = 'SET_EMOJI_CONTAINER';
var MESSAGE_LIST_PARAMS_CHANGED = 'MESSAGE_LIST_PARAMS_CHANGED';
var ON_FILE_INFO_UPLOADED = 'ON_FILE_INFO_UPLOADED';
var ON_TYPING_STATUS_UPDATED = 'ON_TYPING_STATUS_UPDATED';

var channelActions = /*#__PURE__*/Object.freeze({
  __proto__: null,
  FETCH_INITIAL_MESSAGES_FAILURE: FETCH_INITIAL_MESSAGES_FAILURE,
  FETCH_INITIAL_MESSAGES_START: FETCH_INITIAL_MESSAGES_START,
  FETCH_INITIAL_MESSAGES_SUCCESS: FETCH_INITIAL_MESSAGES_SUCCESS,
  FETCH_NEXT_MESSAGES_FAILURE: FETCH_NEXT_MESSAGES_FAILURE,
  FETCH_NEXT_MESSAGES_SUCCESS: FETCH_NEXT_MESSAGES_SUCCESS,
  FETCH_PREV_MESSAGES_FAILURE: FETCH_PREV_MESSAGES_FAILURE,
  FETCH_PREV_MESSAGES_SUCCESS: FETCH_PREV_MESSAGES_SUCCESS,
  MARK_AS_READ: MARK_AS_READ,
  MESSAGE_LIST_PARAMS_CHANGED: MESSAGE_LIST_PARAMS_CHANGED,
  ON_FILE_INFO_UPLOADED: ON_FILE_INFO_UPLOADED,
  ON_MESSAGE_DELETED: ON_MESSAGE_DELETED,
  ON_MESSAGE_DELETED_BY_REQ_ID: ON_MESSAGE_DELETED_BY_REQ_ID,
  ON_MESSAGE_RECEIVED: ON_MESSAGE_RECEIVED,
  ON_MESSAGE_THREAD_INFO_UPDATED: ON_MESSAGE_THREAD_INFO_UPDATED,
  ON_MESSAGE_UPDATED: ON_MESSAGE_UPDATED,
  ON_REACTION_UPDATED: ON_REACTION_UPDATED,
  ON_TYPING_STATUS_UPDATED: ON_TYPING_STATUS_UPDATED,
  RESEND_MESSAGE_START: RESEND_MESSAGE_START,
  RESET_MESSAGES: RESET_MESSAGES,
  SEND_MESSAGE_FAILURE: SEND_MESSAGE_FAILURE,
  SEND_MESSAGE_START: SEND_MESSAGE_START,
  SEND_MESSAGE_SUCCESS: SEND_MESSAGE_SUCCESS,
  SET_CHANNEL_INVALID: SET_CHANNEL_INVALID,
  SET_CURRENT_CHANNEL: SET_CURRENT_CHANNEL,
  SET_EMOJI_CONTAINER: SET_EMOJI_CONTAINER
});

function useInitialMessagesFetch(_a, _b) {
    var currentGroupChannel = _a.currentGroupChannel, initialTimeStamp = _a.initialTimeStamp, userFilledMessageListQuery = _a.userFilledMessageListQuery, replyType = _a.replyType, setIsScrolled = _a.setIsScrolled;
    var logger = _b.logger, scrollRef = _b.scrollRef, messagesDispatcher = _b.messagesDispatcher;
    var channelUrl = currentGroupChannel === null || currentGroupChannel === void 0 ? void 0 : currentGroupChannel.url;
    /**
     * useCallback(() => {}, [currentGroupChannel]) was buggy, that is why we did
     * const channelUrl = currentGroupChannel && currentGroupChannel.url;
     * useCallback(() => {}, [channelUrl])
     * Again, this hook is supposed to execute when currentGroupChannel changes
     * The 'channelUrl' here is not the same memory reference from Conversation.props
     */
    var fetchMessages = React.useCallback(function () {
        logger.info('Channel useInitialMessagesFetch: Setup started', currentGroupChannel);
        setIsScrolled(false);
        messagesDispatcher({
            type: RESET_MESSAGES,
            payload: null,
        });
        if (currentGroupChannel && (currentGroupChannel === null || currentGroupChannel === void 0 ? void 0 : currentGroupChannel.getMessagesByTimestamp)) {
            var messageListParams_1 = {
                prevResultSize: PREV_RESULT_SIZE,
                isInclusive: true,
                includeReactions: true,
                includeMetaArray: true,
            };
            if (initialTimeStamp) {
                messageListParams_1.nextResultSize = NEXT_RESULT_SIZE;
            }
            if (replyType === 'QUOTE_REPLY' || replyType === 'THREAD') {
                messageListParams_1.includeThreadInfo = true;
                messageListParams_1.includeParentMessageInfo = true;
                messageListParams_1.replyType = message.ReplyType.ONLY_REPLY_TO_CHANNEL;
            }
            if (userFilledMessageListQuery) {
                Object.keys(userFilledMessageListQuery).forEach(function (key) {
                    // @ts-ignore
                    messageListParams_1[key] = userFilledMessageListQuery[key];
                });
            }
            if ((replyType
                && (replyType === 'QUOTE_REPLY' || replyType === 'THREAD'))
                || userFilledMessageListQuery) {
                logger.info('Channel useInitialMessagesFetch: Setup messageListParams', messageListParams_1);
                messagesDispatcher({
                    type: MESSAGE_LIST_PARAMS_CHANGED,
                    payload: messageListParams_1,
                });
            }
            logger.info('Channel: Fetching messages', { currentGroupChannel: currentGroupChannel, userFilledMessageListQuery: userFilledMessageListQuery });
            messagesDispatcher({
                type: FETCH_INITIAL_MESSAGES_START,
                payload: null,
            });
            currentGroupChannel
                .getMessagesByTimestamp(initialTimeStamp || new Date().getTime(), messageListParams_1)
                .then(function (messages) {
                messagesDispatcher({
                    type: FETCH_INITIAL_MESSAGES_SUCCESS,
                    payload: {
                        currentGroupChannel: currentGroupChannel,
                        messages: messages,
                    },
                });
            })
                .catch(function (error) {
                logger.error('Channel: Fetching messages failed', error);
                messagesDispatcher({
                    type: FETCH_INITIAL_MESSAGES_FAILURE,
                    payload: { currentGroupChannel: currentGroupChannel },
                });
            })
                .finally(function () {
                if (!initialTimeStamp) {
                    setTimeout(function () { return utils.scrollIntoLast(0, scrollRef, setIsScrolled); }, consts.SCROLL_BOTTOM_DELAY_FOR_FETCH);
                }
                else {
                    setTimeout(function () {
                        utils.scrollToRenderedMessage(scrollRef, initialTimeStamp, setIsScrolled);
                    }, 500);
                }
            });
        }
    }, [channelUrl, userFilledMessageListQuery, initialTimeStamp]);
    React.useEffect(function () {
        fetchMessages();
    }, [fetchMessages]);
    return fetchMessages;
}

exports.FETCH_INITIAL_MESSAGES_FAILURE = FETCH_INITIAL_MESSAGES_FAILURE;
exports.FETCH_INITIAL_MESSAGES_START = FETCH_INITIAL_MESSAGES_START;
exports.FETCH_INITIAL_MESSAGES_SUCCESS = FETCH_INITIAL_MESSAGES_SUCCESS;
exports.FETCH_NEXT_MESSAGES_FAILURE = FETCH_NEXT_MESSAGES_FAILURE;
exports.FETCH_NEXT_MESSAGES_SUCCESS = FETCH_NEXT_MESSAGES_SUCCESS;
exports.FETCH_PREV_MESSAGES_FAILURE = FETCH_PREV_MESSAGES_FAILURE;
exports.FETCH_PREV_MESSAGES_SUCCESS = FETCH_PREV_MESSAGES_SUCCESS;
exports.MARK_AS_READ = MARK_AS_READ;
exports.MESSAGE_LIST_PARAMS_CHANGED = MESSAGE_LIST_PARAMS_CHANGED;
exports.NEXT_RESULT_SIZE = NEXT_RESULT_SIZE;
exports.ON_FILE_INFO_UPLOADED = ON_FILE_INFO_UPLOADED;
exports.ON_MESSAGE_DELETED = ON_MESSAGE_DELETED;
exports.ON_MESSAGE_DELETED_BY_REQ_ID = ON_MESSAGE_DELETED_BY_REQ_ID;
exports.ON_MESSAGE_RECEIVED = ON_MESSAGE_RECEIVED;
exports.ON_MESSAGE_THREAD_INFO_UPDATED = ON_MESSAGE_THREAD_INFO_UPDATED;
exports.ON_MESSAGE_UPDATED = ON_MESSAGE_UPDATED;
exports.ON_REACTION_UPDATED = ON_REACTION_UPDATED;
exports.ON_TYPING_STATUS_UPDATED = ON_TYPING_STATUS_UPDATED;
exports.PREV_RESULT_SIZE = PREV_RESULT_SIZE;
exports.RESEND_MESSAGE_START = RESEND_MESSAGE_START;
exports.RESET_MESSAGES = RESET_MESSAGES;
exports.SEND_MESSAGE_FAILURE = SEND_MESSAGE_FAILURE;
exports.SEND_MESSAGE_START = SEND_MESSAGE_START;
exports.SEND_MESSAGE_SUCCESS = SEND_MESSAGE_SUCCESS;
exports.SET_CHANNEL_INVALID = SET_CHANNEL_INVALID;
exports.SET_CURRENT_CHANNEL = SET_CURRENT_CHANNEL;
exports.SET_EMOJI_CONTAINER = SET_EMOJI_CONTAINER;
exports.channelActions = channelActions;
exports.useInitialMessagesFetch = useInitialMessagesFetch;
//# sourceMappingURL=bundle-Dn7m9LzR.js.map
