import { createRequire } from "node:module";
var __defProp = Object.defineProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: (newValue) => all[name] = () => newValue
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __require = /* @__PURE__ */ createRequire(import.meta.url);

// src/schema/json-schema-to-zod.ts
import { z } from "zod";
function jsonSchemaToZod(schema) {
  const s = schema;
  const makeNullable = (zodSchema) => {
    return s.nullable ? z.union([zodSchema, z.null()]) : zodSchema;
  };
  if (s.const !== undefined) {
    return z.literal(s.const);
  }
  if (s.enum) {
    const values = s.enum;
    return makeNullable(z.enum(values.map(String)));
  }
  if (s.anyOf && s.anyOf.length > 0) {
    const schemas = s.anyOf.map((sub) => jsonSchemaToZod(sub));
    if (schemas.length === 1)
      return schemas[0] ?? z.unknown();
    return z.union([
      schemas[0] ?? z.unknown(),
      schemas[1] ?? z.unknown(),
      ...schemas.slice(2)
    ]);
  }
  if (s.oneOf && s.oneOf.length > 0) {
    const schemas = s.oneOf.map((sub) => jsonSchemaToZod(sub));
    if (schemas.length === 1)
      return schemas[0] ?? z.unknown();
    return z.union([
      schemas[0] ?? z.unknown(),
      schemas[1] ?? z.unknown(),
      ...schemas.slice(2)
    ]);
  }
  if (s.allOf && s.allOf.length > 0) {
    const schemas = s.allOf.map((sub) => jsonSchemaToZod(sub));
    return schemas.reduce((acc, curr) => z.intersection(acc, curr));
  }
  switch (s.type) {
    case "string":
      return makeNullable(buildStringSchema(s));
    case "number":
    case "integer":
      return makeNullable(buildNumberSchema(s));
    case "boolean":
      return makeNullable(z.boolean());
    case "null":
      return z.null();
    case "array":
      return makeNullable(buildArraySchema(s));
    case "object":
      return makeNullable(buildObjectSchema(s));
    default:
      return z.unknown();
  }
}
function buildStringSchema(schema) {
  let zodSchema = z.string();
  if (schema.description) {
    zodSchema = zodSchema.describe(schema.description);
  }
  switch (schema.format) {
    case "email":
      zodSchema = zodSchema.email();
      break;
    case "uri":
    case "url":
      zodSchema = zodSchema.url();
      break;
    case "uuid":
      zodSchema = zodSchema.uuid();
      break;
    case "date-time":
      zodSchema = zodSchema.datetime();
      break;
    case "date":
      zodSchema = zodSchema.date();
      break;
  }
  if (schema.minLength !== undefined) {
    zodSchema = zodSchema.min(schema.minLength);
  }
  if (schema.maxLength !== undefined) {
    zodSchema = zodSchema.max(schema.maxLength);
  }
  if (schema.pattern) {
    zodSchema = zodSchema.regex(new RegExp(schema.pattern));
  }
  return zodSchema;
}
function buildNumberSchema(schema) {
  let zodSchema = schema.type === "integer" ? z.number().int() : z.number();
  if (schema.description) {
    zodSchema = zodSchema.describe(schema.description);
  }
  if (schema.minimum !== undefined) {
    zodSchema = zodSchema.min(schema.minimum);
  }
  if (schema.maximum !== undefined) {
    zodSchema = zodSchema.max(schema.maximum);
  }
  return zodSchema;
}
function buildArraySchema(schema) {
  const itemsSchema = schema.items ? jsonSchemaToZod(schema.items) : z.unknown();
  let zodSchema = z.array(itemsSchema);
  if (schema.description) {
    zodSchema = zodSchema.describe(schema.description);
  }
  return zodSchema;
}
function buildObjectSchema(schema) {
  const properties = schema.properties ?? {};
  const required = new Set(schema.required ?? []);
  const shape = {};
  for (const [key, propSchema] of Object.entries(properties)) {
    const zodProp = jsonSchemaToZod(propSchema);
    shape[key] = required.has(key) ? zodProp : zodProp.optional();
  }
  let zodSchema = z.object(shape);
  if (schema.description) {
    zodSchema = zodSchema.describe(schema.description);
  }
  return zodSchema;
}
function jsonSchemaToZodSafe(schema) {
  if (!schema || Object.keys(schema).length === 0) {
    return z.object({});
  }
  return jsonSchemaToZod(schema);
}
var init_json_schema_to_zod = () => {};

// src/schema/schema-output.ts
init_json_schema_to_zod();
import { Output } from "ai";
function jsonSchemaToOutput(schema) {
  const zodSchema = jsonSchemaToZod(schema);
  return Output.object({
    schema: zodSchema
  });
}
function jsonSchemaToArrayOutput(itemSchema) {
  const zodSchema = jsonSchemaToZod(itemSchema);
  return Output.array({
    element: zodSchema
  });
}
function enumToChoiceOutput(choices) {
  return Output.choice({
    options: choices
  });
}
function zodToOutput(schema) {
  return Output.object({
    schema
  });
}
function textOutput() {
  return Output.text();
}
var SchemaOutput = {
  fromJsonSchema: jsonSchemaToOutput,
  arrayFromJsonSchema: jsonSchemaToArrayOutput,
  fromEnum: enumToChoiceOutput,
  fromZod: zodToOutput,
  text: textOutput
};

// src/schema/index.ts
init_json_schema_to_zod();
export {
  zodToOutput,
  textOutput,
  jsonSchemaToZodSafe,
  jsonSchemaToZod,
  jsonSchemaToOutput,
  jsonSchemaToArrayOutput,
  enumToChoiceOutput,
  SchemaOutput
};
