import{u as A,a as y,D as N,r as l,s as h,j as e,a5 as C,F as x,an as B,a6 as P,B as $,f as O}from"./index-KAtljJVt.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-Dfxk0AMB.js";import{b as Q,g as V,m as W,B as k}from"./BindRow-Cbpys0Vm.js";import"./envs-CSqIi4cL.js";import"./floating-ui.dom-I8hrORoS.js";import"./react-select.esm-DL84IisZ.js";import"./defineProperty-CegpTSss.js";import"./extends-CF3RwP-h.js";import"./DevicePicker-D5lrfSSh.js";import"./EndpointPicker-Cjgep8ml.js";import"./SelectField-wRS87yKs.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=l.memo(({endpointId:n,rules:t,devices:o,groups:m,sourceIdx:d,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:D,context:g}=q({open:i,onOpenChange:r,placement:"bottom-end",middleware:[z(8),L({padding:16,crossAxis:!0}),M({element:s})],strategy:"fixed",transform:!1}),R=T(g,{event:"click"}),F=K(g),E=U(g),{getReferenceProps:S,getFloatingProps:v}=G([R,F,E]);l.useEffect(()=>{j(i?W(f.ieee_address,n):null)},[i,f.ieee_address,n]);const w=l.useCallback(async([a,u])=>{await b([a,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(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(x,{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:d,groups:m,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:D,...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:()=>r(!1),className:"btn btn-ghost btn-sm",children:e.jsx(x,{icon:O})})]}),e.jsx(k,{rule:p,sourceIdx:d,groups:m,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 cs({sourceIdx:n,device:t}){const{t:o}=A("common"),m=y(N(s=>s.devices[n])),d=y(N(s=>s.groups[n])),f=l.useMemo(()=>Q(t),[t]),b=l.useCallback(async([s,i])=>{const r=V(i.target,m,d);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(n,"bridge/request/device/bind",p):await h(n,"bridge/request/device/unbind",p)},[n,t,m,d]),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 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(x,{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(x,{icon:B}),o(s=>s.clear_all)]})]}),f.map(s=>e.jsx(Y,{...s,devices:m,groups:d,sourceIdx:n,device:t,onApply:b},s.endpointId))]})}export{cs as default};
