"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.looseValue = exports.looseRecord = exports.looseArray = void 0;
const zod_1 = require("zod");
function looseArray(schema, catchCallback) {
    const nullableSchema = schema.nullable().catch(catchCallback
        ? () => {
            catchCallback();
            return null;
        }
        : null);
    const arrayOfNullables = zod_1.z.array(nullableSchema);
    const arrayWithFallback = catchCallback
        ? arrayOfNullables.catch(() => {
            catchCallback();
            return [];
        })
        : arrayOfNullables.catch([]);
    const filteredArray = arrayWithFallback.transform((xs) => xs.filter((x) => x !== null));
    return filteredArray;
}
exports.looseArray = looseArray;
function looseRecord(schema, catchCallback) {
    const nullableSchema = schema.nullable().catch(catchCallback
        ? () => {
            catchCallback();
            return null;
        }
        : null);
    const recordOfNullables = zod_1.z.record(nullableSchema);
    const recordWithFallback = catchCallback
        ? recordOfNullables.catch(() => {
            catchCallback();
            return {};
        })
        : recordOfNullables.catch({});
    const filteredRecord = recordWithFallback.transform((rec) => {
        for (const key of Object.keys(rec)) {
            if (rec[key] === null) {
                delete rec[key];
            }
        }
        return rec;
    });
    return filteredRecord;
}
exports.looseRecord = looseRecord;
function looseValue(schema, catchCallback) {
    const nullableSchema = schema.nullable();
    const schemaWithFallback = catchCallback
        ? nullableSchema.catch(() => {
            catchCallback();
            return null;
        })
        : nullableSchema.catch(null);
    return schemaWithFallback;
}
exports.looseValue = looseValue;
//# sourceMappingURL=schema-utils.js.map