const filterNullishValues = value => value !== undefined && value !== null;
export function removeFieldsWithNullishValues(object) {
    return filterValue(object, filterNullishValues);
}
function filterValue(object, filterValue = filterNullishValues) {
    const clone = {};
    for (const [key, value] of Object.entries(object)) {
        if (filterValue(value)) {
            if (typeof value === 'object') {
                if (Array.isArray(value)) {
                    clone[key] = value.filter(filterValue);
                }
                else {
                    clone[key] = removeFieldsWithNullishValues(value);
                }
            }
            else {
                clone[key] = value;
            }
        }
    }
    return clone;
}
export const pickFields = (source, fields) => Object.fromEntries(fields.map(field => [field, source[field]]));
export const mutateData = (context, ...mutations) => {
    for (const mutation of mutations) {
        const override = mutation.__override__;
        for (const [key, value] of Object.entries(mutation).filter(([key]) => key !== '__override__')) {
            if (typeof value === 'function') {
                if (override !== false || context[key] === undefined) {
                    context[key] = value(context);
                }
            }
            else if (context[key] === undefined || override === true) {
                context[key] = value;
            }
        }
    }
};
