import{j as e,u as g,L as f,F as b,a$ as h,b0 as j,a6 as _,Z as v,r as l,a8 as C,s as x,B as N,a as k,E as w}from"./index-ClCsMWbS.js";import{u as G,b as y}from"./useColumnCount-BIzxc4Aw.js";import{E as S}from"./EndpointPicker-xjSURnYy.js";import{S as M}from"./SelectField-WiB6Edsu.js";import"./envs-CSqIi4cL.js";const E=({sourceIdx:a,group:s,endpoint:i,removeFromGroup:d})=>{const{t:n}=g(["groups","common","zigbee"]);return e.jsxs("div",{className:"mb-3 card card-border bg-base-200 border-base-300 rounded-box shadow-md",children:[e.jsx("div",{className:"card-body p-3",children:e.jsx("div",{className:"flex flex-row items-center gap-3 w-full",children:e.jsxs("div",{className:"min-w-0",children:[e.jsxs(f,{to:`/group/${a}/${s.id}/devices`,className:"link link-hover font-semibold",children:["#",s.id," - ",s.friendly_name,i?` (${n(o=>o.endpoint,{ns:"zigbee"})}: ${i})`:""]}),s.description&&e.jsx("div",{className:"text-xs opacity-50 truncate",title:s.description,children:s.description})]})})}),e.jsxs("div",{className:"flex flex-row flex-wrap gap-1 mx-2 mb-2 justify-around items-center",children:[e.jsxs("div",{className:"badge badge-soft badge-ghost cursor-default me-2 tooltip","data-tip":n(o=>o.group_members),children:[e.jsx(b,{icon:h})," ",s.members.length]}),e.jsxs("div",{className:"badge badge-soft badge-ghost cursor-default me-2 tooltip","data-tip":n(o=>o.group_scenes),children:[e.jsx(b,{icon:j}),s.scenes.length]}),e.jsx(_,{item:[s,i],onClick:d,className:"btn btn-square btn-error btn-sm",title:n(o=>o.remove_from_group),modalDescription:n(o=>o.dialog_confirmation_prompt,{ns:"common"}),modalCancelLabel:n(o=>o.cancel,{ns:"common"}),children:e.jsx(b,{icon:v})})]})]})},F=a=>a?.data?e.jsx(E,{...a.data}):null,$=l.memo(({groups:a,value:s,label:i,onChange:d,...n})=>{const{t:o}=g("common"),m=l.useCallback(r=>{const{value:u}=r.target,p=Number.parseInt(u,10);d(a.find(t=>p===t.id))},[a,d]),c=l.useMemo(()=>a.map(r=>e.jsx("option",{value:r.id,children:r.friendly_name},r.friendly_name)).sort((r,u)=>r.key.localeCompare(u.key)),[a]);return e.jsxs(M,{name:"group_picker",label:i,value:s,onChange:m,...n,children:[e.jsx("option",{value:"",disabled:!0,children:o(r=>r.select_group)}),c]})}),q=l.memo(({sourceIdx:a,device:s,nonMemberGroups:i})=>{const{t:d}=g(["groups","zigbee"]),[n,o]=l.useState(""),[m,c]=l.useState(""),r=l.useMemo(()=>C(s),[s]),u=l.useCallback(t=>{c(t.id),o(r.values().next().value)},[r]),p=l.useCallback(async()=>await x(a,"bridge/request/group/members/add",{group:m.toString(),endpoint:n,device:s.ieee_address}),[a,m,s.ieee_address,n]);return e.jsxs(e.Fragment,{children:[e.jsx("h2",{className:"text-lg font-semibold",children:d(t=>t.add_to_group)}),e.jsxs("div",{className:"mb-3",children:[e.jsx($,{label:d(t=>t.group,{ns:"zigbee"}),value:m,groups:i,onChange:u,required:!0}),e.jsx(S,{label:d(t=>t.endpoint,{ns:"zigbee"}),values:r,value:n,onChange:t=>o(t),required:!0})]}),e.jsx(N,{onClick:p,className:"btn btn-primary",disabled:n==null||m==null||n===""||m==="",children:d(t=>t.add_to_group)})]})});function D({sourceIdx:a,device:s}){const i=k(w(c=>c.groups[a])),d=G(),n=l.useCallback(async([c,r])=>await x(a,"bridge/request/group/members/remove",{device:s.ieee_address,endpoint:r,group:c.id.toString()}),[a,s.ieee_address]),[o,m]=l.useMemo(()=>{const c=[],r=[];for(const u of i){const p=u.members.filter(t=>t.ieee_address===s.ieee_address);if(p.length>0)for(const t of p)c.push({sourceIdx:a,group:u,endpoint:t.endpoint,removeFromGroup:n});else r.push(u)}return[c,r]},[a,i,s.ieee_address,n]);return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex flex-row flex-wrap gap-4 mb-3 w-full",children:e.jsx("div",{className:"card card-border bg-base-200 border-base-300 rounded-box shadow-md flex-1",children:e.jsx("div",{className:"card-body p-4",children:e.jsx(q,{sourceIdx:a,device:s,nonMemberGroups:m})})})}),e.jsx("div",{children:e.jsx(y,{useWindowScroll:!0,columnCount:d,data:o,ItemContent:F,className:"gap-3"},`membergroups-${o.length}`)})]})}export{D as default};
