import{u as A,a as y,I as N,r as l,s as h,j as e,a9 as C,F as x,at as B,aa as P,B as $,f as q}from"./index-CE-9RlbR.js";import{u as O,o as z,s as L,b as M,g as T,d as K,c as U,e as G,F as H,f as J}from"./floating-ui.react-BJkbBPAO.js";import{b as Q,g as V,m as W,B as k}from"./BindRow-C4hlqyF0.js";import"./envs-CRUy-_iG.js";import"./floating-ui.dom-D02yrJtw.js";import"./react-select.esm-CxImRSNQ.js";import"./DevicePicker-D9pe3MS2.js";import"./EndpointPicker-WNk50fpC.js";import"./SelectField-BPXpeItQ.js";const X=a=>`${a.isNew}-${a.source.endpoint}-${a.source.ieee_address}-${"ieee_address"in a.target?a.target.ieee_address:a.target.id}-${a.clusters.join("-")}`,Y=l.memo(({endpointId:a,rules:t,devices:o,groups:d,sourceIdx:m,device:f,onApply:b})=>{const{t:c}=A(["common","zigbee","scene"]),s=l.useRef(null),[i,r]=l.useState(!1),[p,j]=l.useState(null),{refs:_,floatingStyles:R,context:g}=O({open:i,onOpenChange:r,placement:"bottom-end",middleware:[z(8),L({padding:16,crossAxis:!0}),M({element:s})],strategy:"fixed",transform:!1}),D=T(g,{event:"click"}),F=K(g),E=U(g),{getReferenceProps:S,getFloatingProps:v}=G([D,F,E]);l.useEffect(()=>{j(i?W(f.ieee_address,a):null)},[i,f.ieee_address,a]);const w=l.useCallback(async([n,u])=>{await b([n,u]),u.isNew&&r(!1)},[b]);return e.jsx("section",{className:"card bg-base-200 card-border border-base-200 shadow-sm",children:e.jsxs("div",{className:"card-body p-3",children:[e.jsxs("div",{className:"card-title flex flex-wrap gap-2 items-center justify-between",children:[c(n=>n.source_endpoint,{ns:"common"})," ",a,e.jsxs("button",{ref:_.setReference,type:"button",className:"btn btn-primary btn-outline btn-sm",...S(),children:[e.jsx(x,{icon:P}),e.jsx("span",{children:c(n=>n.add,{ns:"common"})})]})]}),t.length>0?t.map((n,u)=>e.jsx(k,{rule:n,sourceIdx:m,groups:d,device:f,devices:o,showDivider:u<t.length-1,onApply:w},X(n))):e.jsx("p",{className:"text-sm opacity-70",children:c(n=>n.none,{ns:"common"})}),i&&p?e.jsx(H,{children:e.jsxs("aside",{ref:_.setFloating,style:R,...v({className:"card bg-base-200 shadow-lg border border-base-300 w-[min(32rem,calc(100vw-2.5rem))] max-h-[90vh] z-11"}),children:[e.jsxs("div",{className:"card-body py-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h4",{className:"card-title",children:[c(n=>n.add,{ns:"common"}),": ",c(n=>n.source_endpoint,{ns:"common"})," ",a]}),e.jsx($,{onClick:()=>r(!1),className:"btn btn-ghost btn-square",children:e.jsx(x,{icon:q})})]}),e.jsx(k,{rule:p,sourceIdx:m,groups:d,device:f,devices:o,showDivider:!1,onApply:w})]}),e.jsx(J,{ref:s,context:g,className:"fill-base-200 [&>path:first-of-type]:stroke-base-300 [&>path:last-of-type]:stroke-base-300"})]})}):null]})})});function rs({sourceIdx:a,device:t}){const{t:o}=A("common"),d=y(N(s=>s.devices[a])),m=y(N(s=>s.groups[a])),f=l.useMemo(()=>Q(t),[t]),b=l.useCallback(async([s,i])=>{const r=V(i.target,d,m);if(!r)return;const p={from:t.ieee_address,from_endpoint:i.source.endpoint,to:r.to,to_endpoint:r.toEndpoint,clusters:i.clusters};s==="Bind"?await h(a,"bridge/request/device/bind",p):await h(a,"bridge/request/device/unbind",p)},[a,t,d,m]),c=l.useCallback(async([s,i,r])=>{await h(s,"bridge/request/device/binds/clear",{target:i,ieee_list:r})},[]);return e.jsxs("div",{className:"flex flex-col w-full gap-3",children:[e.jsxs("div",{className:"flex flex-row flex-wrap gap-3",children:[e.jsxs(C,{className:"btn btn-outline btn-error",item:[a,t.ieee_address,[t.ieee_address]],onClick:c,title:o(s=>s.clear_self_as_source),modalDescription:o(s=>s.dialog_confirmation_prompt,{ns:"common"}),modalCancelLabel:o(s=>s.cancel,{ns:"common"}),children:[e.jsx(x,{icon:B}),o(s=>s.clear_self_as_source)]}),e.jsxs(C,{className:"btn btn-outline btn-error",item:[a,t.ieee_address,["0xffffffffffffffff"]],onClick:c,title:o(s=>s.clear_all),modalDescription:o(s=>s.dialog_confirmation_prompt,{ns:"common"}),modalCancelLabel:o(s=>s.cancel,{ns:"common"}),children:[e.jsx(x,{icon:B}),o(s=>s.clear_all)]})]}),f.map(s=>e.jsx(Y,{...s,devices:d,groups:m,sourceIdx:a,device:t,onApply:b},s.endpointId))]})}export{rs as default};
