"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PREDICATE_NON_MATCH_OPTIONS_PROPS = exports.PREDICATE_COPY_PROPS = exports.PREDICATE_DATE_PROPS = void 0;
exports.expandPredicate = expandPredicate;
const get_prop_1 = require("../../objects/get-prop");
const set_prop_1 = require("../../objects/set-prop");
const util_1 = require("../../util");
const valueToMatchOptions_1 = require("../valueToMatchOptions");
const relativeDateToDateRange_1 = require("../relativeDateToDateRange");
/**
 * @internal
 * Predicate properties that are treated as dates
 */
exports.PREDICATE_DATE_PROPS = ["created", "modified", "lastlogin"];
/**
 * @internal
 * Predicate properties that are just copied forward
 */
exports.PREDICATE_COPY_PROPS = [
    "bbox",
    "categoriesAsParam",
    "categoryFilter",
    "filterType",
    "isopendata",
    "isviewonly",
    "memberType",
    "name",
    "searchUserAccess",
    "searchUserName",
    "term",
    "openData", // boolean used by the OGC Search API to filter items
];
/**
 * @internal
 * Predicate properties that are not treated as match options
 */
exports.PREDICATE_NON_MATCH_OPTIONS_PROPS = [
    ...exports.PREDICATE_DATE_PROPS,
    ...exports.PREDICATE_COPY_PROPS,
];
/**
 * @private
 * Expand a predicate
 * @param predicate
 * @returns
 */
function expandPredicate(predicate) {
    const result = {};
    // Do the conversion
    Object.entries(predicate).forEach(([key, value]) => {
        // Handle MatchOptions fields
        if (!exports.PREDICATE_NON_MATCH_OPTIONS_PROPS.includes(key)) {
            (0, set_prop_1.setProp)(key, (0, valueToMatchOptions_1.valueToMatchOptions)(value), result);
        }
        // Handle Date fields
        if (exports.PREDICATE_DATE_PROPS.includes(key)) {
            const dateFieldValue = (0, util_1.cloneObject)((0, get_prop_1.getProp)(predicate, key));
            if ((0, get_prop_1.getProp)(predicate, `${key}.type`) === "relative-date") {
                (0, set_prop_1.setProp)(key, (0, relativeDateToDateRange_1.relativeDateToDateRange)(dateFieldValue), result);
            }
            else {
                (0, set_prop_1.setProp)(key, dateFieldValue, result);
            }
        }
        // Handle fields that are just copied forward
        if (exports.PREDICATE_COPY_PROPS.includes(key) &&
            Object.prototype.hasOwnProperty.call(predicate, key)) {
            (0, set_prop_1.setProp)(key, value, result);
        }
    });
    return result;
}
//# sourceMappingURL=expandPredicate.js.map