import React__default, { useState } from 'react';
import { C as ChannelPreview } from './bundle-szN3I4WN.js';
import { C as ChannelPreviewAction } from './bundle-NFkyRDpH.js';
import { u as useChannelListContext, L as LEAVE_CHANNEL_SUCCESS, S as SET_CURRENT_CHANNEL } from './bundle--LjMBMp3.js';
import { useSendbirdStateContext } from '../useSendbirdStateContext.js';
import { G as GroupChannelListHeader } from './bundle-0Pl6hdf0.js';
import EditUserProfile from '../EditUserProfile.js';
import PlaceHolder, { PlaceHolderTypes } from '../ui/PlaceHolder.js';
import { u as useOnScrollPositionChangeDetector } from './bundle-OYvgeZlT.js';
import { A as AddChannel } from './bundle-YBExtK73.js';

var GroupChannelListUIView = function (_a) {
    var renderHeader = _a.renderHeader, renderPlaceHolderError = _a.renderPlaceHolderError, renderPlaceHolderLoading = _a.renderPlaceHolderLoading, renderPlaceHolderEmptyList = _a.renderPlaceHolderEmptyList, onChangeTheme = _a.onChangeTheme, onUserProfileUpdated = _a.onUserProfileUpdated, allowProfileEdit = _a.allowProfileEdit, channels = _a.channels, onLoadMore = _a.onLoadMore, initialized = _a.initialized, renderChannel = _a.renderChannel, renderAddChannel = _a.renderAddChannel;
    var _b = useState(false), showProfileEdit = _b[0], setShowProfileEdit = _b[1];
    var stores = useSendbirdStateContext().stores;
    var renderer = {
        addChannel: renderAddChannel,
        channel: renderChannel,
        placeholder: {
            loading: function () {
                if (initialized)
                    return null;
                if (renderPlaceHolderLoading)
                    return renderPlaceHolderLoading();
                return React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.LOADING });
            },
            empty: function () {
                if (!initialized)
                    return null;
                if (renderPlaceHolderEmptyList)
                    return renderPlaceHolderEmptyList();
                return React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.NO_CHANNELS });
            },
            error: function () {
                if (!initialized || !stores.sdkStore.error)
                    return null;
                if (renderPlaceHolderError)
                    return renderPlaceHolderError();
                return React__default.createElement(PlaceHolder, { type: PlaceHolderTypes.WRONG });
            },
        },
    };
    return (React__default.createElement(React__default.Fragment, null,
        React__default.createElement("div", { className: "sendbird-channel-list__header" }, (renderHeader === null || renderHeader === void 0 ? void 0 : renderHeader()) || (React__default.createElement(GroupChannelListHeader, { onEdit: function () { return allowProfileEdit && setShowProfileEdit(true); }, allowProfileEdit: allowProfileEdit, renderIconButton: function () { return renderer.addChannel(); } }))),
        showProfileEdit && (React__default.createElement(EditUserProfile, { onThemeChange: onChangeTheme, onCancel: function () { return setShowProfileEdit(false); }, onEditProfile: function (user) {
                setShowProfileEdit(false);
                onUserProfileUpdated(user);
            } })),
        React__default.createElement(ChannelListComponent, { data: channels, renderItem: renderer.channel, onLoadMore: onLoadMore, placeholderLoading: renderer.placeholder.loading(), placeholderEmpty: renderer.placeholder.empty(), placeholderError: renderer.placeholder.error() })));
};
/**
 * To do: Implement windowing
 * Implement windowing if you are dealing with large number of messages/channels
 * https://github.com/bvaughn/react-window -> recommendation
 * We hesitate to bring one more dependency to our library,
 * we are planning to implement it inside the library
 * */
var ChannelListComponent = function (props) {
    var data = props.data, renderItem = props.renderItem, onLoadMore = props.onLoadMore, placeholderLoading = props.placeholderLoading, placeholderError = props.placeholderError, placeholderEmpty = props.placeholderEmpty;
    var onScroll = useOnScrollPositionChangeDetector({
        onReachedBottom: function () { return onLoadMore(); },
    });
    return (React__default.createElement("div", { className: 'sendbird-channel-list__body', onScroll: onScroll },
        placeholderError,
        React__default.createElement("div", null, data.map(function (item, index) { return renderItem({ item: item, index: index }); })),
        placeholderLoading,
        data.length === 0 && placeholderEmpty));
};

