import{logger as N}from"../../../tools/notifiers/logger.js";import{isNode as f}from"../../../../markdoc/helpers/guards/is-node.js";import{isConditionalNode as T}from"../../../../markdoc/helpers/guards/is-conditional-node.js";import{isContentNode as m}from"../../../../markdoc/helpers/guards/is-content-node.js";import{getNodeAttribute as u}from"../../../../markdoc/helpers/get-node-attribute.js";import{extractRbacFromCondition as w}from"../../../../markdoc/helpers/extract-rbac-from-condition-node.js";import{TextNode as n}from"./nodes/text-node.js";import{TAG_TITLE_ATTRIBUTES as b,TagNode as p}from"./nodes/tag-node.js";import{HeadingNode as y}from"./nodes/heading-node.js";import{joinSectionContent as d}from"./join-section-content.js";class B{#s;#o;#e;#r;#c;constructor({ast:i,partials:t,skipConditionals:r=!1,getInnerContent:s,renderTag:o}){if(this.#s=i,this.#o=t,this.#e=r,this.#r=s,this.#c=o,!this.#s||!f(this.#s))throw new Error("ast is not a valid Markdoc Node.")}#i(){return{skipConditionals:this.#e,renderTag:this.#c}}*transform(){yield*this.#n(this.#s,{parentNode:null})}*#n(i,t,r=this.#r){if(!(!i||!f(i))){if(T(i)){if(this.#e)return;const s=w(i);s&&s.length>0&&(yield*this.#t(i,{...t,rbacTeams:s},r));return}if(m(i)){yield new n({node:i,content:r([i],this.#i()),...t});return}if(i.type==="heading"){yield new y({node:i,content:r([i],this.#i()),rbacTeams:t?.rbacTeams});return}if(i.type==="tag"){yield*this.#l(i,t,r);return}yield*this.#t(i,t,r)}}*#l(i,t,r=this.#r){switch(i.tag){case"partial":{const s=i.attributes.file,o=i.attributes.variables??{};if(s&&this.#o[s]){yield*this.#n(this.#o[s],t,(e,l)=>r(e,{...l,variables:o}));return}N.warn(`Could not create search indexes for partial \u201C${s}\u201D: file not found`);return}case"cards":case"tabs":case"code-walkthrough":{yield*this.#t(i,t,r);return}case"markdoc-example":{const s=r([i],this.#i());yield new n({node:i,content:s,...t});return}case"code-snippet":{const s=u(i,"title"),o=r([i],this.#i());if(s){const e=new p({node:i,content:s,...t});t={...t,parentNode:e},yield e}o&&(yield new n({node:i,content:o,...t}));return}default:{const s=this.#h(i,t,r);if(s){yield*s;return}const o=b.find(c=>c in i.attributes),e=(o&&u(i,o))??"",l=typeof e=="string"?e:r([e]);if(l){const c=new p({node:i,content:l,...t});t={...t,parentNode:c},yield c}if(a(i))yield*this.#t(i,t,r);else{const c=r([i],this.#i());c&&(yield new n({node:i,content:c,...t}))}return}}}#h(i,t,r){const s=()=>a(i)?d([...this.#t(i,t,r)]):"",o=this.#c?.(i,{getBody:s});return o===void 0?void 0:[new n({node:i,content:o.endsWith(`
`)?o:`${o}
`,...t})]}*#t(i,t,r=this.#r){for(const s of[...Object.values(i.slots),...i.children])for(const o of this.#n(s,t,r))o instanceof y&&(t={...t,parentNode:o}),yield o}}function a(h){return h.children.length>0||Object.keys(h.slots).length>0}export{B as AstToSearchNodeTransformer};
