import{isPrimitive as S}from"../../../../utils/guards/is-primitive.js";import{getNodeAttribute as c}from"../../../../markdoc/helpers/get-node-attribute.js";import{getVariable as v}from"../../../../markdoc/helpers/get-variable.js";import{isTag as A}from"../../../../markdoc/helpers/guards/is-tag.js";import{isNode as C}from"../../../../markdoc/helpers/guards/is-node.js";import{isConditionalNode as N}from"../../../../markdoc/helpers/guards/is-conditional-node.js";import{isVariable as y}from"../../../../markdoc/helpers/guards/is-variable.js";import{isExampleNode as B}from"../../../../markdoc/helpers/guards/is-example-node.js";import{isFenceNode as E}from"../../../../markdoc/helpers/guards/is-fence-node.js";function a(s,n={}){const r=[];for(const e of s){if(S(e)){const i=n.isTrim?m(String(e)):String(e);i&&r.push(i)}else if(C(e))switch(e.type){case"text":r.push(a([c(e,"content")],n));break;case"code":r.push(`\`${a([c(e,"content")],n)}\``);break;case"blockquote":r.push(`> ${a(e.children,n)}
`);break;case"fence":const i=c(e,"process")===!1;r.push(g(e,n,i));break;case"list":const h=c(e,"ordered")||!1,l=c(e,"marker"),d=e.children.map((t,o)=>`${" ".repeat((n?.indent??0)*2)}${h?o+1:""}${l} ${a([t],{...n??{},indent:(n?.indent??0)+1})}`);r.push(`${d.join("")}
`);break;case"item":r.push(e.children.map(t=>`${a([t],n).trimEnd()}
`).join(""));break;case"thead":const u=e.children.map(t=>a([t],n));r.push(`${u}| ${" --- |".repeat(e.children?.[0]?.children.length)}
`);break;case"tr":const f=e.children.map(t=>a([t],n));r.push(`| ${f.join(" | ")} |
`);break;case"em":r.push(`*${a(e.children,n)}*`);break;case"strong":r.push(`**${a(e.children,n)}**`);break;case"softbreak":case"hardbreak":r.push(`
`);break;case"hr":r.push(`

---

`);break;case"heading":const p=c(e,"level")??0;r.push(`${"#".repeat(p)} ${a(e.children,n)}
`);break;case"image":const k=c(e,"alt")||"",b=c(e,"src")||"";r.push(`![${k}](${b})`);break;case"link":const T=c(e,"href")||"";r.push(`[${a(e.children,n)}](${T})`);break;case"paragraph":case"table":r.push(`${a(e.children,n)}
`);break;case"tag":if(N(e)&&n.skipConditionals)continue;if(B(e)){r.push(...e.children.map(t=>{if(E(t)){const o=c(t,"process")!==!0;return g(t,n,o)}return a([t],n)}));continue}if(e.tag==="code-snippet"){const t=c(e,"rawContent");if(t){const o=c(e,"language"),$=c(e,"title"),j=`
\`\`\`${`${o||""}${$?`${o?" ":""}${$}`:""}`}
${t.trimEnd()}
\`\`\`
`;r.push(j)}continue}if(e.tag==="json-example"){const t=w(e);t&&r.push(t);continue}if(e.tag==="json-schema"){const t=x(e);t&&r.push(t);continue}if(n.renderTag){const t=n.renderTag(e,{getBody:()=>a(e.children,n)});if(t!==void 0){r.push(t);continue}}r.push(`${a(e.children,n)}`);break;default:r.push(a(e.children,n));break}else A(e)&&r.push(a(e.children,n));y(e)&&r.push(a([v(e,n.variables)],n))}return n.isTrim?m(r.map(e=>m(e)).join("")):r.join("")}function m(s){return s.replace(/^[ \t\r\f]+|[ \t\r\f]+$/g,"")}function g(s,n={},r=!1){const e=c(s,"title"),i=c(s,"language"),h=`${i||""}${e?`${i?" ":""}${e}`:""}`,l=r?(c(s,"content")||"").trimEnd():a(s.children,n).trimEnd();return`
\`\`\`${h}
${l}
\`\`\`
`}function w(s){const n=c(s,"json")??c(s,"valueResolved")??c(s,"schemaResolved"),r=c(s,"title");return n===null?null:`
\`\`\`${`json${r?` ${r}`:""}`}
${JSON.stringify(n,null,2)}
\`\`\`
`}function x(s){const n=c(s,"schemaResolved");if(n===null)return null;const r=c(s,"title"),{components:e,openapi:i,...h}=n,{__root:l,...d}=e?.schemas||{},u=l?{...l,...Object.keys(d).length>0?{components:{schemas:d}}:{},...h}:n;return`
\`\`\`${`json${r?` ${r}`:""}`}
${JSON.stringify(u,null,2)}
\`\`\`
`}export{a as toMarkdown};
