import { ChannelFilter } from "../../../discussions/api/enums/channelFilter";
import { unique } from "../../../util";
/**
 * @private
 *
 * Transforms an array of unknown values into an object containing `ids` and `notIds` arrays.
 *
 * @param values An array of unknown values
 * @returns and object containing an `ids` and `notIds` array
 */
export const parseIdsAndNotIds = (values) => {
    return values.reduce((acc, id) => {
        if (typeof id === "object" && id !== null && "not" in id) {
            return Object.assign(Object.assign({}, acc), { notIds: [
                    ...acc.notIds,
                    ...(Array.isArray(id.not)
                        ? id.not
                        : [id.not]),
                ] });
        }
        else if (typeof id === "string") {
            return Object.assign(Object.assign({}, acc), { ids: [...acc.ids, id] });
        }
        else {
            return acc;
        }
    }, {
        ids: [],
        notIds: [],
    });
};
/**
 * @private
 *
 * Flattens an array of IFilter objects into a map of key/value pairs where the key is the name of the
 * predicate and the value is a unique array of all values for that predicate name spanning all collections
 * and predicates. Uniqueness is only guaranteed for primitives (i.e. strings, boolean, number, etc). E.g.
 *
 * const x = flattenFilters([{
 *   predicates: [{
 *     access: 'public',
 *   }, {
 *     access: 'org',
 *   }],
 * }, {
 *   predicates: [{
 *     access: ['private', 'org]
 *   }]
 * }]);
 *
 * x // => { access: ['public', 'org', 'private']}
 * @param filters An array of IFilter objects
 * @returns an object containing key/value pairs of parameter values
 */
export function flattenFilters(filters) {
    return filters.reduce((acc1, filter) => filter.predicates.reduce((acc2, predicate) => Object.keys(predicate).reduce((acc3, predicateKey) => {
        const valuesToAdd = Array.isArray(predicate[predicateKey])
            ? predicate[predicateKey]
            : [predicate[predicateKey]];
        const values = valuesToAdd.reduce((acc4, valueToAdd) => acc4.includes(valueToAdd) ? acc4 : [...acc4, valueToAdd], acc3[predicateKey] || []);
        return Object.assign(Object.assign({}, acc3), { [predicateKey]: values });
    }, acc2), acc1), {});
}
/**
 * @private
 * Converts an IFilter array into a partial ISearchChannels
 * object of filter parameters
 * @param filters An array of IFilter objects
 * @returns a partial ISearchChannels object
 */
export function processChannelFilters(filters) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    const channelOptions = {};
    const flattenedFilters = flattenFilters(filters);
    // term
    if ((_a = flattenedFilters.term) === null || _a === void 0 ? void 0 : _a.length) {
        channelOptions.name = flattenedFilters.term[0];
    }
    // group
    if ((_b = flattenedFilters.group) === null || _b === void 0 ? void 0 : _b.length) {
        channelOptions.groups = flattenedFilters.group;
    }
    // access
    if ((_c = flattenedFilters.access) === null || _c === void 0 ? void 0 : _c.length) {
        channelOptions.access = flattenedFilters.access;
    }
    // orgId
    if ((_d = flattenedFilters.orgId) === null || _d === void 0 ? void 0 : _d.length) {
        channelOptions.orgIds = flattenedFilters.orgId;
    }
    // discussion
    if ((_e = flattenedFilters.discussion) === null || _e === void 0 ? void 0 : _e.length) {
        channelOptions.discussion = flattenedFilters.discussion[0];
    }
    // hasUserPosts
    if (((_f = flattenedFilters.hasUserPosts) === null || _f === void 0 ? void 0 : _f[0]) === true) {
        channelOptions.filterBy = ChannelFilter.HAS_USER_POSTS;
    }
    // role
    if ((_g = flattenedFilters.role) === null || _g === void 0 ? void 0 : _g.length) {
        channelOptions.roles = flattenedFilters.role;
    }
    // owner
    if ((_h = flattenedFilters.owner) === null || _h === void 0 ? void 0 : _h.length) {
        channelOptions.creator = flattenedFilters.owner[0];
    }
    // id
    if ((_j = flattenedFilters.id) === null || _j === void 0 ? void 0 : _j.length) {
        const { ids, notIds } = parseIdsAndNotIds(flattenedFilters.id);
        if (ids.length) {
            channelOptions.ids = ids;
        }
        if (notIds.length) {
            channelOptions.notIds = notIds.filter(unique);
        }
    }
    // createdDateRange
    if ((_k = flattenedFilters.createdDateRange) === null || _k === void 0 ? void 0 : _k.length) {
        channelOptions.createdBefore = new Date(flattenedFilters.createdDateRange[0].to);
        channelOptions.createdAfter = new Date(flattenedFilters.createdDateRange[0].from);
    }
    // updatedDateRange
    if ((_l = flattenedFilters.updatedDateRange) === null || _l === void 0 ? void 0 : _l.length) {
        channelOptions.updatedBefore = new Date(flattenedFilters.updatedDateRange[0].to);
        channelOptions.updatedAfter = new Date(flattenedFilters.updatedDateRange[0].from);
    }
    return channelOptions;
}
//# sourceMappingURL=processChannelFilters.js.map