var ChannelListUI = function (props) {
    var renderHeader = props.renderHeader, renderChannelPreview = props.renderChannelPreview, renderPlaceHolderError = props.renderPlaceHolderError, renderPlaceHolderLoading = props.renderPlaceHolderLoading, renderPlaceHolderEmptyList = props.renderPlaceHolderEmptyList;
    var _a = useChannelListContext(), onThemeChange = _a.onThemeChange, allowProfileEdit = _a.allowProfileEdit, allChannels = _a.allChannels, currentChannel = _a.currentChannel, channelListDispatcher = _a.channelListDispatcher, typingChannels = _a.typingChannels, initialized = _a.initialized, fetchChannelList = _a.fetchChannelList, onProfileEditSuccess = _a.onProfileEditSuccess;
    var _b = useSendbirdStateContext(), stores = _b.stores, config = _b.config;
    var logger = config.logger, _c = config.isOnline, isOnline = _c === void 0 ? false : _c;
    var sdk = stores.sdkStore.sdk;
    var renderListItem = function (props) {
        var channel = props.item, index = props.index;
        var onLeaveChannel = function (channel, cb) {
            logger.info('ChannelList: Leaving channel', channel);
            channel
                .leave()
                .then(function (res) {
                logger.info('ChannelList: Leaving channel success', res);
                if (cb && typeof cb === 'function')
                    cb(channel, null);
                channelListDispatcher({
                    type: LEAVE_CHANNEL_SUCCESS,
                    payload: channel.url,
                });
            })
                .catch(function (err) {
                logger.error('ChannelList: Leaving channel failed', err);
                if (cb && typeof cb === 'function')
                    cb(channel, err);
            });
        };
        var onClickChannel = function () {
            if (!isOnline && !(sdk === null || sdk === void 0 ? void 0 : sdk.isCacheEnabled)) {
                logger.warning('ChannelList: Inactivated clicking channel item during offline.');
                return;
            }
            logger.info('ChannelList: Clicked on channel:', channel);
            channelListDispatcher({
                type: SET_CURRENT_CHANNEL,
                payload: channel,
            });
        };
        if (renderChannelPreview) {
            return (React__default.createElement("div", { key: channel === null || channel === void 0 ? void 0 : channel.url, onClick: onClickChannel }, renderChannelPreview({ channel: channel, onLeaveChannel: onLeaveChannel })));
        }
        return (React__default.createElement(ChannelPreview, { key: channel === null || channel === void 0 ? void 0 : channel.url, tabIndex: index, onClick: onClickChannel, channel: channel, onLeaveChannel: function () { return onLeaveChannel(channel, null); }, isActive: (channel === null || channel === void 0 ? void 0 : channel.url) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url), isTyping: typingChannels === null || typingChannels === void 0 ? void 0 : typingChannels.some(function (_a) {
                var url = _a.url;
                return url === (channel === null || channel === void 0 ? void 0 : channel.url);
            }), renderChannelAction: function () { return (React__default.createElement(ChannelPreviewAction, { channel: channel, disabled: !isOnline, onLeaveChannel: function () { return onLeaveChannel(channel, null); } })); } }));
    };
    return (React__default.createElement(GroupChannelListUIView, { renderHeader: renderHeader, renderChannel: renderListItem, renderPlaceHolderError: renderPlaceHolderError, renderPlaceHolderLoading: renderPlaceHolderLoading, renderPlaceHolderEmptyList: renderPlaceHolderEmptyList, onChangeTheme: onThemeChange, allowProfileEdit: allowProfileEdit, onUserProfileUpdated: onProfileEditSuccess, channels: allChannels, onLoadMore: fetchChannelList, initialized: initialized, renderAddChannel: function () { return React__default.createElement(AddChannel, null); } }));
};

export { ChannelListUI as C, GroupChannelListUIView as G };
//# sourceMappingURL=bundle-iXzQekqM.js.map
