'use strict';

var core = require('../core/core.js');
var util = require('../core/util.js');
var schemas = require('../core/schemas.js');
var registries = require('../core/registries.js');
var api = require('../core/api.js');
var iso = require('./iso.js');
var parse = require('./parse.js');

const ZodType = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) => {
    schemas.$ZodType.init(inst, def);
    inst.def = def;
    inst.type = def.type;
    Object.defineProperty(inst, "_def", { value: def });
    // base methods
    inst.check = (...checks) => {
        return inst.clone(util.mergeDefs(def, {
            checks: [
                ...(def.checks ?? []),
                ...checks.map((ch) => typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch),
            ],
        }));
    };
    inst.clone = (def, params) => util.clone(inst, def, params);
    inst.brand = () => inst;
    inst.register = ((reg, meta) => {
        reg.add(inst, meta);
        return inst;
    });
    // parsing
    inst.parse = (data, params) => parse.parse(inst, data, params, { callee: inst.parse });
    inst.safeParse = (data, params) => parse.safeParse(inst, data, params);
    inst.parseAsync = async (data, params) => parse.parseAsync(inst, data, params, { callee: inst.parseAsync });
    inst.safeParseAsync = async (data, params) => parse.safeParseAsync(inst, data, params);
    inst.spa = inst.safeParseAsync;
    // encoding/decoding
    inst.encode = (data, params) => parse.encode(inst, data, params);
    inst.decode = (data, params) => parse.decode(inst, data, params);
    inst.encodeAsync = async (data, params) => parse.encodeAsync(inst, data, params);
    inst.decodeAsync = async (data, params) => parse.decodeAsync(inst, data, params);
    inst.safeEncode = (data, params) => parse.safeEncode(inst, data, params);
    inst.safeDecode = (data, params) => parse.safeDecode(inst, data, params);
    inst.safeEncodeAsync = async (data, params) => parse.safeEncodeAsync(inst, data, params);
    inst.safeDecodeAsync = async (data, params) => parse.safeDecodeAsync(inst, data, params);
    // refinements
    inst.refine = (check, params) => inst.check(refine(check, params));
    inst.superRefine = (refinement) => inst.check(superRefine(refinement));
    inst.overwrite = (fn) => inst.check(api._overwrite(fn));
    // wrappers
    inst.optional = () => optional(inst);
    inst.nullable = () => nullable(inst);
    inst.nullish = () => optional(nullable(inst));
    inst.nonoptional = (params) => nonoptional(inst, params);
    inst.array = () => array(inst);
    inst.or = (arg) => union([inst, arg]);
    inst.and = (arg) => intersection(inst, arg);
    inst.transform = (tx) => pipe(inst, transform(tx));
    inst.default = (def) => _default(inst, def);
    inst.prefault = (def) => prefault(inst, def);
    // inst.coalesce = (def, params) => coalesce(inst, def, params);
    inst.catch = (params) => _catch(inst, params);
    inst.pipe = (target) => pipe(inst, target);
    inst.readonly = () => readonly(inst);
    // meta
    inst.describe = (description) => {
        const cl = inst.clone();
        registries.globalRegistry.add(cl, { description });
        return cl;
    };
    Object.defineProperty(inst, "description", {
        get() {
            return registries.globalRegistry.get(inst)?.description;
        },
        configurable: true,
    });
    inst.meta = (...args) => {
        if (args.length === 0) {
            return registries.globalRegistry.get(inst);
        }
        const cl = inst.clone();
        registries.globalRegistry.add(cl, args[0]);
        return cl;
    };
    // helpers
    inst.isOptional = () => inst.safeParse(undefined).success;
    inst.isNullable = () => inst.safeParse(null).success;
    return inst;
});
/** @internal */
const _ZodString = /*@__PURE__*/ core.$constructor("_ZodString", (inst, def) => {
    schemas.$ZodString.init(inst, def);
    ZodType.init(inst, def);
    const bag = inst._zod.bag;
    inst.format = bag.format ?? null;
    inst.minLength = bag.minimum ?? null;
    inst.maxLength = bag.maximum ?? null;
    // validations
    inst.regex = (...args) => inst.check(api._regex(...args));
    inst.includes = (...args) => inst.check(api._includes(...args));
    inst.startsWith = (...args) => inst.check(api._startsWith(...args));
    inst.endsWith = (...args) => inst.check(api._endsWith(...args));
    inst.min = (...args) => inst.check(api._minLength(...args));
    inst.max = (...args) => inst.check(api._maxLength(...args));
    inst.length = (...args) => inst.check(api._length(...args));
    inst.nonempty = (...args) => inst.check(api._minLength(1, ...args));
    inst.lowercase = (params) => inst.check(api._lowercase(params));
    inst.uppercase = (params) => inst.check(api._uppercase(params));
    // transforms
    inst.trim = () => inst.check(api._trim());
    inst.normalize = (...args) => inst.check(api._normalize(...args));
    inst.toLowerCase = () => inst.check(api._toLowerCase());
    inst.toUpperCase = () => inst.check(api._toUpperCase());
});
const ZodString = /*@__PURE__*/ core.$constructor("ZodString", (inst, def) => {
    schemas.$ZodString.init(inst, def);
    _ZodString.init(inst, def);
    inst.email = (params) => inst.check(api._email(ZodEmail, params));
    inst.url = (params) => inst.check(api._url(ZodURL, params));
    inst.jwt = (params) => inst.check(api._jwt(ZodJWT, params));
    inst.emoji = (params) => inst.check(api._emoji(ZodEmoji, params));
    inst.guid = (params) => inst.check(api._guid(ZodGUID, params));
    inst.uuid = (params) => inst.check(api._uuid(ZodUUID, params));
    inst.uuidv4 = (params) => inst.check(api._uuidv4(ZodUUID, params));
    inst.uuidv6 = (params) => inst.check(api._uuidv6(ZodUUID, params));
    inst.uuidv7 = (params) => inst.check(api._uuidv7(ZodUUID, params));
    inst.nanoid = (params) => inst.check(api._nanoid(ZodNanoID, params));
    inst.guid = (params) => inst.check(api._guid(ZodGUID, params));
    inst.cuid = (params) => inst.check(api._cuid(ZodCUID, params));
    inst.cuid2 = (params) => inst.check(api._cuid2(ZodCUID2, params));
    inst.ulid = (params) => inst.check(api._ulid(ZodULID, params));
    inst.base64 = (params) => inst.check(api._base64(ZodBase64, params));
    inst.base64url = (params) => inst.check(api._base64url(ZodBase64URL, params));
    inst.xid = (params) => inst.check(api._xid(ZodXID, params));
    inst.ksuid = (params) => inst.check(api._ksuid(ZodKSUID, params));
    inst.ipv4 = (params) => inst.check(api._ipv4(ZodIPv4, params));
    inst.ipv6 = (params) => inst.check(api._ipv6(ZodIPv6, params));
    inst.cidrv4 = (params) => inst.check(api._cidrv4(ZodCIDRv4, params));
    inst.cidrv6 = (params) => inst.check(api._cidrv6(ZodCIDRv6, params));
    inst.e164 = (params) => inst.check(api._e164(ZodE164, params));
    // iso
    inst.datetime = (params) => inst.check(iso.datetime(params));
    inst.date = (params) => inst.check(iso.date(params));
    inst.time = (params) => inst.check(iso.time(params));
    inst.duration = (params) => inst.check(iso.duration(params));
});
function string(params) {
    return api._string(ZodString, params);
}
const ZodStringFormat = /*@__PURE__*/ core.$constructor("ZodStringFormat", (inst, def) => {
    schemas.$ZodStringFormat.init(inst, def);
    _ZodString.init(inst, def);
});
const ZodEmail = /*@__PURE__*/ core.$constructor("ZodEmail", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodEmail.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodGUID = /*@__PURE__*/ core.$constructor("ZodGUID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodGUID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodUUID = /*@__PURE__*/ core.$constructor("ZodUUID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodUUID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodURL = /*@__PURE__*/ core.$constructor("ZodURL", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodURL.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodEmoji = /*@__PURE__*/ core.$constructor("ZodEmoji", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodEmoji.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodNanoID = /*@__PURE__*/ core.$constructor("ZodNanoID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodNanoID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodCUID = /*@__PURE__*/ core.$constructor("ZodCUID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodCUID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodCUID2 = /*@__PURE__*/ core.$constructor("ZodCUID2", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodCUID2.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodULID = /*@__PURE__*/ core.$constructor("ZodULID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodULID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodXID = /*@__PURE__*/ core.$constructor("ZodXID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodXID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodKSUID = /*@__PURE__*/ core.$constructor("ZodKSUID", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodKSUID.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodIPv4 = /*@__PURE__*/ core.$constructor("ZodIPv4", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodIPv4.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodIPv6 = /*@__PURE__*/ core.$constructor("ZodIPv6", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodIPv6.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodCIDRv4 = /*@__PURE__*/ core.$constructor("ZodCIDRv4", (inst, def) => {
    schemas.$ZodCIDRv4.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodCIDRv6 = /*@__PURE__*/ core.$constructor("ZodCIDRv6", (inst, def) => {
    schemas.$ZodCIDRv6.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodBase64 = /*@__PURE__*/ core.$constructor("ZodBase64", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodBase64.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodBase64URL = /*@__PURE__*/ core.$constructor("ZodBase64URL", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodBase64URL.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodE164 = /*@__PURE__*/ core.$constructor("ZodE164", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodE164.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodJWT = /*@__PURE__*/ core.$constructor("ZodJWT", (inst, def) => {
    // ZodStringFormat.init(inst, def);
    schemas.$ZodJWT.init(inst, def);
    ZodStringFormat.init(inst, def);
});
const ZodArray = /*@__PURE__*/ core.$constructor("ZodArray", (inst, def) => {
    schemas.$ZodArray.init(inst, def);
    ZodType.init(inst, def);
    inst.element = def.element;
    inst.min = (minLength, params) => inst.check(api._minLength(minLength, params));
    inst.nonempty = (params) => inst.check(api._minLength(1, params));
    inst.max = (maxLength, params) => inst.check(api._maxLength(maxLength, params));
    inst.length = (len, params) => inst.check(api._length(len, params));
    inst.unwrap = () => inst.element;
});
function array(element, params) {
    return api._array(ZodArray, element, params);
}
const ZodUnion = /*@__PURE__*/ core.$constructor("ZodUnion", (inst, def) => {
    schemas.$ZodUnion.init(inst, def);
    ZodType.init(inst, def);
    inst.options = def.options;
});
function union(options, params) {
    return new ZodUnion({
        type: "union",
        options: options,
        ...util.normalizeParams(params),
    });
}
const ZodIntersection = /*@__PURE__*/ core.$constructor("ZodIntersection", (inst, def) => {
    schemas.$ZodIntersection.init(inst, def);
    ZodType.init(inst, def);
});
function intersection(left, right) {
    return new ZodIntersection({
        type: "intersection",
        left: left,
        right: right,
    });
}
const ZodTransform = /*@__PURE__*/ core.$constructor("ZodTransform", (inst, def) => {
    schemas.$ZodTransform.init(inst, def);
    ZodType.init(inst, def);
    inst._zod.parse = (payload, _ctx) => {
        if (_ctx.direction === "backward") {
            throw new core.$ZodEncodeError(inst.constructor.name);
        }
        payload.addIssue = (issue) => {
            if (typeof issue === "string") {
                payload.issues.push(util.issue(issue, payload.value, def));
            }
            else {
                // for Zod 3 backwards compatibility
                const _issue = issue;
                if (_issue.fatal)
                    _issue.continue = false;
                _issue.code ?? (_issue.code = "custom");
                _issue.input ?? (_issue.input = payload.value);
                _issue.inst ?? (_issue.inst = inst);
                // _issue.continue ??= true;
                payload.issues.push(util.issue(_issue));
            }
        };
        const output = def.transform(payload.value, payload);
        if (output instanceof Promise) {
            return output.then((output) => {
                payload.value = output;
                return payload;
            });
        }
        payload.value = output;
        return payload;
    };
});
function transform(fn) {
    return new ZodTransform({
        type: "transform",
        transform: fn,
    });
}
const ZodOptional = /*@__PURE__*/ core.$constructor("ZodOptional", (inst, def) => {
    schemas.$ZodOptional.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
});
function optional(innerType) {
    return new ZodOptional({
        type: "optional",
        innerType: innerType,
    });
}
const ZodNullable = /*@__PURE__*/ core.$constructor("ZodNullable", (inst, def) => {
    schemas.$ZodNullable.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
});
function nullable(innerType) {
    return new ZodNullable({
        type: "nullable",
        innerType: innerType,
    });
}
const ZodDefault = /*@__PURE__*/ core.$constructor("ZodDefault", (inst, def) => {
    schemas.$ZodDefault.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
    inst.removeDefault = inst.unwrap;
});
function _default(innerType, defaultValue) {
    return new ZodDefault({
        type: "default",
        innerType: innerType,
        get defaultValue() {
            return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
        },
    });
}
const ZodPrefault = /*@__PURE__*/ core.$constructor("ZodPrefault", (inst, def) => {
    schemas.$ZodPrefault.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
});
function prefault(innerType, defaultValue) {
    return new ZodPrefault({
        type: "prefault",
        innerType: innerType,
        get defaultValue() {
            return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
        },
    });
}
const ZodNonOptional = /*@__PURE__*/ core.$constructor("ZodNonOptional", (inst, def) => {
    schemas.$ZodNonOptional.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
});
function nonoptional(innerType, params) {
    return new ZodNonOptional({
        type: "nonoptional",
        innerType: innerType,
        ...util.normalizeParams(params),
    });
}
const ZodCatch = /*@__PURE__*/ core.$constructor("ZodCatch", (inst, def) => {
    schemas.$ZodCatch.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
    inst.removeCatch = inst.unwrap;
});
function _catch(innerType, catchValue) {
    return new ZodCatch({
        type: "catch",
        innerType: innerType,
        catchValue: (typeof catchValue === "function" ? catchValue : () => catchValue),
    });
}
const ZodPipe = /*@__PURE__*/ core.$constructor("ZodPipe", (inst, def) => {
    schemas.$ZodPipe.init(inst, def);
    ZodType.init(inst, def);
    inst.in = def.in;
    inst.out = def.out;
});
function pipe(in_, out) {
    return new ZodPipe({
        type: "pipe",
        in: in_,
        out: out,
        // ...util.normalizeParams(params),
    });
}
const ZodReadonly = /*@__PURE__*/ core.$constructor("ZodReadonly", (inst, def) => {
    schemas.$ZodReadonly.init(inst, def);
    ZodType.init(inst, def);
    inst.unwrap = () => inst._zod.def.innerType;
});
function readonly(innerType) {
    return new ZodReadonly({
        type: "readonly",
        innerType: innerType,
    });
}
const ZodCustom = /*@__PURE__*/ core.$constructor("ZodCustom", (inst, def) => {
    schemas.$ZodCustom.init(inst, def);
    ZodType.init(inst, def);
});
function refine(fn, _params = {}) {
    return api._refine(ZodCustom, fn, _params);
}
// superRefine
function superRefine(fn) {
    return api._superRefine(fn);
}
// preprocess
// /** @deprecated Use `z.pipe()` and `z.transform()` instead. */
function preprocess(fn, schema) {
    return pipe(transform(fn), schema);
}

exports.ZodArray = ZodArray;
exports.ZodBase64 = ZodBase64;
exports.ZodBase64URL = ZodBase64URL;
exports.ZodCIDRv4 = ZodCIDRv4;
exports.ZodCIDRv6 = ZodCIDRv6;
exports.ZodCUID = ZodCUID;
exports.ZodCUID2 = ZodCUID2;
exports.ZodCatch = ZodCatch;
exports.ZodCustom = ZodCustom;
exports.ZodDefault = ZodDefault;
exports.ZodE164 = ZodE164;
exports.ZodEmail = ZodEmail;
exports.ZodEmoji = ZodEmoji;
exports.ZodGUID = ZodGUID;
exports.ZodIPv4 = ZodIPv4;
exports.ZodIPv6 = ZodIPv6;
exports.ZodIntersection = ZodIntersection;
exports.ZodJWT = ZodJWT;
exports.ZodKSUID = ZodKSUID;
exports.ZodNanoID = ZodNanoID;
exports.ZodNonOptional = ZodNonOptional;
exports.ZodNullable = ZodNullable;
exports.ZodOptional = ZodOptional;
exports.ZodPipe = ZodPipe;
exports.ZodPrefault = ZodPrefault;
exports.ZodReadonly = ZodReadonly;
exports.ZodString = ZodString;
exports.ZodStringFormat = ZodStringFormat;
exports.ZodTransform = ZodTransform;
exports.ZodType = ZodType;
exports.ZodULID = ZodULID;
exports.ZodURL = ZodURL;
exports.ZodUUID = ZodUUID;
exports.ZodUnion = ZodUnion;
exports.ZodXID = ZodXID;
exports._ZodString = _ZodString;
exports._default = _default;
exports.array = array;
exports.catch = _catch;
exports.intersection = intersection;
exports.nonoptional = nonoptional;
exports.nullable = nullable;
exports.optional = optional;
exports.pipe = pipe;
exports.prefault = prefault;
exports.preprocess = preprocess;
exports.readonly = readonly;
exports.refine = refine;
exports.string = string;
exports.superRefine = superRefine;
exports.transform = transform;
exports.union = union;
