"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processFilters = processFilters;
const orval_events_1 = require("../../../events/api/orval/api/orval-events");
const toEnumConverters_1 = require("./toEnumConverters");
const processChannelFilters_1 = require("../hubDiscussionsHelpers/processChannelFilters");
const util_1 = require("../../../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
 */
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;
    const flattenedFilters = (0, processChannelFilters_1.flattenFilters)(filters);
    const processedFilters = {};
    // access
    if ((_a = flattenedFilters.access) === null || _a === void 0 ? void 0 : _a.length) {
        processedFilters.access = (0, toEnumConverters_1.toEnums)(flattenedFilters.access, orval_events_1.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 = (0, toEnumConverters_1.toEnums)(flattenedFilters.entityType, orval_events_1.EventAssociationEntityType);
    }
    // id
    if ((_e = flattenedFilters.id) === null || _e === void 0 ? void 0 : _e.length) {
        const { ids, notIds } = (0, processChannelFilters_1.parseIdsAndNotIds)(flattenedFilters.id);
        if (ids.length) {
            processedFilters.eventIds = ids.filter(util_1.unique);
        }
        if (notIds.length) {
            processedFilters.notEventIds = notIds.filter(util_1.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.orgId = flattenedFilters.orgId[0];
    }
    // 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 } = (0, processChannelFilters_1.parseIdsAndNotIds)(flattenedFilters.group);
        if (ids.length) {
            processedFilters.sharedToGroups = ids;
        }
        if (notIds.length) {
            const uniqueIds = notIds.filter(util_1.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 = (0, toEnumConverters_1.toEnums)(flattenedFilters.attendanceType, orval_events_1.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)
        ? (0, toEnumConverters_1.toEnums)(flattenedFilters.status, orval_events_1.EventStatus)
        : [orval_events_1.EventStatus.PLANNED, orval_events_1.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) {
        // We are explicitly checking if the to and from values are present
        // Because w/ Occurrence, we can have just to or from values
        // if you are updating this file, remove this comment and address the lint error
        /* eslint-disable @typescript-eslint/no-unused-expressions */
        flattenedFilters.startDateRange[0].to &&
            (processedFilters.startDateTimeBefore = new Date(flattenedFilters.startDateRange[0].to).toISOString());
        flattenedFilters.startDateRange[0].from &&
            (processedFilters.startDateTimeAfter = new Date(flattenedFilters.startDateRange[0].from).toISOString());
        /* eslint-enable @typescript-eslint/no-unused-expressions */
    }
    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
        // Because w/ Occurrence, we can have just to or from values
        // if you are updating this file, remove this comment and address the lint error
        /* eslint-disable @typescript-eslint/no-unused-expressions */
        flattenedFilters.endDateRange[0].to &&
            (processedFilters.endDateTimeBefore = new Date(flattenedFilters.endDateRange[0].to).toISOString());
        flattenedFilters.endDateRange[0].from &&
            (processedFilters.endDateTimeAfter = new Date(flattenedFilters.endDateRange[0].from).toISOString());
        /* eslint-enable @typescript-eslint/no-unused-expressions */
    }
    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 there's an occurrence filter, we need to adjust the startDateTimeBefore, startDateTimeAfter
    // Depending on the occurrence.
    if ((_v = flattenedFilters.occurrence) === null || _v === void 0 ? void 0 : _v.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 (((_w = flattenedFilters.modified) === null || _w === void 0 ? void 0 : _w.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