import { EventAccess, EventAssociationEntityType, EventAttendanceType, EventStatus, } from "../../../events/api/orval/api/orval-events";
import { toEnums } from "./toEnumConverters";
import { flattenFilters, parseIdsAndNotIds, } from "../hubDiscussionsHelpers/processChannelFilters";
import { unique } from "../../../util";
/**
 * Builds a Partial<ISearchEvents> given an Array of IFilter objects
 * @param filters An Array of IFilter
 * @returns a Partial<ISearchEvents> for the given Array of IFilter objects
 */
export function processFilters(filters) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
    const flattenedFilters = flattenFilters(filters);
    const processedFilters = {};
    // access
    if ((_a = flattenedFilters.access) === null || _a === void 0 ? void 0 : _a.length) {
        processedFilters.access = toEnums(flattenedFilters.access, EventAccess);
    }
    // canEdit
    if ((_b = flattenedFilters.canEdit) === null || _b === void 0 ? void 0 : _b.length) {
        processedFilters.canEdit = flattenedFilters.canEdit[0];
    }
    // entityId
    if ((_c = flattenedFilters.entityId) === null || _c === void 0 ? void 0 : _c.length) {
        processedFilters.entityIds = flattenedFilters.entityId;
    }
    // entityType
    if ((_d = flattenedFilters.entityType) === null || _d === void 0 ? void 0 : _d.length) {
        processedFilters.entityTypes = toEnums(flattenedFilters.entityType, EventAssociationEntityType);
    }
    // id
    if ((_e = flattenedFilters.id) === null || _e === void 0 ? void 0 : _e.length) {
        const { ids, notIds } = parseIdsAndNotIds(flattenedFilters.id);
        if (ids.length) {
            processedFilters.eventIds = ids.filter(unique);
        }
        if (notIds.length) {
            processedFilters.notEventIds = notIds.filter(unique);
        }
    }
    // term
    if ((_f = flattenedFilters.term) === null || _f === void 0 ? void 0 : _f.length) {
        processedFilters.title = flattenedFilters.term[0];
    }
    // orgId
    if ((_g = flattenedFilters.orgId) === null || _g === void 0 ? void 0 : _g.length) {
        processedFilters.orgIds = flattenedFilters.orgId;
    }
    // categories
    if ((_h = flattenedFilters.categories) === null || _h === void 0 ? void 0 : _h.length) {
        processedFilters.categories = flattenedFilters.categories;
    }
    // tags
    if ((_j = flattenedFilters.tags) === null || _j === void 0 ? void 0 : _j.length) {
        processedFilters.tags = flattenedFilters.tags;
    }
    // group
    if ((_k = flattenedFilters.group) === null || _k === void 0 ? void 0 : _k.length) {
        const { ids, notIds } = parseIdsAndNotIds(flattenedFilters.group);
        if (ids.length) {
            processedFilters.sharedToGroups = ids;
        }
        if (notIds.length) {
            const uniqueIds = notIds.filter(unique);
            // we do not currently have an inverse parameter for `sharedToGroups` at this time. instead, we have separate
            // params for `withoutEditGroups` and `withoutReadGroups`. rather than trying to reconcile whether
            // any given group ID belongs to a view or update group (which would require additional HTTP requests),
            // we simply pass all the ids to both `withoutEditGroups` and `withoutReadGroups`.
            processedFilters.withoutEditGroups = uniqueIds;
            processedFilters.withoutReadGroups = uniqueIds;
        }
    }
    // attendanceType
    if ((_l = flattenedFilters.attendanceType) === null || _l === void 0 ? void 0 : _l.length) {
        processedFilters.attendanceTypes = toEnums(flattenedFilters.attendanceType, EventAttendanceType);
    }
    // owner
    if ((_m = flattenedFilters.owner) === null || _m === void 0 ? void 0 : _m.length) {
        processedFilters.createdByIds = flattenedFilters.owner;
    }
    // status
    processedFilters.status = ((_o = flattenedFilters.status) === null || _o === void 0 ? void 0 : _o.length)
        ? toEnums(flattenedFilters.status, EventStatus)
        : [EventStatus.PLANNED, EventStatus.CANCELED];
    // if a startDateRange was provided, we prioritize that over individual startDateBefore or startDateAfter
    // filters to prevent collisions
    if ((_p = flattenedFilters.startDateRange) === null || _p === void 0 ? void 0 : _p.length) {
        const dateRange = flattenedFilters.startDateRange[0];
        if (dateRange.to) {
            processedFilters.startDateTimeBefore = new Date(dateRange.to).toISOString();
        }
        if (dateRange.from) {
            processedFilters.startDateTimeAfter = new Date(dateRange.from).toISOString();
        }
    }
    else {
        // startDateBefore
        if ((_q = flattenedFilters.startDateBefore) === null || _q === void 0 ? void 0 : _q.length) {
            processedFilters.startDateTimeBefore = new Date(flattenedFilters.startDateBefore[0]).toISOString();
        }
        // startDateAfter
        if ((_r = flattenedFilters.startDateAfter) === null || _r === void 0 ? void 0 : _r.length) {
            processedFilters.startDateTimeAfter = new Date(flattenedFilters.startDateAfter[0]).toISOString();
        }
    }
    // if a endDateRange was provided, we prioritize that over individual endDateBefore or endDateAfter
    // filters to prevent collisions
    if ((_s = flattenedFilters.endDateRange) === null || _s === void 0 ? void 0 : _s.length) {
        // We are explicitly checking if the to and from values are present
        const dateRange = flattenedFilters.endDateRange[0];
        if (dateRange.to) {
            processedFilters.endDateTimeBefore = new Date(dateRange.to).toISOString();
        }
        if (dateRange.from) {
            processedFilters.endDateTimeAfter = new Date(dateRange.from).toISOString();
        }
    }
    else {
        // endDateBefore
        if ((_t = flattenedFilters.endDateBefore) === null || _t === void 0 ? void 0 : _t.length) {
            processedFilters.endDateTimeBefore = new Date(flattenedFilters.endDateBefore[0]).toISOString();
        }
        // endDateAfter
        if ((_u = flattenedFilters.endDateAfter) === null || _u === void 0 ? void 0 : _u.length) {
            processedFilters.endDateTimeAfter = new Date(flattenedFilters.endDateAfter[0]).toISOString();
        }
    }
    // if a createdDateRange was provided, we prioritize that over individual createdDateBefore or createdDateAfter
    // filters to prevent collisions
    if ((_v = flattenedFilters.createdDateRange) === null || _v === void 0 ? void 0 : _v.length) {
        // We are explicitly checking if the to and from values are present
        const dateRange = flattenedFilters.createdDateRange[0];
        if (dateRange.to) {
            processedFilters.createdAtBefore = new Date(dateRange.to).toISOString();
        }
        if (dateRange.from) {
            processedFilters.createdAtAfter = new Date(dateRange.from).toISOString();
        }
    }
    else {
        // createdDateBefore
        if ((_w = flattenedFilters.createdDateBefore) === null || _w === void 0 ? void 0 : _w.length) {
            processedFilters.createdAtBefore = new Date(flattenedFilters.createdDateBefore[0]).toISOString();
        }
        // createdDateAfter
        if ((_x = flattenedFilters.createdDateAfter) === null || _x === void 0 ? void 0 : _x.length) {
            processedFilters.createdAtAfter = new Date(flattenedFilters.createdDateAfter[0]).toISOString();
        }
    }
    // If there's an occurrence filter, we need to adjust the startDateTimeBefore, startDateTimeAfter
    // Depending on the occurrence.
    if ((_y = flattenedFilters.occurrence) === null || _y === void 0 ? void 0 : _y.length) {
        flattenedFilters.occurrence.forEach((o) => {
            switch (o) {
                case "upcoming":
                    processedFilters.startDateTimeAfter = new Date().toISOString();
                    break;
                case "past":
                    processedFilters.endDateTimeBefore = new Date().toISOString();
                    break;
                case "inProgress":
                    processedFilters.startDateTimeBefore = new Date().toISOString();
                    processedFilters.endDateTimeAfter = new Date().toISOString();
                    break;
            }
        });
    }
    // modified
    if (((_z = flattenedFilters.modified) === null || _z === void 0 ? void 0 : _z.length) &&
        flattenedFilters.modified[0].to &&
        flattenedFilters.modified[0].from) {
        // range
        processedFilters.updatedAtBefore = new Date(flattenedFilters.modified[0].to).toISOString();
        processedFilters.updatedAtAfter = new Date(flattenedFilters.modified[0].from).toISOString();
    }
    return processedFilters;
}
//# sourceMappingURL=processFilters.js.map