import{r as n,b as j,ah as R,j as t,B as N,R as k,W as w}from"./index-BeDhuS24.js";import{I as C}from"./InputField-E2g5qJCn.js";import{C as E,A as y}from"./ClusterSinglePicker-BavLYJxh.js";import{E as S}from"./EndpointPicker-Cni8eLro.js";import"./envs-DFlyG_dH.js";import"./SelectField-CgXiODUe.js";const q=["maximum_report_interval","minimum_report_interval","reportable_change","endpoint","cluster","attribute"],A=n.memo(l=>{const{rule:r,device:o,onApply:a}=l,[e,u]=n.useState({rule:r}),{t:s}=j(["zigbee","common"]);n.useEffect(()=>{u({rule:r})},[r]);const c=n.useCallback(i=>{e.rule.endpoint=i,u({rule:e.rule})},[e]),_=n.useCallback(i=>{e.rule.cluster=i,u({rule:e.rule})},[e]),g=n.useCallback(i=>{e.rule.attribute=i,u({rule:e.rule})},[e]),p=n.useCallback(i=>{const{name:m,valueAsNumber:d}=i.target;e.rule[m]=d,u({rule:e.rule})},[e]),v=n.useCallback(()=>{a({...e.rule,maximum_report_interval:65535})},[e,a]),x=n.useMemo(()=>R(o),[o]),f=n.useMemo(()=>{const i=new Set,m=new Set;e.rule.cluster&&m.add(e.rule.cluster);const d=o.endpoints[Number.parseInt(e.rule.endpoint,10)];if(d){for(const b of d.clusters.output)m.add(b);for(const b of d.clusters.input)m.has(b)||i.add(b)}return[{name:"available",clusters:m},{name:"possible",clusters:i}]},[o.endpoints,e.rule.endpoint,e.rule.cluster]),h=n.useMemo(()=>q.every(i=>e.rule[i]!==void 0&&e.rule[i]!==""),[e]);return t.jsxs(t.Fragment,{children:[t.jsxs("div",{className:"flex flex-row flex-wrap gap-2",children:[t.jsx(S,{label:s("endpoint"),disabled:!r.isNew,values:x,value:e.rule.endpoint,onChange:c,required:!0}),t.jsx(E,{label:s("cluster"),disabled:!e.rule.endpoint,clusters:f,value:e.rule.cluster,onChange:_,required:!0}),t.jsx(y,{label:s("attribute"),disabled:!e.rule.cluster,value:e.rule.attribute,cluster:e.rule.cluster,device:o,onChange:g,required:!0}),t.jsx(C,{name:"minimum_report_interval",label:s("min_rep_interval"),type:"number",value:e.rule.minimum_report_interval,onChange:p,required:!0}),t.jsx(C,{name:"maximum_report_interval",label:s("max_rep_interval"),type:"number",value:e.rule.maximum_report_interval,onChange:p,required:!0}),t.jsx(C,{name:"reportable_change",label:s("min_rep_change"),type:"number",value:e.rule.reportable_change,onChange:p,required:!0}),t.jsxs("fieldset",{className:"fieldset",children:[t.jsx("legend",{className:"fieldset-legend",children:s("actions")}),t.jsxs("div",{className:"join join-horizontal",children:[t.jsx(N,{className:"btn btn-primary join-item",item:e.rule,onClick:a,disabled:!h,children:s("common:apply")}),e.rule.isNew?null:t.jsx(k,{title:s("common:disable"),className:"btn btn-error join-item",onClick:v,modalDescription:s("common:dialog_confirmation_prompt"),modalCancelLabel:s("common:cancel"),children:s("common:disable")})]})]})]}),t.jsx("div",{className:"divider"})]})}),I=l=>{const r=[];for(const o in l.endpoints){const a=l.endpoints[o];for(const e of a.configured_reportings)r.push({...e,endpoint:o})}return r},M=l=>`${l.isNew}-${l.endpoint}-${l.cluster}-${l.attribute}`;function z(l){const{device:r}=l,{sendMessage:o}=n.useContext(w),[a,e]=n.useState({isNew:r.ieee_address,reportable_change:0,minimum_report_interval:60,maximum_report_interval:3600,endpoint:"",cluster:"",attribute:""}),u=n.useMemo(()=>I(r),[r]);n.useEffect(()=>{e({isNew:r.ieee_address,reportable_change:0,minimum_report_interval:60,maximum_report_interval:3600,endpoint:"",cluster:"",attribute:""})},[r.ieee_address]);const s=n.useCallback(async c=>{const{cluster:_,endpoint:g,attribute:p,minimum_report_interval:v,maximum_report_interval:x,reportable_change:f}=c;await o("bridge/request/device/configure_reporting",{id:r.ieee_address,endpoint:g,cluster:_,attribute:p,minimum_report_interval:v,maximum_report_interval:x,reportable_change:f,option:{}})},[o,r.ieee_address]);return t.jsx("div",{className:"flex flex-col",children:[...u,a].map(c=>t.jsx(A,{rule:c,device:r,onApply:s},M(c)))})}export{z as default};
