import React, { useContext, useEffect } from "react";
import { isToday, isYesterday, isThisWeek } from "date-fns";
// Context
import { NotificationsContext } from "../../../notificationsContext";
// Components
import { NotificationCardShell } from "../../shared/NotificationCardShell";
import NoNotifications from "../../NoNotifications";
// Utils
import { getActiveFilter, searchNotifications } from "../../../utils";
import { ErrorBoundary } from "../../../../common/ErrorBoundary";
var Periods;
(function (Periods) {
    Periods["TODAY"] = "today";
    Periods["THIS_WEEK"] = "thisWeek";
    Periods["YESTERDAY"] = "yesterday";
    Periods["OLDER"] = "older";
})(Periods || (Periods = {}));
const isEmpty = (byPeriodNotifications, accountForSnooze) => {
    if (accountForSnooze) {
        return Object.values(byPeriodNotifications).every((value) => value.length === 0);
    }
    else {
        return Object.values(byPeriodNotifications).every((value) => value.length === 0 || value.filter((notification) => !notification.isSnoozed).length === 0);
    }
};
export const CardView = (props) => {
    var _a;
    const { notifications } = props;
    const notificationsContext = useContext(NotificationsContext);
    const { drawerControlProps, searchString, filteredNotifications, setFilteredNotifications } = notificationsContext;
    const activeFilter = getActiveFilter(drawerControlProps);
    const byPeriodNotifications = {
        today: [],
        yesterday: [],
        thisWeek: [],
        older: [],
    };
    useEffect(() => {
        if (searchString === "") {
            setFilteredNotifications(notifications);
        }
        else {
            setFilteredNotifications(searchNotifications(notifications, searchString));
        }
    }, [searchString, notifications]);
    const controlFilteredNotifications = activeFilter ? (_a = activeFilter.run) === null || _a === void 0 ? void 0 : _a.call(activeFilter, filteredNotifications) : [];
    controlFilteredNotifications.reduce((accumulator, notification) => {
        const notificationDate = Date.parse(notification.issuedAt);
        if (isToday(notificationDate)) {
            accumulator.today.push(notification);
        }
        else if (isYesterday(notificationDate)) {
            accumulator.yesterday.push(notification);
        }
        else if (isThisWeek(notificationDate)) {
            accumulator.thisWeek.push(notification);
        }
        else {
            accumulator.older.push(notification);
        }
        return accumulator;
    }, byPeriodNotifications);
    const NotificationCardComponent = props.notificationCard ? props.notificationCard : NotificationCardShell;
    const renderSeparatorLabel = (period) => {
        if (period === Periods.TODAY) {
            return React.createElement("p", { className: "notification-day" }, "Today");
        }
        else if (period === Periods.YESTERDAY) {
            return React.createElement("p", { className: "notification-day" }, "Yesterday");
        }
        else if (period === Periods.THIS_WEEK) {
            return React.createElement("p", { className: "notification-day" }, "This Week");
        }
        else {
            return React.createElement("p", { className: "notification-day" }, "Older");
        }
    };
    const renderNotificationsBlock = (period) => byPeriodNotifications[period]
        .sort((a, b) => Date.parse(b.issuedAt) - Date.parse(a.issuedAt))
        .filter((notification) => (activeFilter.action === "snoozed" ? notification.isSnoozed : !notification.isSnoozed))
        .map((notification, index) => {
        const key = `${index}_${notification.id ? notification.id : notification.issuedAt}`;
        return (React.createElement("div", { key: key },
            index === 0 ? renderSeparatorLabel(period) : null,
            React.createElement(ErrorBoundary, { key: key },
                React.createElement(NotificationCardComponent, { key: notification.id, notification: notification, removeNotification: () => { }, uiContext: "center" })),
            React.createElement("div", { className: "divider" }, "\u00A0")));
    });
    return isEmpty(byPeriodNotifications, activeFilter.action === "snoozed") ? (React.createElement(NoNotifications, null)) : (React.createElement(React.Fragment, null, Object.keys(byPeriodNotifications).map((period) => renderNotificationsBlock(period))));
};
export default CardView;
//# sourceMappingURL=CardView.js.map