import{r as t,b as _,j as e,a as w,W as C,ah as N,B as k,L as y,R as G,F as S,x as M}from"./index-DtyjNqrf.js";import{E}from"./EndpointPicker-Bik90BnT.js";import{S as I}from"./SelectField-DfdnxD7O.js";import"./envs-DFlyG_dH.js";const q=t.memo(o=>{const{t:r}=_("common"),{groups:a,value:c,label:l,onChange:m,...i}=o,g=t.useCallback(u=>{const{value:d}=u.target,x=Number.parseInt(d,10);m(a.find(b=>x===b.id))},[a,m]),p=t.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(I,{name:"group_picker",label:l,value:c,onChange:g,...i,children:[e.jsx("option",{value:"",disabled:!0,children:r("select_group")}),p]})});function B({device:o}){const{t:r}=_(["groups","zigbee","common"]),a=w(s=>s.groups),{sendMessage:c}=t.useContext(C),[l,m]=t.useState(""),[i,g]=t.useState(""),p=t.useMemo(()=>N(o),[o]),[u,d]=t.useMemo(()=>{const s=[],n=[];for(const f of a){const j=f.members.filter(h=>h.ieee_address===o.ieee_address);if(j.length>0)for(const h of j)s.push([f,h.endpoint]);else n.push(f)}return[s,n]},[a,o.ieee_address]),x=t.useCallback(s=>{g(s.id),m(p.values().next().value)},[p]),b=t.useCallback(async()=>await c("bridge/request/group/members/add",{group:i.toString(),endpoint:l,device:o.ieee_address}),[c,i,o.ieee_address,l]),v=t.useCallback(async([s,n])=>await c("bridge/request/group/members/remove",{device:o.ieee_address,endpoint:n,group:s.id.toString()}),[c,o.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:r("add_to_group")}),e.jsxs("div",{className:"mb-3",children:[e.jsx(q,{label:r("zigbee:group"),value:i,groups:d,onChange:x,required:!0}),e.jsx(E,{label:r("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:r("add_to_group")})]})}),e.jsx("div",{className:"flex flex-row flex-wrap justify-center gap-3 mt-3",children:u.map(([s,n])=>e.jsxs("ul",{className:"w-[23rem] list bg-base-200 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?` (${r("endpoint")}: ${n})`:""]}),o.description&&e.jsx("div",{className:"text-xs opacity-50 truncate",title:o.description,children:o.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:[r("group_members"),": ",s.members.length]}),e.jsxs("div",{className:"badge badge-soft badge-ghost cursor-default me-2",children:[r("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:r("remove_from_group"),modalDescription:r("common:dialog_confirmation_prompt"),modalCancelLabel:r("common:cancel"),children:e.jsx(S,{icon:M})})})]},`${s.id}-${n}`))})]})}export{B as default};
