import{F as e}from"./DB5ifLM-.js";import{o as t,p as n}from"./CMdgFTSd.js";var r=new Set([`date-time`,`time`,`date`,`duration`,`email`,`hostname`,`uri`,`ipv4`,`ipv6`,`uuid`]);function i(e){return JSON.parse(JSON.stringify(e))}function a(e){return o(i(e))}function o(e){let n={},i=t(e,`$ref`);if(i!==void 0)return n.$ref=i,n;let a=t(e,`$defs`);if(a!==void 0){let e={};n.$defs=e;for(let[t,n]of Object.entries(a))e[t]=o(n)}let s=t(e,`type`),c=t(e,`anyOf`),l=t(e,`oneOf`),u=t(e,`allOf`);if(Array.isArray(c))n.anyOf=c.map(e=>o(e));else if(Array.isArray(l))n.anyOf=l.map(e=>o(e));else if(Array.isArray(u))n.allOf=u.map(e=>o(e));else{if(s===void 0)throw Error(`JSON schema must have a type defined if anyOf/oneOf/allOf are not used`);n.type=s}let d=t(e,`description`);d!==void 0&&(n.description=d);let f=t(e,`title`);if(f!==void 0&&(n.title=f),s===`object`){let r=t(e,`properties`)||{};n.properties=Object.fromEntries(Object.entries(r).map(([e,t])=>[e,o(t)])),t(e,`additionalProperties`),n.additionalProperties=!1;let i=t(e,`required`);i!==void 0&&(n.required=i)}else if(s===`string`){let i=t(e,`format`);i!==void 0&&r.has(i)?n.format=i:i!==void 0&&(e.format=i)}else if(s===`array`){let r=t(e,`items`);r!==void 0&&(n.items=o(r));let i=t(e,`minItems`);i!==void 0&&(i===0||i===1)?n.minItems=i:i!==void 0&&(e.minItems=i)}if(Object.keys(e).length>0){let t=n.description;n.description=(t?t+`

`:``)+`{`+Object.entries(e).map(([e,t])=>`${e}: ${JSON.stringify(t)}`).join(`, `)+`}`}return n}function s(t){let r=e(t,{reused:`ref`});return r=a(r),{type:`json_schema`,schema:{...r},parse:e=>{let r;try{r=JSON.parse(e)}catch(e){throw new n(`Failed to parse structured output as JSON: ${e instanceof Error?e.message:String(e)}`)}let i=t.safeParse(r);if(!i.success){let e=i.error.issues.slice(0,5).map(e=>`  - ${e.path.join(`.`)}: ${e.message}`).join(`
`),t=i.error.issues.length,r=t>5?`\n  ... and ${t-5} more issue(s)`:``;throw new n(`Failed to parse structured output: ${i.error.message}\nValidation issues:\n${e}${r}`)}return i.data}}}export{s as zodOutputFormat};