"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.coerceObject = coerceObject;
exports.coerceIfBoolean = coerceIfBoolean;
exports.coerceIfFalse = coerceIfFalse;
exports.unwrapType = unwrapType;
const v4_1 = __importDefault(require("zod/v4"));
function coerceObject(schema) {
    return (value) => {
        switch (typeof value) {
            case 'string':
                return schema.parse(JSON.parse(value));
            case 'object':
                return value;
            default:
                return null;
        }
    };
}
function coerceIfBoolean(value) {
    if (typeof value === 'string') {
        if (value === 'true') {
            return true;
        }
        if (value === 'false') {
            return false;
        }
        return value;
    }
    return value;
}
function coerceIfFalse(value) {
    if (value === undefined || value === '') {
        return null;
    }
    if (typeof value === 'string') {
        if (value === 'false') {
            return false;
        }
        return value;
    }
    return value;
}
function unwrapType(type) {
    assertZodType(type);
    let unwrappedType = v4_1.default.clone(type);
    while (unwrappedType instanceof v4_1.default.ZodOptional ||
        unwrappedType instanceof v4_1.default.ZodDefault ||
        unwrappedType instanceof v4_1.default.ZodNullable ||
        unwrappedType instanceof v4_1.default.ZodPipe) {
        if (unwrappedType instanceof v4_1.default.ZodPipe) {
            const nextWrap = unwrappedType.def.out;
            assertZodType(nextWrap);
            unwrappedType = nextWrap;
        }
        else {
            const nextWrap = unwrappedType.unwrap();
            assertZodType(nextWrap);
            unwrappedType = nextWrap;
        }
    }
    return unwrappedType;
}
function assertZodType(type) {
    if (!(type instanceof v4_1.default.ZodType)) {
        throw new Error(`Unknown schema field type: ${type && typeof type === 'object' ? type.constructor.name : typeof type}`);
    }
}
//# sourceMappingURL=utils.js.map