"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processPostFilters = processPostFilters;
const postStatus_1 = require("../../../discussions/api/enums/postStatus");
const postType_1 = require("../../../discussions/api/enums/postType");
const sharingAccess_1 = require("../../../discussions/api/enums/sharingAccess");
const bboxStringToGeoJSONPolygon_1 = require("../bboxStringToGeoJSONPolygon");
const toEnumConverters_1 = require("../hubEventsHelpers/toEnumConverters");
const processChannelFilters_1 = require("./processChannelFilters");
/**
 * Builds a Partial<ISearchPosts> given an Array of IFilter objects
 * @param filters An Array of IFilter
 * @returns A Partial<ISearchPosts> for the given Array of IFilter objects
 */
function processPostFilters(filters) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    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, sharingAccess_1.SharingAccess);
    }
    // bbox
    if ((_b = flattenedFilters.bbox) === null || _b === void 0 ? void 0 : _b.length) {
        // for now, just map bbox to the `geometry` filter. a `featureGeometry` filter
        // also exists, but we cannot use it in combination with `geometry` because
        // the API will `AND` those conditions together. if we desire the ability to
        // match posts whose `geometry` OR `featureGeometry` interesect the provided bbox,
        // we will need a new API parameter, and update this mapping to use it.
        const polygon = (0, bboxStringToGeoJSONPolygon_1.bboxStringToGeoJSONPolygon)(flattenedFilters.bbox[0]);
        processedFilters.geometry = polygon;
    }
    // `term` searches against both `title` and `body` so
    // ignore `title` and `body` filters when `term` is provied
    if ((_c = flattenedFilters.term) === null || _c === void 0 ? void 0 : _c.length) {
        processedFilters.term = flattenedFilters.term[0];
    }
    else {
        // title
        if ((_d = flattenedFilters.title) === null || _d === void 0 ? void 0 : _d.length) {
            processedFilters.title = flattenedFilters.title[0];
        }
        // body
        if ((_e = flattenedFilters.body) === null || _e === void 0 ? void 0 : _e.length) {
            processedFilters.body = flattenedFilters.body[0];
        }
    }
    // channels
    if ((_f = flattenedFilters.channel) === null || _f === void 0 ? void 0 : _f.length) {
        processedFilters.channels = flattenedFilters.channel;
    }
    // creator
    if ((_g = flattenedFilters.owner) === null || _g === void 0 ? void 0 : _g.length) {
        processedFilters.creator = flattenedFilters.owner[0];
    }
    // discussion
    if ((_h = flattenedFilters.discussion) === null || _h === void 0 ? void 0 : _h.length) {
        processedFilters.discussion = flattenedFilters.discussion[0];
    }
    // editor
    if ((_j = flattenedFilters.editor) === null || _j === void 0 ? void 0 : _j.length) {
        processedFilters.editor = flattenedFilters.editor[0];
    }
    // status
    if ((_k = flattenedFilters.status) === null || _k === void 0 ? void 0 : _k.length) {
        processedFilters.status = (0, toEnumConverters_1.toEnums)(flattenedFilters.status, postStatus_1.PostStatus);
    }
    // parents
    if (Array.isArray(flattenedFilters.parentId)) {
        processedFilters.parents = flattenedFilters.parentId;
    }
    // groups
    if ((_l = flattenedFilters.groups) === null || _l === void 0 ? void 0 : _l.length) {
        processedFilters.groups = flattenedFilters.groups;
    }
    // post type
    if ((_m = flattenedFilters.postType) === null || _m === void 0 ? void 0 : _m.length) {
        processedFilters.postType = (0, toEnumConverters_1.toEnum)(flattenedFilters.postType[0], postType_1.PostType);
    }
    // created
    if ((_o = flattenedFilters.created) === null || _o === void 0 ? void 0 : _o.length) {
        processedFilters.createdBefore = new Date(flattenedFilters.created[0].to);
        processedFilters.createdAfter = new Date(flattenedFilters.created[0].from);
    }
    // modified
    if ((_p = flattenedFilters.modified) === null || _p === void 0 ? void 0 : _p.length) {
        processedFilters.updatedBefore = new Date(flattenedFilters.modified[0].to);
        processedFilters.updatedAfter = new Date(flattenedFilters.modified[0].from);
    }
    return processedFilters;
}
//# sourceMappingURL=processPostFilters.js.map