import{JsonPointer as w}from"@redocly/openapi-docs";import{isObject as d}from"../../../../utils/guards/is-object.js";import{getNodeAttribute as u}from"../../../../markdoc/helpers/get-node-attribute.js";import{setNodeAttributeValue as s}from"../../../../markdoc/helpers/set-node-attribute-value.js";import{MdResolveError as p}from"./md-resolve-error.js";import{resolvePathInMarkdown as $}from"../resolve-path-in-markdown.js";async function b(e,o,m,{context:n,actions:h}){const r=u(e,o);try{if(!d(r)||typeof r.$ref!="string"){try{JSON.stringify(r)}catch(i){throw new Error(`Invalid JSON value: ${i.message}`)}s(e,o+"Resolved",r);return}const[t,f]=r.$ref.split("#");if(!t)throw new Error(`Invalid $ref. Path to file is required: ${r.$ref}`);const l=$(t,m,h.contentDir),c=await n.fs.getFileInfo(l);if(!c)throw new Error(`Invalid $ref. No such file: ${l}`);let a;try{a=(await n.cache.load(c.realRelativePath,"yaml")).data}catch(i){throw new Error(`Invalid file format. File must be valid json or yaml: ${l} ${i?.message||""}`)}if(!f){s(e,o+"Resolved",a);return}const v=w.get(a,f);s(e,o+"Resolved",v)}catch(t){throw s(e,o+"ResolvedErrors",[t.message]),new p(t.message,{rawLink:""})}}export{b as resolveJsonExampleRef};
