import{u as A,a as y,E as N,r,s as x,j as e,a6 as C,F as h,am as B,a7 as P,B as $,f as O}from"./index-ClCsMWbS.js";import{u as q,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-BfzwIwuD.js";import{b as Q,g as V,m as W,B as k}from"./BindRow-Cs1sVcpM.js";import"./envs-CSqIi4cL.js";import"./DevicePicker-0T64KBIx.js";import"./SelectField-WiB6Edsu.js";import"./EndpointPicker-xjSURnYy.js";const X=n=>`${n.isNew}-${n.source.endpoint}-${n.source.ieee_address}-${"ieee_address"in n.target?n.target.ieee_address:n.target.id}-${n.clusters.join("-")}`,Y=r.memo(({endpointId:n,rules:t,devices:o,groups:d,sourceIdx:m,device:f,onApply:b})=>{const{t:c}=A(["common","zigbee","scene"]),s=r.useRef(null),[i,l]=r.useState(!1),[p,j]=r.useState(null),{refs:_,floatingStyles:R,context:g}=q({open:i,onOpenChange:l,placement:"bottom-end",middleware:[z(8),L({padding:16,crossAxis:!0}),M({element:s})]}),D=T(g,{event:"click"}),E=K(g),F=U(g),{getReferenceProps:S,getFloatingProps:v}=G([D,E,F]);r.useEffect(()=>{j(i?W(f.ieee_address,n):null)},[i,f.ieee_address,n]);const w=r.useCallback(async([a,u])=>{await b([a,u]),u.isNew&&l(!1)},[b]);return e.jsx("section",{className:"card bg-base-100 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(a=>a.source_endpoint,{ns:"common"})," ",n,e.jsxs("button",{ref:_.setReference,type:"button",className:"btn btn-primary btn-outline btn-sm",...S(),children:[e.jsx(h,{icon:P}),e.jsx("span",{children:c(a=>a.add,{ns:"common"})})]})]}),t.length>0?t.map((a,u)=>e.jsx(k,{rule:a,sourceIdx:m,groups:d,device:f,devices:o,showDivider:u<t.length-1,onApply:w},X(a))):e.jsx("p",{className:"text-sm opacity-70",children:c(a=>a.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-[80vh] 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(a=>a.add,{ns:"common"}),": ",c(a=>a.source_endpoint,{ns:"common"})," ",n]}),e.jsx($,{onClick:()=>l(!1),className:"btn btn-ghost btn-sm",children:e.jsx(h,{icon:O})})]}),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 os({sourceIdx:n,device:t}){const{t:o}=A("common"),d=y(N(s=>s.devices[n])),m=y(N(s=>s.groups[n])),f=r.useMemo(()=>Q(t),[t]),b=r.useCallback(async([s,i])=>{const l=V(i.target,d,m);if(!l)return;const p={from:t.ieee_address,from_endpoint:i.source.endpoint,to:l.to,to_endpoint:l.toEndpoint,clusters:i.clusters};s==="Bind"?await x(n,"bridge/request/device/bind",p):await x(n,"bridge/request/device/unbind",p)},[n,t,d,m]),c=r.useCallback(async([s,i,l])=>{await x(s,"bridge/request/device/binds/clear",{target:i,ieee_list:l})},[]);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 join-item",item:[n,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(h,{icon:B}),o(s=>s.clear_self_as_source)]}),e.jsxs(C,{className:"btn btn-outline btn-error join-item",item:[n,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(h,{icon:B}),o(s=>s.clear_all)]})]}),f.map(s=>e.jsx(Y,{...s,devices:d,groups:m,sourceIdx:n,device:t,onApply:b},s.endpointId))]})}export{os as default};
