(()=>{"use strict";const e=window.wp.element,{render:a,useState:t,useEffect:r,useCallback:n}=wp.element,l=({saveStatus:a,isDirty:t,onSaveChanges:r})=>(0,e.createElement)("header",{className:"lcake-kit-header"},(0,e.createElement)("div",{className:"lcake-kit-header-text"},(0,e.createElement)("h1",null,"Widget Manager"),(0,e.createElement)("p",null,"Customize your WordPress widgets")),(0,e.createElement)("div",{className:"lcake-kit-header-actions"},(0,e.createElement)("button",{className:`button button-primary lcake-kit-save-button ${a}`,onClick:r,disabled:!t||"saving"===a},"saving"===a?"Saving...":"success"===a?"Saved!":"error"===a?"Error!":"Save Changes"))),i=({widget:a,onDragStart:t,onDragOver:r,isDragOver:n})=>(0,e.createElement)("div",{draggable:"true",onDragStart:()=>t(a),onDragOver:e=>r(e,a.id),className:"lcake-kit-widget-card "+(n?"is-drag-over":"")},(0,e.createElement)("span",{className:`dashicons ${a.icon}`}),(0,e.createElement)("div",{className:"lcake-kit-widget-info"},(0,e.createElement)("strong",null,a.label,a.is_pro),(0,e.createElement)("span",null,a.description))),s=({allWidgets:a,onDrop:r,onDragOver:n,onDragLeaveContainer:l,onDragStart:s,dragOverWidgetId:o})=>{const[d,c]=t(""),g=a.filter(e=>e.label.toLowerCase().includes(d.toLowerCase()));return(0,e.createElement)("aside",{className:"lcake-kit-sidebar"},(0,e.createElement)("h2",null,"Available Widgets"),(0,e.createElement)("div",{className:"lcake-kit-search-wrapper"},(0,e.createElement)("input",{type:"text",placeholder:"Search widgets...",value:d,onChange:e=>c(e.target.value)})),(0,e.createElement)("div",{className:"lcake-kit-widget-list",onDrop:r,onDragOver:n,onDragLeave:l},g.map(a=>(0,e.createElement)(i,{key:a.id,widget:a,onDragStart:s,onDragOver:n,isDragOver:o===a.id}))))},o=({enabledWidgets:a,onDrop:t,onDragOver:r,onDragLeaveContainer:n,onDragStart:l,dragOverWidgetId:s})=>(0,e.createElement)("section",{className:"lcake-kit-drop-area",onDrop:t,onDragOver:r,onDragLeave:n},(0,e.createElement)("h2",null,"Enabled Widgets"),(0,e.createElement)("div",{className:"lcake-kit-widget-list lcake-kit-widget-dropzone"},a.map(a=>(0,e.createElement)(i,{key:a.id,widget:a,onDragStart:l,onDragOver:r,isDragOver:s===a.id}))),0===a.length&&(0,e.createElement)("div",{className:"lcake-kit-drop-placeholder visible"},(0,e.createElement)("p",null,"Drop widgets here"),(0,e.createElement)("span",null,"Drag widgets from the sidebar to enable them")));a((0,e.createElement)(()=>{const{all_widgets:a,enabled_widgets:i}=window.LCAKE_SETTINGS,[d,c]=t([]),[g,m]=t([]),[v,u]=t(""),[E,k]=t(!1),[p,D]=t(null),[w,b]=t(null);r(()=>{const e=a.filter(e=>i.includes(e.id)),t=a.filter(e=>!i.includes(e.id));m(e),c(t)},[a,i]);const h=n(e=>{D(e)},[]),S=n(e=>{if(!p)return;k(!0);const a=g.some(e=>e.id===p.id)?"enabled":"available";let t="enabled"===a?[...g]:[...d];t=t.filter(e=>e.id!==p.id);let r="enabled"===e?[...g]:[...d];r=a===e?t:r.filter(e=>e.id!==p.id);const n=w?r.findIndex(e=>e.id===w):-1;-1!==n?r.splice(n,0,p):r.push(p),"enabled"===e?m(r):c(r),a!==e&&("enabled"===a?m(t):c(t)),D(null),b(null)},[p,g,d,w]),N=n((e,a=null)=>{e.preventDefault(),a!==w&&b(a)},[w]),O=n(()=>{b(null)},[]);return(0,e.createElement)(e.Fragment,null,(0,e.createElement)(l,{saveStatus:v,isDirty:E,onSaveChanges:async()=>{u("saving");const{api_url:e,nonce:a}=window.LCAKE_SETTINGS,t=g.map(e=>e.id);try{if(!(await fetch(e,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":a},body:JSON.stringify({enabled_widgets:t})})).ok)throw new Error("Network response was not ok.");k(!1),u("success")}catch(e){console.error("Error saving settings:",e),u("error")}finally{setTimeout(()=>u(""),2e3)}}}),(0,e.createElement)("main",{className:"lcake-kit-main-content"},(0,e.createElement)(s,{allWidgets:d,onDrop:()=>S("available"),onDragOver:N,onDragLeaveContainer:O,onDragStart:h,dragOverWidgetId:w}),(0,e.createElement)(o,{enabledWidgets:g,onDrop:()=>S("enabled"),onDragOver:N,onDragLeaveContainer:O,onDragStart:h,dragOverWidgetId:w})))},null),document.getElementById("lcake-kit-react-root"))})();