import { parseISO, differenceInSeconds } from "date-fns";
/**
 * Simplify this function with optional chaining
return notification?.actionsHistory?.sort((a: IPerformedAction, b: IPerformedAction) => Date.parse(b.datePerformed) - Date.parse(a.datePerformed))?.[0]
 */
export const getMostRecentAction = (notification) => {
    if (!notification || !notification.actionsHistory)
        return null;
    let actionHistoryArray = [
        ...notification.actionsHistory.sort((a, b) => Date.parse(b.datePerformed) - Date.parse(a.datePerformed)),
    ];
    if (actionHistoryArray.length > 0) {
        return actionHistoryArray[0];
    }
    else {
        return null;
    }
};
export const isNewNotification = (notification, newNotificationHaloPeriodSeconds) => {
    const mostRecentAction = getMostRecentAction(notification);
    if ((mostRecentAction && mostRecentAction.type === "FINSEMBLE:READ") ||
        notification.isRead ||
        (notification.actionsHistory && notification.actionsHistory.length > 1)) {
        return false;
    }
    return differenceInSeconds(Date.now(), parseISO(notification.issuedAt)) < newNotificationHaloPeriodSeconds;
};
export const isReNotification = (notification, newNotificationHaloPeriodSeconds) => {
    const mostRecentAction = getMostRecentAction(notification);
    if (!mostRecentAction || mostRecentAction.type !== "FINSEMBLE:WAKE")
        return false;
    return differenceInSeconds(Date.now(), parseISO(mostRecentAction.datePerformed)) < newNotificationHaloPeriodSeconds;
};
export const getActiveFilter = (drawerControlProps) => drawerControlProps.controls.filter((control) => control.action === drawerControlProps.active)[0];
const search = (notification, searchField, searchString) => {
    var _a, _b, _c, _d;
    if (searchField) {
        return !!((_a = notification[searchField]) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(searchString.toLowerCase()));
    }
    else {
        return !!(((_b = notification["title"]) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes(searchString.toLowerCase())) ||
            ((_c = notification["details"]) === null || _c === void 0 ? void 0 : _c.toLowerCase().includes(searchString.toLowerCase())) ||
            ((_d = notification["source"]) === null || _d === void 0 ? void 0 : _d.toLowerCase().includes(searchString.toLowerCase())));
    }
};
export const searchNotifications = (notifications, searchTerm) => [...notifications].filter((notification) => {
    const searchTermSplit = searchTerm.split(":");
    if (searchTermSplit.length === 1) {
        return search(notification, null, searchTermSplit[0].trim());
    }
    else {
        const field = searchTermSplit[0].trim();
        if (["title", "details", "source"].includes(field) && notification.hasOwnProperty(field)) {
            return search(notification, field, searchTermSplit[1].trim());
        }
        else {
            return search(notification, null, searchTermSplit[1].trim());
        }
    }
});
export const getMuteFilterFromNotification = (notification) => {
    const muteFilter = {};
    if (notification.source) {
        muteFilter.source = notification.source;
    }
    if (notification.type) {
        muteFilter.type = notification.type;
    }
    return muteFilter;
};
//# sourceMappingURL=utils.js.map