/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{parser as e}from"../../../@lezer/xml/dist/index.js";import{LanguageSupport as t,LRLanguage as n,indentNodeProp as l,foldNodeProp as o,bracketMatchingHandle as a,syntaxTree as r}from"../../language/dist/index.js";import{EditorSelection as i}from"../../state/dist/index.js";import{EditorView as s}from"../../view/dist/index.js";function m(e,t){let n=t&&t.getChild("TagName");return n?e.sliceString(n.from,n.to):""}function c(e,t){let n=t&&t.firstChild;return n&&"OpenTag"==n.name?m(e,n):""}function p(e){for(let t=e&&e.parent;t;t=t.parent)if("Element"==t.name)return t;return null}class u{constructor(e,t,n){this.attrs=t,this.attrValues=n,this.children=[],this.name=e.name,this.completion=Object.assign(Object.assign({type:"type"},e.completion||{}),{label:this.name}),this.openCompletion=Object.assign(Object.assign({},this.completion),{label:"<"+this.name}),this.closeCompletion=Object.assign(Object.assign({},this.completion),{label:"</"+this.name+">",boost:2}),this.closeNameCompletion=Object.assign(Object.assign({},this.completion),{label:this.name+">"}),this.text=e.textContent?e.textContent.map(e=>({label:e,type:"text"})):[]}}const f=/^[:\-\.\w\u00b7-\uffff]*$/;function g(e){return Object.assign(Object.assign({type:"property"},e.completion||{}),{label:e.name})}function d(e){return"string"==typeof e?{label:`"${e}"`,type:"constant"}:/^"/.test(e.label)?e:Object.assign(Object.assign({},e),{label:`"${e.label}"`})}function h(e,t){let n=[],l=[],o=/* @__PURE__ */Object.create(null);for(let r of t){let e=g(r);n.push(e),r.global&&l.push(e),r.values&&(o[r.name]=r.values.map(d))}let a=[],i=[],s=/* @__PURE__ */Object.create(null);for(let r of e){let e=l,t=o;r.attributes&&(e=e.concat(r.attributes.map(e=>"string"==typeof e?n.find(t=>t.label==e)||{label:e,type:"property"}:(e.values&&(t==o&&(t=Object.create(t)),t[e.name]=e.values.map(d)),g(e)))));let m=new u(r,e,t);s[m.name]=m,a.push(m),r.top&&i.push(m)}i.length||(i=a);for(let r=0;r<a.length;r++){let t=e[r],n=a[r];if(t.children)for(let e of t.children)s[e]&&n.children.push(s[e]);else n.children=a}return e=>{var t;let{doc:n}=e.state,u=function(e,t){var n;let l=r(e).resolveInner(t,-1),o=null;for(let a=l;!o&&a.parent;a=a.parent)"OpenTag"!=a.name&&"CloseTag"!=a.name&&"SelfClosingTag"!=a.name&&"MismatchedCloseTag"!=a.name||(o=a);if(o&&(o.to>t||o.lastChild.type.isError)){let e=o.parent;if("TagName"==l.name)return"CloseTag"==o.name||"MismatchedCloseTag"==o.name?{type:"closeTag",from:l.from,context:e}:{type:"openTag",from:l.from,context:p(e)};if("AttributeName"==l.name)return{type:"attrName",from:l.from,context:o};if("AttributeValue"==l.name)return{type:"attrValue",from:l.from,context:o};let n=l==o||"Attribute"==l.name?l.childBefore(t):l;return"StartTag"==(null==n?void 0:n.name)?{type:"openTag",from:t,context:p(e)}:"StartCloseTag"==(null==n?void 0:n.name)&&n.to<=t?{type:"closeTag",from:t,context:e}:"Is"==(null==n?void 0:n.name)?{type:"attrValue",from:t,context:o}:n?{type:"attrName",from:t,context:o}:null}if("StartCloseTag"==l.name)return{type:"closeTag",from:t,context:l.parent};for(;l.parent&&l.to==t&&!(null===(n=l.lastChild)||void 0===n?void 0:n.type.isError);)l=l.parent;return"Element"==l.name||"Text"==l.name||"Document"==l.name?{type:"tag",from:t,context:"Element"==l.name?l:p(l)}:null}(e.state,e.pos);if(!u||"tag"==u.type&&!e.explicit)return null;let{type:g,from:d,context:h}=u;if("openTag"==g){let e=i,t=c(n,h);if(t){let n=s[t];e=(null==n?void 0:n.children)||a}return{from:d,options:e.map(e=>e.completion),validFor:f}}if("closeTag"==g){let l=c(n,h);return l?{from:d,to:e.pos+(">"==n.sliceString(e.pos,e.pos+1)?1:0),options:[(null===(t=s[l])||void 0===t?void 0:t.closeNameCompletion)||{label:l+">",type:"type"}],validFor:f}:null}if("attrName"==g){let e=s[m(n,h)];return{from:d,options:(null==e?void 0:e.attrs)||l,validFor:f}}if("attrValue"==g){let t=function(e,t,n){let l=t&&t.getChildren("Attribute").find(e=>e.from<=n&&e.to>=n),o=l&&l.getChild("AttributeName");return o?e.sliceString(o.from,o.to):""}(n,h,d);if(!t)return null;let l=s[m(n,h)],a=((null==l?void 0:l.attrValues)||o)[t];return a&&a.length?{from:d,to:e.pos+('"'==n.sliceString(e.pos,e.pos+1)?1:0),options:a,validFor:/^"[^"]*"?$/}:null}if("tag"==g){let t=c(n,h),l=s[t],o=[],r=h&&h.lastChild;!t||r&&"CloseTag"==r.name&&m(n,r)==t||o.push(l?l.closeCompletion:{label:"</"+t+">",type:"type",boost:2});let p=o.concat(((null==l?void 0:l.children)||(h?a:i)).map(e=>e.openCompletion));if(h&&(null==l?void 0:l.text.length)){let t=h.firstChild;t.to>e.pos-20&&!/\S/.test(e.state.sliceDoc(t.to,e.pos))&&(p=p.concat(l.text))}return{from:d,options:p,validFor:/^<\/?[:\-\.\w\u00b7-\uffff]*$/}}return null}}const b=/* @__PURE__ */n.define({name:"xml",parser:/* @__PURE__ */e.configure({props:[
/* @__PURE__ */l.add({Element(e){let t=/^\s*<\//.test(e.textAfter);return e.lineIndent(e.node.from)+(t?0:e.unit)},"OpenTag CloseTag SelfClosingTag":e=>e.column(e.node.from)+e.unit}),
/* @__PURE__ */o.add({Element(e){let t=e.firstChild,n=e.lastChild;return t&&"OpenTag"==t.name?{from:t.to,to:"CloseTag"==n.name?n.from:e.to}:null}}),
/* @__PURE__ */a.add({"OpenTag CloseTag":e=>e.getChild("TagName")})]}),languageData:{commentTokens:{block:{open:"\x3c!--",close:"--\x3e"}},indentOnInput:/^\s*<\/$/}});function C(e={}){let n=[b.data.of({autocomplete:h(e.elements||[],e.attributes||[])})];return!1!==e.autoCloseTags&&n.push(v),new t(b,n)}function T(e,t,n=e.length){if(!t)return"";let l=t.firstChild,o=l&&l.getChild("TagName");return o?e.sliceString(o.from,Math.min(o.to,n)):""}const v=/* @__PURE__ */s.inputHandler.of((e,t,n,l,o)=>{if(e.composing||e.state.readOnly||t!=n||">"!=l&&"/"!=l||!b.isActiveAt(e.state,t,-1))return!1;let a=o(),{state:s}=a,m=s.changeByRange(e=>{var t,n,o;let a,{head:m}=e,c=s.doc.sliceString(m-1,m)==l,p=r(s).resolveInner(m,-1);if(c&&">"==l&&"EndTag"==p.name){let l=p.parent;if("CloseTag"!=(null===(n=null===(t=l.parent)||void 0===t?void 0:t.lastChild)||void 0===n?void 0:n.name)&&(a=T(s.doc,l.parent,m))){return{range:e,changes:{from:m,to:m+(">"===s.doc.sliceString(m,m+1)?1:0),insert:`</${a}>`}}}}else if(c&&"/"==l&&"StartCloseTag"==p.name){let e=p.parent;if(p.from==m-2&&"CloseTag"!=(null===(o=e.lastChild)||void 0===o?void 0:o.name)&&(a=T(s.doc,e,m))){let e=m+(">"===s.doc.sliceString(m,m+1)?1:0),t=`${a}>`;return{range:i.cursor(m+t.length,-1),changes:{from:m,to:e,insert:t}}}}return{range:e}});return!m.changes.empty&&(e.dispatch([a,s.update(m,{userEvent:"input.complete",scrollIntoView:!0})]),!0)});export{v as autoCloseTags,h as completeFromSchema,C as xml,b as xmlLanguage};