/* esm.sh - yaml@2.9.1/browser/dist/nodes/Alias */
import{anchorIsValid as m}from"../doc/anchors.mjs";import{visit as p}from"../visit.mjs";import{ALIAS as v,isAlias as h,isCollection as d,isPair as g,hasAnchor as w}from"./identity.mjs";import{NodeBase as C}from"./Node.mjs";import{toJS as A}from"./toJS.mjs";var f=class extends C{constructor(o){super(v),this.source=o,Object.defineProperty(this,"tag",{set(){throw new Error("Alias nodes cannot have tags")}})}resolve(o,e){if(e?.maxAliasCount===0)throw new ReferenceError("Alias resolution is disabled");let r;e?.aliasResolveCache?r=e.aliasResolveCache:(r=[],p(o,{Node:(t,a)=>{(h(a)||w(a))&&r.push(a)}}),e&&(e.aliasResolveCache=r));let s;for(let t of r){if(t===this)break;t.anchor===this.source&&(s=t)}if(s&&e){let{anchors:t,doc:a,maxAliasCount:c}=e,i=t.get(s);if(i||(A(s,null,e),i=t.get(s)),i?.res===void 0){let l="This should not happen: Alias anchor was not resolved?";throw new ReferenceError(l)}if(c>=0&&(i.count+=1,i.aliasCount===0&&(i.aliasCount=u(a,s,t)),i.count*i.aliasCount>c)){let l="Excessive alias count indicates a resource exhaustion attack";throw new ReferenceError(l)}}return s}toJSON(o,e){if(!e)return{source:this.source};let r=this.resolve(e.doc,e);if(!r){let s=`Unresolved alias (the anchor must be set before the alias): ${this.source}`;throw new ReferenceError(s)}return e.anchors.get(r).res}toString(o,e,r){let s=`*${this.source}`;if(o){if(m(this.source),o.options.verifyAliasOrder&&!o.anchors.has(this.source)){let t=`Unresolved alias (the anchor must be set before the alias): ${this.source}`;throw new Error(t)}if(o.implicitKey)return`${s} `}return s}};function u(n,o,e){if(h(o)){let r=o.resolve(n),s=e&&r&&e.get(r);return s?s.count*s.aliasCount:0}else if(d(o)){let r=0;for(let s of o.items){let t=u(n,s,e);t>r&&(r=t)}return r}else if(g(o)){let r=u(n,o.key,e),s=u(n,o.value,e);return Math.max(r,s)}return 1}export{f as Alias};
//# sourceMappingURL=Alias.mjs.map