"use strict";
/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.string = string;
exports.boolean = boolean;
exports.number = number;
exports.bigint = bigint;
exports.date = date;
exports.literal = literal;
exports.literalBigInt = literalBigInt;
exports.optional = optional;
exports.nullable = nullable;
const z = __importStar(require("zod/v3"));
const defaultToZeroValue_js_1 = require("./defaultToZeroValue.js");
const unrecognized_js_1 = require("./unrecognized.js");
function string() {
    return z.union([
        z.string(),
        // Null or undefined -> ""
        zodDefaultToZeroValue(""),
        // Any other value -> String(x)
        z.any().transform((x) => (0, unrecognized_js_1.unrecognized)(JSON.stringify(x))),
    ]);
}
function boolean() {
    return z.union([
        z.boolean(),
        // String "true" (case insensitive) -> true, "false" -> false
        z.string().transform((x, ctx) => {
            const lower = x.toLowerCase();
            if (lower === "true")
                return (0, unrecognized_js_1.unrecognized)(true);
            if (lower === "false")
                return (0, unrecognized_js_1.unrecognized)(false);
            ctx.addIssue({
                code: "invalid_type",
                expected: "boolean",
                received: "string",
            });
            return z.NEVER;
        }),
        zodDefaultToZeroValue(false),
    ]);
}
function number() {
    return z.union([
        z.number(),
        // String -> Number
        z.string().transform((x, ctx) => {
            const num = Number(x);
            if (isNaN(num)) {
                ctx.addIssue({
                    code: "invalid_type",
                    expected: "number",
                    received: "string",
                });
                return z.NEVER;
            }
            return (0, unrecognized_js_1.unrecognized)(num);
        }),
        // Null or undefined -> 0
        zodDefaultToZeroValue(0),
    ]);
}
function bigint() {
    return z.union([
        z.string().transform((x, ctx) => {
            try {
                return BigInt(x);
            }
            catch (error) {
                ctx.addIssue({
                    code: "invalid_type",
                    expected: "bigint",
                    received: "string",
                });
                return z.NEVER;
            }
        }),
        zodDefaultToZeroValue(0n),
    ]);
}
function date() {
    return z.union([
        z.union([z.string(), zodDefaultToZeroValue(0)]).transform((x) => new Date(x)).pipe(z.date()),
        z.number().transform((x, ctx) => {
            const date = new Date(x);
            if (isNaN(date.getTime())) {
                ctx.addIssue({
                    code: "invalid_type",
                    expected: "date",
                    received: "number",
                });
                return z.NEVER;
            }
            return (0, unrecognized_js_1.unrecognized)(date);
        }),
    ]);
}
function literal(value) {
    return z.union([z.literal(value), zodDefaultToZeroValue(value)]);
}
function literalBigInt(value) {
    return z.literal(String(value)).transform((x) => BigInt(x));
}
function optional(t) {
    return z.union([
        z.undefined(),
        // Null -> undefined
        z.null().transform(() => (0, unrecognized_js_1.unrecognized)(undefined)),
        t,
    ]);
}
function nullable(t) {
    return z.union([
        z.null(),
        // Undefined -> null
        z.undefined().transform(() => (0, defaultToZeroValue_js_1.defaultToZeroValue)(null)),
        t,
    ]);
}
function zodDefaultToZeroValue(value) {
    return z.any().transform((input, ctx) => {
        if (input === undefined)
            return (0, defaultToZeroValue_js_1.defaultToZeroValue)(value);
        if (input === null)
            return (0, defaultToZeroValue_js_1.defaultToZeroValue)(value);
        ctx.addIssue({
            code: "invalid_type",
            expected: "undefined",
            received: "unknown",
        });
        return z.NEVER;
    });
}
//# sourceMappingURL=primitives.js.map