/*! phoscon-app 2024-02-26 */

const SVGNS="http://www.w3.org/2000/svg",INTERACTIVE=1,ICON=2,svgData={ICON:{x:0,y:0,w:150,h:150,viewbox:"-2 -2 152 152",padding:20,stroke:3},INTERACTIVE:{x:0,y:0,w:250,h:250,viewbox:"-2 -2 254 254",padding:40,stroke:2}},getGenericSwIcon=buttonmap=>{return createSvg({data:svgData.ICON,buttonmap:buttonmap,type:2})},getGenericSwInteractive=buttonmap=>{return createSvg({data:svgData.INTERACTIVE,buttonmap:buttonmap,type:1})},createSvg=({data:data,buttonmap:buttonmap,type:type})=>{var svg=getSvgNode(data.viewbox,data.w,data.h);if(1===type){var outerRect=getOuterRect(data.x,data.y,data.w,data.h,data.stroke);svg.appendChild(outerRect)}let stroke=2===type?2*data.stroke:data.stroke/2;var innerRect=getInnerRect(data.x,data.y,data.w,data.h,stroke,data.padding);return svg.appendChild(innerRect),svg.setAttribute("class",2===type?"item-icon generated-sw-svg":"generated-sw-svg"),buttonmap&&buttonmap.buttons?getButtonRectGroup(data.x,data.y,data.h,data.w,data.padding,buttonmap,type).then(gNodes=>gNodes.forEach(g=>svg.appendChild(g))).then(()=>setSwTargets(svg,buttonmap)).catch(err=>err):Promise.resolve(svg)},getSvgNode=(viewbox,w,h)=>{var svg=document.createElementNS(SVGNS,"svg");return setAttributes(svg,{viewBox:viewbox,height:w,width:h}),svg},getOuterRect=(x,y,w,h,stroke)=>{let rect=document.createElementNS(SVGNS,"rect");return rectSetAttributes(rect,x,y,w,h),setAttributes(rect,{"stroke-width":stroke,class:"sw-line",rx:"3",ry:"3",fill:"none"}),rect},getInnerRect=(x,y,w,h,stroke,padding)=>{let rect=document.createElementNS(SVGNS,"rect");return rectSetAttributes(rect,x+padding/2-4,y+padding/2-4,w-padding+8,h-padding+8),setAttributes(rect,{"stroke-width":stroke,class:"sw-line",rx:"3",ry:"3",fill:"none"}),rect},getButtonRectGroup=(x,y,svgHeight,svgWidth,padding,buttonmap,type)=>new Promise((resolve,reject)=>{let rectWidth,rectHeight,buttonNumber=Object.keys(buttonmap.buttons).length,pHalf=padding/2;buttonNumber%2==0||buttonNumber>3?(rectWidth=(svgWidth-padding)/2,rectHeight=(svgHeight-padding)/Math.round(buttonNumber/2)):(rectWidth=svgWidth-padding,rectHeight=(svgHeight-padding)/buttonNumber);let group1=document.createElementNS(SVGNS,"g"),group2=document.createElementNS(SVGNS,"g");Object.keys(buttonmap.buttons).forEach(btn=>{btn=parseInt(btn),btnPre=btn-1,x=pHalf,y=btnPre*rectHeight+pHalf,(buttonNumber%2==0||buttonNumber>3)&&(y=(Math.round(btn/2)-1)*rectHeight+pHalf,x=btn%2==0?rectWidth+pHalf:x);let rect=createRect(x,y,rectWidth,rectHeight,"sw-line");group1.appendChild(rect);let rect2=createRect(x,y,rectWidth,rectHeight,"sw-fill");2===type?rect2.setAttribute("data-btn",1e3*btn):rect2.setAttribute("class","sw-target sw-fill"),group2.appendChild(rect2)}),resolve([group1,group2])}),createRect=(x,y,w,h,cl)=>{let rect=document.createElementNS(SVGNS,"rect");return rectSetAttributes(rect,x,y,w,h,cl),rect},rectSetAttributes=(rect,x,y,w,h,cl)=>{setAttributes(rect,{x:x,y:y,width:w,height:h,"stroke-width":"1",class:cl}),"sw-line"==cl&&rect.setAttribute("fill","none")},setAttributes=(elem,attr)=>Object.keys(attr).forEach(a=>{elem.setAttribute(a,attr[a])});function setSwTargets(svg,btnmap){return new Promise((resolve,reject)=>{let targets=svg.querySelectorAll(".sw-target")||[];if(0==targets.length)return void resolve(svg);let buttons=btnmap.buttons,btnkeys=Object.keys(buttons),values=btnmap.values;targets.forEach((t,idx)=>{let button=btnkeys[idx],btnval=Object.entries(values).filter(([key,val])=>val.button==button).map(([key,val])=>key).join();t.setAttribute("data-buttonEvent",btnval)}),resolve(svg)})}