let d=null;function x(){return d||(d=fetch("/attributes/api/list",{headers:{Accept:"application/json","X-Requested-With":"XMLHttpRequest"},credentials:"same-origin"}).then(t=>t.ok?t.json():{system:[],custom:[]}).then(t=>[...t&&t.system||[],...t&&t.custom||[]]).catch(()=>[]),d)}function g(t){try{const n=JSON.parse(t.value||"{}");return n&&typeof n=="object"?n:{}}catch{return{}}}function v(t,n){t.value=JSON.stringify(n)}function b(t){const n=[],c=new Set,p=/\{\{\s*([a-zA-Z0-9_][\w.-]*)\s*\}\}/g;let u,f=0;for(;(u=p.exec(t||""))!==null;){const r=u[1];c.has(r)||(c.add(r),f+=1,n.push({slot:String(f),token:r,named:!/^\d+$/.test(r)}))}return n}function w({body:t,hidden:n,rows:c,empty:p}){if(!t||!n||!c)return;let u=[];function f(s){const l='<option value="">— not mapped —</option>',h=u.map(a=>{const e=a.key||"",o=(a.name||e)+(e?` (${e})`:""),i=e===s?" selected":"";return`<option value="${e.replace(/"/g,"&quot;")}"${i}>${o.replace(/</g,"&lt;")}</option>`}).join("");return l+h}function r(){const s=g(n),l=b(t.value),h=new Set(l.map(e=>e.slot)),a={};l.forEach(({slot:e,token:o,named:i})=>{i?a[e]=s[e]||o:s[e]&&(a[e]=s[e])}),Object.keys(s).forEach(e=>{h.has(e)}),v(n,a),p&&p.classList.toggle("hidden",l.length>0),c.innerHTML=l.map(({slot:e,token:o,named:i})=>`
            <div class="flex items-center gap-2" data-var-row="${e}">
                <span class="inline-flex items-center justify-center min-w-[34px] px-1.5 py-1 rounded-md bg-wa-bubble text-wa-deep text-[11px] font-mono font-semibold shrink-0">{{${o.replace(/</g,"&lt;")}}}</span>
                <svg viewBox="0 0 16 16" class="w-3 h-3 text-ink-500 shrink-0" fill="none" stroke="currentColor" stroke-width="1.6"><path d="M3 8h9M9 5l3 3-3 3"/></svg>
                <select data-var-slot="${e}" class="ctrl flex-1 px-[9px] py-[5px] border border-paper-200 rounded-lg bg-white text-[12px] text-ink-900 leading-[1.4] font-sans focus:outline-none focus:border-wa-deep focus:ring-4 focus:ring-wa-deep/10">
                    ${f(a[e]||"")}
                </select>
            </div>`).join(""),c.querySelectorAll("[data-var-slot]").forEach(e=>{e.addEventListener("change",()=>{const o=g(n),i=e.getAttribute("data-var-slot"),m=e.value;m?o[i]=m:delete o[i],v(n,o)})})}t.addEventListener("input",r),x().then(s=>{u=Array.isArray(s)?s:[],r()})}export{w as i};
