import{j as e,u as g,L as h,F as b,be as j,bf as _,a9 as N,a0 as C,r as l,D as v,ab as w,s as f,B as S,a as G,I as k}from"./index-CE-9RlbR.js";import{u as E,g as y}from"./useColumnCount-CdMiuwHk.js";import{E as M}from"./EndpointPicker-WNk50fpC.js";import{S as A}from"./react-select.esm-CxImRSNQ.js";import"./envs-CRUy-_iG.js";import"./SelectField-BPXpeItQ.js";import"./floating-ui.dom-D02yrJtw.js";const L=({sourceIdx:o,group:s,endpoint:d,removeFromGroup:i})=>{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(h,{to:`/group/${o}/${s.id}/devices`,className:"link link-hover font-semibold",children:["#",s.id," - ",s.friendly_name,d?` (${n(a=>a.endpoint,{ns:"zigbee"})}: ${d})`:""]}),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 cursor-default me-2 tooltip","data-tip":n(a=>a.group_members),children:[e.jsx(b,{icon:j})," ",s.members.length]}),e.jsxs("div",{className:"badge badge-soft cursor-default me-2 tooltip","data-tip":n(a=>a.group_scenes),children:[e.jsx(b,{icon:_}),s.scenes.length]}),e.jsx(N,{item:[s,d],onClick:i,className:"btn btn-square btn-error btn-sm",title:n(a=>a.remove_from_group),modalDescription:n(a=>a.dialog_confirmation_prompt,{ns:"common"}),modalCancelLabel:n(a=>a.cancel,{ns:"common"}),children:e.jsx(b,{icon:C})})]})]})},T=o=>o?.data?e.jsx(L,{...o.data}):null,$=l.memo(({groups:o,label:s,required:d,disabled:i,onChange:n})=>{const{t:a}=g("common"),u=l.useCallback(r=>{if(r){const m=Number.parseInt(r.value,10);n(o.find(p=>m===p.id))}},[o,n]),c=l.useMemo(()=>o.map(r=>({value:`${r.id}`,label:r.friendly_name})).sort((r,m)=>r.label.localeCompare(m.label)),[o]);return e.jsxs("fieldset",{className:"fieldset",children:[s&&e.jsxs("legend",{className:"fieldset-legend",children:[s,d?" *":""]}),e.jsx(A,{unstyled:!0,placeholder:a(r=>r.select_group),"aria-label":s??a(r=>r.select_group),options:c,isSearchable:!0,isDisabled:i,onChange:u,className:"min-w-64",classNames:v})]})}),F=l.memo(({sourceIdx:o,device:s,nonMemberGroups:d})=>{const{t:i}=g(["groups","zigbee"]),[n,a]=l.useState(""),[u,c]=l.useState(""),r=l.useMemo(()=>w(s),[s]),m=l.useCallback(t=>{c(t.id);const x=r.values().next().value;a(x??"")},[r]),p=l.useCallback(async()=>await f(o,"bridge/request/group/members/add",{group:u.toString(),endpoint:n,device:s.ieee_address}),[o,u,s.ieee_address,n]);return e.jsxs(e.Fragment,{children:[e.jsx("h2",{className:"text-lg font-semibold",children:i(t=>t.add_to_group)}),e.jsxs("div",{className:"flex flex-row flex-wrap mb-3 gap-3",children:[e.jsx($,{label:i(t=>t.group,{ns:"zigbee"}),groups:d,onChange:m,required:!0}),e.jsx(M,{label:i(t=>t.endpoint,{ns:"zigbee"}),values:r,value:n,onChange:t=>a(t),required:!0})]}),e.jsx(S,{onClick:p,className:"btn btn-primary",disabled:n==null||u==null||n===""||u==="",children:i(t=>t.add_to_group)})]})});function W({sourceIdx:o,device:s}){const d=G(k(c=>c.groups[o])),i=E(),n=l.useCallback(async([c,r])=>await f(o,"bridge/request/group/members/remove",{device:s.ieee_address,endpoint:r,group:c.id.toString()}),[o,s.ieee_address]),[a,u]=l.useMemo(()=>{const c=[],r=[];for(const m of d){const p=m.members.filter(t=>t.ieee_address===s.ieee_address);if(p.length>0)for(const t of p)c.push({sourceIdx:o,group:m,endpoint:t.endpoint,removeFromGroup:n});else r.push(m)}return[c,r]},[o,d,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(F,{sourceIdx:o,device:s,nonMemberGroups:u})})})}),e.jsx("div",{children:e.jsx(y,{useWindowScroll:!0,columnCount:i,data:a,ItemContent:T,className:"gap-3"},`membergroups-${a.length}`)})]})}export{W as default};
