/*
* 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!
*/
import{LitElement as g,html as r}from"lit";import{property as u,customElement as v}from"lit/decorators.js";import s from"~labels/core.wizards";import m from"~labels/core.core";import"@typo3/backend/element/icon-element.js";import h from"@typo3/backend/modal.js";var d=function(p,t,e,a){var l=arguments.length,n=l<3?t:a===null?a=Object.getOwnPropertyDescriptor(t,e):a,i;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(p,t,e,a);else for(var o=p.length-1;o>=0;o--)(i=p[o])&&(n=(l<3?i(n):l>3?i(t,e,n):i(t,e))||n);return l>3&&n&&Object.defineProperty(t,e,n),n};let c=class extends g{constructor(){super(...arguments),this.type="textarea",this.selectorData="",this.delimiter="|",this.enclosure="",this.appendRows=1,this.table=[]}get firstRow(){return this.table[0]||[]}connectedCallback(){super.connectedCallback(),this.selectorData=this.getAttribute("selector"),this.delimiter=this.getAttribute("delimiter"),this.enclosure=this.getAttribute("enclosure")||"",this.readTableFromTextarea()}createRenderRoot(){return this}render(){return this.renderTemplate()}provideMinimalTable(){(this.table.length===0||this.firstRow.length===0)&&(this.table=[[""]])}readTableFromTextarea(){const t=document.querySelector(this.selectorData),e=[];t.value.split(`
`).forEach(a=>{if(a!==""){this.enclosure&&(a=a.replace(new RegExp(this.enclosure,"g"),""));const l=a.split(this.delimiter);e.push(l)}}),this.table=e}writeTableSyntaxToTextarea(){const t=document.querySelector(this.selectorData);let e="";this.table.forEach(a=>{const l=a.length;e+=a.reduce((n,i,o)=>{const b=l-1===o?"":this.delimiter;return i=i.replace(/\r?\n/g,"
"),n+this.enclosure+i+this.enclosure+b},"")+`
`}),t.value=e,t.dispatchEvent(new CustomEvent("change",{bubbles:!0}))}modifyTable(t,e,a){const l=t.target;this.table[e][a]=l.value,this.writeTableSyntaxToTextarea(),this.requestUpdate()}toggleType(){this.type=this.type==="input"?"textarea":"input"}moveColumn(t,e){this.table=this.table.map(a=>{const l=a.splice(t,1);return a.splice(e,0,...l),a}),this.writeTableSyntaxToTextarea(),this.requestUpdate()}appendColumn(t,e){this.table=this.table.map(a=>(a.splice(e+1,0,""),a)),this.writeTableSyntaxToTextarea(),this.requestUpdate()}removeColumn(t,e){this.table=this.table.map(a=>(a.splice(e,1),a)),this.writeTableSyntaxToTextarea(),this.requestUpdate()}moveRow(t,e,a){const l=this.table.splice(e,1);this.table.splice(a,0,...l),this.writeTableSyntaxToTextarea(),this.requestUpdate()}appendRow(t,e){const a=this.firstRow.concat().fill(""),l=new Array(this.appendRows).fill(a);this.table.splice(e+1,0,...l),this.writeTableSyntaxToTextarea(),this.requestUpdate()}removeRow(t,e){this.table.splice(e,1),this.writeTableSyntaxToTextarea(),this.requestUpdate()}renderTemplate(){this.provideMinimalTable();const t=Object.keys(this.firstRow).map(l=>parseInt(l,10)),e=t[t.length-1],a=this.table.length-1;return r`
| ${this.renderTypeButton()} | ${t.map(l=>r`${this.renderColButtons(l,e)} | `)}${this.table.map((l,n)=>r`
|---|---|
| ${this.renderRowButtons(n,a)} | ${l.map((i,o)=>r`${this.renderDataElement(i,n,o)} | `)}