// (C) 2022-2026 GoodData Corporation
import { compact, isEmpty } from "lodash-es";
function isUseless(obj) {
    return obj === null || obj === undefined || (typeof obj === "object" && isEmpty(obj));
}
/**
 * For a given object returns a copy that has all properties that are empty objects or arrays or nil removed.
 * This is applied recursively.
 *
 * @param obj - object to handle
 * @returns always a new instance if obj is not scalar
 */
export function removeUseless(obj) {
    let res = obj;
    if (Array.isArray(obj)) {
        res = compact(obj.map(removeUseless));
    }
    else if (obj !== null && typeof obj === "object") {
        res = Object.fromEntries(Object.entries(obj)
            .map(([key, value]) => [key, removeUseless(value)])
            .filter(([_, value]) => !isUseless(value)));
    }
    return isUseless(res) ? undefined : res;
}
//# sourceMappingURL=removeUseless.js.map