"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const core = require("zod/v4/core");
const getSchemaId = (id, io) => {
  return io === "input" ? `${id}Input` : id;
};
const getReferenceUri = (id, io) => {
  return `#/components/schemas/${getSchemaId(id, io)}`;
};
function isZodDate(entity) {
  return entity instanceof core.$ZodType && entity._zod.def.type === "date";
}
function isZodUnion(entity) {
  return entity instanceof core.$ZodType && entity._zod.def.type === "union";
}
function isZodUndefined(entity) {
  return entity instanceof core.$ZodType && entity._zod.def.type === "undefined";
}
const getOverride = (ctx, io) => {
  if (isZodUnion(ctx.zodSchema)) {
    ctx.jsonSchema.anyOf = ctx.jsonSchema.anyOf?.filter((schema) => Object.keys(schema).length > 0);
  }
  if (isZodDate(ctx.zodSchema)) {
    if (io === "output") {
      ctx.jsonSchema.type = "string";
      ctx.jsonSchema.format = "date-time";
    }
  }
  if (isZodUndefined(ctx.zodSchema)) {
    if (io === "output") {
      ctx.jsonSchema.type = "null";
    }
  }
};
const zodSchemaToJson = (zodSchema, registry, io, config = {}) => {
  const { target = "draft-2020-12" } = config;
  const schemaRegistryEntry = registry.get(zodSchema);
  if (schemaRegistryEntry?.id) {
    return {
      $ref: getReferenceUri(schemaRegistryEntry.id, io)
    };
  }
  const tempID = "GEN";
  const tempRegistry = new core.$ZodRegistry();
  tempRegistry.add(zodSchema, { id: tempID });
  const {
    schemas: { [tempID]: result }
  } = core.toJSONSchema(tempRegistry, {
    target,
    metadata: registry,
    io,
    unrepresentable: "any",
    cycles: "ref",
    reused: "inline",
    /**
     * The uri option only allows customizing the base path of the `$ref`, and it automatically appends a path to it.
     * As a workaround, we set a placeholder that looks something like this:
     *
     * |       marker          | always added by zod | meta.id |
     * |__SCHEMA__PLACEHOLDER__|      #/$defs/       | User    |
     *
     * @example `__SCHEMA__PLACEHOLDER__#/$defs/User"`
     * @example `__SCHEMA__PLACEHOLDER__#/$defs/Group"`
     *
     * @see jsonSchemaReplaceRef
     * @see https://github.com/colinhacks/zod/issues/4750
     */
    uri: () => `__SCHEMA__PLACEHOLDER__`,
    override: (ctx) => getOverride(ctx, io)
  });
  const jsonSchema = { ...result };
  delete jsonSchema.id;
  const jsonSchemaReplaceRef = JSON.stringify(jsonSchema).replaceAll(
    /"__SCHEMA__PLACEHOLDER__#\/\$defs\/(.+?)"/g,
    (_, id) => `"${getReferenceUri(id, io)}"`
  );
  return JSON.parse(jsonSchemaReplaceRef);
};
const zodRegistryToJson = (registry, io, config = {}) => {
  const { target = "draft-2020-12" } = config;
  const result = core.toJSONSchema(registry, {
    target,
    io,
    unrepresentable: "any",
    cycles: "ref",
    reused: "inline",
    uri: (id) => getReferenceUri(id, io),
    override: (ctx) => getOverride(ctx, io)
  }).schemas;
  const jsonSchemas = {};
  for (const id in result) {
    const jsonSchema = { ...result[id] };
    delete jsonSchema.id;
    jsonSchemas[getSchemaId(id, io)] = jsonSchema;
  }
  return jsonSchemas;
};
exports.zodRegistryToJson = zodRegistryToJson;
exports.zodSchemaToJson = zodSchemaToJson;
//# sourceMappingURL=zod-to-json.cjs.map
