"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const browser_utils_1 = require("../../../utils/browser-utils");
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const components_1 = require("../../../themes/components");
const MediaRoomService_1 = require("../../../services/MediaRoomService");
const PeersService_1 = require("../../../services/PeersService");
const roomSlice_1 = require("../../../store/roomSlice");
const borderRadius_1 = __importDefault(require("../../../themes/foundations/borderRadius"));
const ConfigProvider_1 = require("../../../utils/ConfigProvider");
const APITypes_1 = require("../../../types/APITypes");
const CreateNewPoll_1 = __importDefault(require("./CreateNewPoll"));
const PollsCard_1 = __importDefault(require("./PollsCard"));
const foundations_1 = require("../../../themes/foundations");
function PollsTab() {
    const { sessionConfig } = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const pollsCreationAllowed = sessionConfig.permissions.polls.can_create;
    const { isOpen, onClose, onOpen } = browser_utils_1.useDisclosure();
    const { mediaRoom } = react_1.useContext(MediaRoomService_1.MediaRoomContext);
    const { selfPeer } = react_1.useContext(PeersService_1.PeersServiceContext);
    const polls = react_redux_1.useSelector((state) => state.room.polls);
    const windowDimensions = browser_utils_1.useWindowDimensions();
    const { height, width } = browser_utils_1.useScale(sessionConfig.theme.dimensions || windowDimensions);
    const drawerHeight = Math.floor((height * 2) / 3);
    const onToggle = () => {
        const ot = isOpen ? onClose : onOpen;
        ot();
    };
    const chatMessages = react_redux_1.useSelector((state) => state.room.chatMessages);
    const unread = chatMessages.filter((m) => m.read !== true && m.type === APITypes_1.MessageTypes.poll);
    const dispatch = react_redux_1.useDispatch();
    react_1.useEffect(() => {
        if (unread.length > 0) {
            dispatch(roomSlice_1.markMessagesRead({ [APITypes_1.MessageTypes.poll]: true }));
        }
    }, [unread]);
    const pollsArr = Object.values(polls);
    return (react_1.default.createElement(components_1.Flex, { height: height - 30, width: width, direction: "column", justifyContent: "space-between", overflowY: "auto", px: 10, pb: 10, pt: 50 },
        react_1.default.createElement(components_1.Flex, null,
            react_1.default.createElement(components_1.Box, { overflowY: "auto", className: "dyte-client-noScrollbar" }, !isOpen ? (react_1.default.createElement(react_1.default.Fragment, null, pollsArr.length > 0 ? (react_1.default.createElement(react_native_1.ScrollView, { style: {
                    height: pollsCreationAllowed
                        ? height - 150
                        : height,
                }, contentContainerStyle: {
                    paddingBottom: !pollsCreationAllowed
                        ? 120
                        : 0,
                } },
                react_1.default.createElement(react_native_1.TouchableWithoutFeedback, null,
                    react_1.default.createElement(components_1.Box, null, pollsArr.map((poll, index) => (react_1.default.createElement(components_1.Box, { my: 10 },
                        react_1.default.createElement(components_1.Text, { ml: 4 }, poll.createdBy
                            ? `Poll by ${poll.createdBy}`
                            : ''),
                        react_1.default.createElement(components_1.Box, { mt: 1, className: "dyte-client-bubble dyte-client-other dyte-client-type-3" },
                            react_1.default.createElement(PollsCard_1.default, { mediaRoom: mediaRoom, poll: poll, selfPeer: selfPeer }))))))))) : (react_1.default.createElement(components_1.Flex, { height: height - 150, width: width, align: "center", justify: "center" },
                react_1.default.createElement(components_1.Text, { fontSize: "lg", color: foundations_1.colors.textPrimary['100'] }, "No Polls created yet."))))) : (react_1.default.createElement(components_1.Box, null, isOpen && react_1.default.createElement(CreateNewPoll_1.default, { toggle: onToggle }))))),
        pollsCreationAllowed && (react_1.default.createElement(components_1.Button, { fontSize: "16", onClick: onToggle },
            react_1.default.createElement(components_1.Box, { bg: isOpen ? 'danger.500' : 'primary.500', p: 8, justify: "center", borderRadius: borderRadius_1.default.small },
                react_1.default.createElement(components_1.Text, { textAlign: "center" }, isOpen
                    ? 'Cancel Poll Creation'
                    : 'Create New Poll'))))));
}
exports.default = PollsTab;
//# sourceMappingURL=index.js.map