import{r,b as _,j as e,a as w,W as C,ae as N,B as k,L as y,I as G,F as S,x as I}from"./index-vDL8KyrI.js";import{E as M}from"./EndpointPicker-DOVgHugJ.js";import{S as E}from"./SelectField-apZm61cq.js";const q=r.memo(t=>{const{t:o}=_("common"),{groups:a,value:c,label:l,onChange:m,...i}=t,g=r.useCallback(u=>{const{value:d}=u.target,x=Number.parseInt(d,10);m(a.find(b=>x===b.id))},[a,m]),p=r.useMemo(()=>{const u=[];for(const d of a)u.push(e.jsx("option",{value:d.id,children:d.friendly_name},d.id));return u},[a]);return e.jsxs(E,{name:"group_picker",label:l,value:c,onChange:g,...i,children:[e.jsx("option",{value:"",disabled:!0,children:o("select_group")}),p]})});function A({device:t}){const{t:o}=_(["groups","zigbee","common"]),a=w(s=>s.groups),{sendMessage:c}=r.useContext(C),[l,m]=r.useState(""),[i,g]=r.useState(""),p=r.useMemo(()=>N(t),[t]),[u,d]=r.useMemo(()=>{const s=[],n=[];for(const f of a){const j=f.members.filter(h=>h.ieee_address===t.ieee_address);if(j.length>0)for(const h of j)s.push([f,h.endpoint]);else n.push(f)}return[s,n]},[a,t.ieee_address]),x=r.useCallback(s=>{g(s.id),m(p.values().next().value)},[p]),b=r.useCallback(async()=>await c("bridge/request/group/members/add",{group:i.toString(),endpoint:l,device:t.ieee_address}),[c,i,t.ieee_address,l]),v=r.useCallback(async([s,n])=>await c("bridge/request/group/members/remove",{device:t.ieee_address,endpoint:n,group:s.id.toString()}),[c,t.ieee_address]);return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex flex-row flex-wrap justify-around items-start gap-3 my-2",children:e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:o("add_to_group")}),e.jsxs("div",{className:"mb-3",children:[e.jsx(q,{label:o("zigbee:group"),value:i,groups:d,onChange:x,required:!0}),e.jsx(M,{label:o("zigbee:endpoint"),values:p,value:l,onChange:s=>m(s),required:!0})]}),e.jsx(k,{onClick:b,className:"btn btn-primary",disabled:l==null||i==null||l===""||i==="",children:o("add_to_group")})]})}),e.jsx("div",{className:"flex flex-row flex-wrap justify-between items-stretch gap-3 mt-3",children:u.map(([s,n])=>e.jsxs("ul",{className:"flex-auto basis-sm list rounded-box shadow-md",children:[e.jsxs("li",{className:"list-row grow",children:[e.jsxs("div",{children:[e.jsxs(y,{to:`/group/${s.id}/devices`,className:"link link-hover",children:["#",s.id," - ",s.friendly_name,n?` (${o("endpoint")}: ${n})`:""]}),e.jsx("div",{className:"text-xs opacity-50",children:s.description||""})]}),e.jsxs("div",{className:"list-col-wrap text-sm w-full gap-2",children:[e.jsxs("div",{className:"badge badge-soft badge-ghost cursor-default me-2",children:[o("group_members"),": ",s.members.length]}),e.jsxs("div",{className:"badge badge-soft badge-ghost cursor-default me-2",children:[o("group_scenes"),": ",s.scenes.length]})]})]}),e.jsx("li",{className:"flex flex-row flex-wrap gap-1 m-4 justify-around items-center",children:e.jsx(G,{item:[s,n],onClick:v,className:"btn btn-square btn-error btn-sm",title:o("remove_from_group"),modalDescription:o("common:dialog_confirmation_prompt"),modalCancelLabel:o("common:cancel"),children:e.jsx(S,{icon:I})})})]},`${s.id}-${n}`))})]})}export{A as default};
