/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
class c{constructor(t,s){this.childNodes={},this.extPath="",this.parent=null,this.name=t,this.childNodes={},this.extPath="",this.value="",this.isExternal=!1,this.tsParser=s}getChildNodes(){const t=this.getExtNode();if(t!==null&&typeof t.c=="object")for(const s of Object.keys(t.c)){const e=new c(s,this.tsParser);e.global=!0,e.value=t.c[s].v?t.c[s].v:"",e.isExternal=!0,this.childNodes[s]=e}return this.childNodes}getValue(){if(this.value)return this.value;const t=this.getExtNode();if(t&&t.v)return t.v;const s=this.getNodeTypeFromTsref();return s||""}getNodeTypeFromTsref(){const t=this.extPath.split("."),s=t.pop(),e=this.parent.getValue();return e&&this.tsParser.tsRef.typeHasProperty(e,s)?this.tsParser.tsRef.getType(e).properties[s].value:""}getExtNode(){let t=this.tsParser.extTsObjTree;if(this.extPath==="")return t;const s=this.extPath.split(".");for(let e=0;e<s.length;e++){const r=s[e];if(typeof t.c>"u"||typeof t.c[r]>"u")return null;t=t.c[r]}return t}}class g extends Array{lastElementEquals(t){return this.length>0&&this[this.length-1]===t}popIfLastElementEquals(t){return this.lastElementEquals(t)?(this.pop(),!0):!1}}class E{constructor(t,s){this.tsRef=t,this.extTsObjTree=s,this.tsTree=new c("_L_",this)}getOperator(t){const s=[":=","=<","<",">","="];for(let e=0;e<s.length;e++){const r=s[e];if(t.indexOf(r)!==-1)return(r==="=<"||r==="<")&&t.indexOf(">")>-1?"=":r}return-1}buildTsObjTree(t){this.tsTree=new c("",this),this.tsTree.value="TLO";let s=1,e="",r=!1,o=!1;const n=new g,l=[];let i;for(;s<=t.currentLineNumber;){e="";const h=t.lineTokens[s-1];for(let f=0;f<=h.length;++f)if(f<h.length&&h[f].string.length>0){const a=h[f].string;a.startsWith("#")?n.push("#"):a==="("?n.push("("):a.startsWith("/*")?n.push("/*"):a==="{"&&this.getOperator(e)===-1&&(n.push("{"),l.push(e.trim()),r=!0),a.search(/^\s*\[.*\]/)!==-1&&e.search(/\S/)===-1&&a.search(/^\s*\[(global|end|GLOBAL|END)\]/)===-1&&!n.lastElementEquals("#")&&!n.lastElementEquals("/*")&&!n.lastElementEquals("{")&&!n.lastElementEquals("(")&&(o=!0,r=!0),e.search(/\S/)===-1&&!n.lastElementEquals("#")&&!n.lastElementEquals("/*")&&!n.lastElementEquals("(")&&(a.search(/^\s*\[(global|end|GLOBAL|END)\]/)!==-1&&!n.lastElementEquals("{")||a.search(/^\s*\[(global|GLOBAL)\]/)!==-1)&&(o=!1,r=!0),a===")"&&n.popIfLastElementEquals("("),a.startsWith("*/")&&(n.popIfLastElementEquals("/*"),r=!0),a==="}"&&e.replace(/\s/g,"")===""&&(n.popIfLastElementEquals("{"),l.length>0&&l.pop(),r=!0),n.lastElementEquals("#")||(e+=a)}else{if(!n.lastElementEquals("/*")&&!n.lastElementEquals("(")&&!r&&!o){e=e.trim();const a=this.getOperator(e);if(a!==-1){const d=e.indexOf(a);i=e.substring(0,d),l.length>0&&(i=l.join(".")+"."+i);let u=e.substring(d+a.length,e.length).trim();switch(i=i.trim(),a){case"=":i.search(/\s/g)===-1&&i.length>0&&this.setTreeNodeValue(i,u);break;case"=<":l.length>0&&u.substr(0,1)==="."&&(u=l.join(".")+u),i.search(/\s/g)===-1&&i.length>0&&u.search(/\s/g)===-1&&u.length>0&&this.setReference(i,u);break;case"<":l.length>0&&u.substr(0,1)==="."&&(u=l.join(".")+u),i.search(/\s/g)===-1&&i.length>0&&u.search(/\s/g)===-1&&u.length>0&&this.setCopy(i,u);break;case">":this.deleteTreeNodeValue(i);break;case":=":break;default:break}}}n.popIfLastElementEquals("#"),r=!1}s++}if(!n.lastElementEquals("/*")&&!n.lastElementEquals("(")&&!r){const h=e.indexOf("<");h!==-1?(i=e.substring(h+1,e.length).trim(),l.length>0&&i.substr(0,1)==="."&&(i=l.join(".")+i)):(i=e,l.length>0&&(i=l.join(".")+"."+i,i=i.replace(/\s/g,"")));const f=i.lastIndexOf(".");i=i.substring(0,f)}return this.getTreeNode(i)}getTreeNode(t){if(t=t.trim(),t.length===0)return this.tsTree;const s=t.split(".");let e=this.tsTree.childNodes,r,o=this.tsTree;for(let n=0;n<s.length;n++){if(r=s[n],typeof e[r]>"u"||typeof e[r].childNodes>"u"){e[r]=new c(r,this),e[r].parent=o;let l=o.extPath;l&&(l+="."),l+=r,e[r].extPath=l}if(n===s.length-1)return e[r];o=e[r],e=e[r].childNodes}}setTreeNodeValue(t,s){const e=this.getTreeNode(t);e.parent!==null&&e.parent.value==="GIFBUILDER"&&s==="TEXT"&&(s="GB_TEXT"),e.parent!==null&&e.parent.value==="GIFBUILDER"&&s==="IMAGE"&&(s="GB_IMAGE"),this.tsRef.isType(s)&&(e.value=s)}deleteTreeNodeValue(t){const s=this.getTreeNode(t);s.value=null,s.childNodes={}}setReference(t,s){const e=t.split("."),r=e[e.length-1],o=this.getTreeNode(t),n=this.getTreeNode(s);o.parent!==null?o.parent.childNodes[r]=n:this.tsTree.childNodes[r]=n}setCopy(t,s){this.clone=l=>{if(typeof l!="object")return l;const i={};for(const h in l)h!=="tsParser"&&(h!=="parent"?typeof l[h]=="object"?i[h]=this.clone(l[h]):i[h]=l[h]:"parent"in l&&(i.parent=l.parent));return i};const e=t.split("."),r=e[e.length-1],o=this.getTreeNode(t),n=this.getTreeNode(s);o.parent!==null?o.parent.childNodes[r]=this.clone(n):this.tsTree.childNodes[r]=this.clone(n)}}export{c as TreeNode,E as TsParser};
