/*!
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{Node as t,textblockTypeInputRule as e,mergeAttributes as n}from"../../core/dist/index.js";import{Plugin as r,PluginKey as a,TextSelection as o,Selection as s}from"../../../prosemirror-state/dist/index.js";var i=/^```([a-z]+)?[\s\n]$/,l=/^~~~([a-z]+)?[\s\n]$/,d=t.create({name:"codeBlock",addOptions:()=>({languageClassPrefix:"language-",exitOnTripleEnter:!0,exitOnArrowDown:!0,defaultLanguage:null,enableTabIndentation:!1,tabSize:4,HTMLAttributes:{}}),content:"text*",marks:"",group:"block",code:!0,defining:!0,addAttributes(){return{language:{default:this.options.defaultLanguage,parseHTML:t=>{var e;const{languageClassPrefix:n}=this.options;if(!n)return null;const r=[...(null==(e=t.firstElementChild)?void 0:e.classList)||[]].filter(t=>t.startsWith(n)).map(t=>t.replace(n,""))[0];return r||null},rendered:!1}}},parseHTML:()=>[{tag:"pre",preserveWhitespace:"full"}],renderHTML({node:t,HTMLAttributes:e}){return["pre",n(this.options.HTMLAttributes,e),["code",{class:t.attrs.language?this.options.languageClassPrefix+t.attrs.language:null},0]]},markdownTokenName:"code",parseMarkdown:(t,e)=>{var n;return!1===(null==(n=t.raw)?void 0:n.startsWith("```"))&&"indented"!==t.codeBlockStyle?[]:e.createNode("codeBlock",{language:t.lang||null},t.text?[e.createTextNode(t.text)]:[])},renderMarkdown:(t,e)=>{var n;let r="";const a=(null==(n=t.attrs)?void 0:n.language)||"";if(t.content){r=[`\`\`\`${a}`,e.renderChildren(t.content),"```"].join("\n")}else r=`\`\`\`${a}\n\n\`\`\``;return r},addCommands(){return{setCodeBlock:t=>({commands:e})=>e.setNode(this.name,t),toggleCodeBlock:t=>({commands:e})=>e.toggleNode(this.name,"paragraph",t)}},addKeyboardShortcuts(){return{"Mod-Alt-c":()=>this.editor.commands.toggleCodeBlock(),Backspace:()=>{const{empty:t,$anchor:e}=this.editor.state.selection,n=1===e.pos;return!(!t||e.parent.type.name!==this.name)&&(!(!n&&e.parent.textContent.length)&&this.editor.commands.clearNodes())},Tab:({editor:t})=>{var e;if(!this.options.enableTabIndentation)return!1;const n=null!=(e=this.options.tabSize)?e:4,{state:r}=t,{selection:a}=r,{$from:o,empty:s}=a;if(o.parent.type!==this.type)return!1;const i=" ".repeat(n);return s?t.commands.insertContent(i):t.commands.command(({tr:t})=>{const{from:e,to:n}=a,o=r.doc.textBetween(e,n,"\n","\n").split("\n").map(t=>i+t).join("\n");return t.replaceWith(e,n,r.schema.text(o)),!0})},"Shift-Tab":({editor:t})=>{var e;if(!this.options.enableTabIndentation)return!1;const n=null!=(e=this.options.tabSize)?e:4,{state:r}=t,{selection:a}=r,{$from:s,empty:i}=a;return s.parent.type===this.type&&(i?t.commands.command(({tr:t})=>{var e;const{pos:a}=s,i=s.start(),l=s.end(),d=r.doc.textBetween(i,l,"\n","\n").split("\n");let c=0,p=0;const u=a-i;for(let n=0;n<d.length;n+=1){if(p+d[n].length>=u){c=n;break}p+=d[n].length+1}const m=(null==(e=d[c].match(/^ */))?void 0:e[0])||"",h=Math.min(m.length,n);if(0===h)return!0;let g=i;for(let n=0;n<c;n+=1)g+=d[n].length+1;t.delete(g,g+h);return a-g<=h&&t.setSelection(o.create(t.doc,g)),!0}):t.commands.command(({tr:t})=>{const{from:e,to:o}=a,s=r.doc.textBetween(e,o,"\n","\n").split("\n").map(t=>{var e;const r=(null==(e=t.match(/^ */))?void 0:e[0])||"",a=Math.min(r.length,n);return t.slice(a)}).join("\n");return t.replaceWith(e,o,r.schema.text(s)),!0}))},Enter:({editor:t})=>{if(!this.options.exitOnTripleEnter)return!1;const{state:e}=t,{selection:n}=e,{$from:r,empty:a}=n;if(!a||r.parent.type!==this.type)return!1;const o=r.parentOffset===r.parent.nodeSize-2,s=r.parent.textContent.endsWith("\n\n");return!(!o||!s)&&t.chain().command(({tr:t})=>(t.delete(r.pos-2,r.pos),!0)).exitCode().run()},ArrowDown:({editor:t})=>{if(!this.options.exitOnArrowDown)return!1;const{state:e}=t,{selection:n,doc:r}=e,{$from:a,empty:o}=n;if(!o||a.parent.type!==this.type)return!1;if(!(a.parentOffset===a.parent.nodeSize-2))return!1;const i=a.after();if(void 0===i)return!1;return r.nodeAt(i)?t.commands.command(({tr:t})=>(t.setSelection(s.near(r.resolve(i))),!0)):t.commands.exitCode()}}},addInputRules(){return[e({find:i,type:this.type,getAttributes:t=>({language:t[1]})}),e({find:l,type:this.type,getAttributes:t=>({language:t[1]})})]},addProseMirrorPlugins(){return[new r({key:new a("codeBlockVSCodeHandler"),props:{handlePaste:(t,e)=>{if(!e.clipboardData)return!1;if(this.editor.isActive(this.type.name))return!1;const n=e.clipboardData.getData("text/plain"),r=e.clipboardData.getData("vscode-editor-data"),a=r?JSON.parse(r):void 0,s=null==a?void 0:a.mode;if(!n||!s)return!1;const{tr:i,schema:l}=t.state,d=l.text(n.replace(/\r\n?/g,"\n"));return i.replaceSelectionWith(this.type.create({language:s},d)),i.selection.$from.parent.type!==this.type&&i.setSelection(o.near(i.doc.resolve(Math.max(0,i.selection.from-2)))),i.setMeta("paste",!0),t.dispatch(i),!0}}})]}});export{d as CodeBlock,i as backtickInputRegex,l as tildeInputRegex};