import{u as f,j as e,K as y,r as b,B as j}from"./index-fb4a4614.js";import{i as S,g as _,T as C,D as F}from"./DashboardFeatureWrapper-ef543cb2.js";function R(s){const{value:n,values:a,disabled:m,onChange:t,label:l,...c}=s,{t:p}=f("common"),o=r=>{t(r.target.value)},i=a.length==1,u=a.map(r=>e.jsx("option",{value:r,children:r},r));return u.unshift(e.jsx("option",{hidden:!0,children:p("select_endpoint")},"hided")),e.jsxs("div",{className:"form-group",children:[l&&e.jsx("label",{className:"form-label",children:l}),e.jsx("select",{disabled:n&&i||m,value:n,className:"form-control",title:i?p("the_only_endpoint"):"",onChange:o,...c,children:u})]})}function I(s){const{t:n}=f("common"),{devices:a,value:m,label:t,onChange:l,groups:c=[],...p}=s,o=r=>{const{value:d}=r.target;if(a[d])l(a[d],"device");else{const x=c.find(h=>parseInt(d,10)===h.id);l(x,"group")}};let i=[e.jsx("option",{hidden:!0,children:n("select_device")},"hidden")];const u=[];if(Object.values(a).sort((r,d)=>r.friendly_name.localeCompare(d.friendly_name)).forEach(r=>{var d;u.push(e.jsx("option",{title:(d=r.definition)==null?void 0:d.description,value:r.ieee_address,children:y(r)},r.ieee_address))}),c&&c.length){const r=c.map(d=>e.jsx("option",{value:d.id,children:d.friendly_name},d.friendly_name));i.push(e.jsx("optgroup",{label:n("groups"),children:r},"Groups")),i.push(e.jsx("optgroup",{label:n("devices"),children:u},"Devices"))}else i=i.concat(u);return e.jsxs("div",{className:"form-group",children:[t&&e.jsx("label",{className:"form-label",children:t}),e.jsx("select",{value:m,onChange:o,className:"form-control",...p,children:i})]})}const k=["state","color_temp","color","transition","brightness"];function v(s,n={}){let{property:a,name:m,features:t}=s;if(S(s)&&(t=t.map(l=>v(l,a?n[a]:n)).filter(l=>l),t=Object.values(_(t,"property")).map(l=>l[0])),k.includes(m)||Array.isArray(t)&&t.length>0)return{...s,features:t}}function g(s){if(s.endpoints){const n=[];return Object.entries(s.endpoints).forEach(([a,m])=>{var t;(t=m.scenes)==null||t.forEach(l=>n.push({...l,endpoint:a}))}),n}else if(s.scenes)return s.scenes;return[]}const D=(s,n=[])=>s>=0&&s<=255&&!n.find(a=>a.id==s);function P(s){var x;const{target:n,deviceState:a,sceneStore:m,setDeviceState:t}=s,l=g(n),{t:c}=f("scene"),[p,o]=b.useState(0),[i,u]=b.useState(""),r=`Scene ${p}`;let d=[];return n.definition&&(d=(((x=n.definition)==null?void 0:x.exposes)??[]).map(h=>v(h,a)).filter(h=>h)),e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{htmlFor:"add-scene",className:"form-label",children:c("scene_id")}),e.jsx("input",{id:"add-scene",className:"form-control",min:0,max:255,value:p,type:"number",onChange:h=>o(h.target.valueAsNumber)}),e.jsx("label",{htmlFor:"add-scene-name",className:"form-label",children:c("scene_name")}),e.jsx("input",{id:"add-scene-name",className:"form-control",value:i,type:"string",onChange:h=>u(h.target.value),placeholder:r}),e.jsx(C,{feature:{features:d},className:"row",type:"composite",device:n,deviceState:a,onChange:(h,N)=>{t(n.friendly_name,N)},featureWrapperClass:F,minimal:!0})]}),e.jsx("div",{className:"d-flex",children:e.jsx("button",{disabled:!D(p,l),type:"submit",onClick:()=>m(n.friendly_name,{id:p,name:i||r}),className:"btn btn-primary ms-auto",children:c("store")})})]})}function E(s){const{t:n}=f("scene"),{onSceneSelected:a,scenes:m=[],value:t}=s,l=o=>{const[i,u]=o.target.value.split("-");a({id:parseInt(i,10),endpoint:u})},c=e.jsxs(e.Fragment,{children:[e.jsx("label",{htmlFor:"rr-scene",className:"form-label",children:n("scene_name")}),e.jsxs("select",{onChange:l,id:"rr-scene",className:"form-select",children:[e.jsx("option",{hidden:!0,children:n("select_scene")},"hidden"),m.map(o=>e.jsx("option",{value:`${o.id}-${o.endpoint}`,children:o.name},`${o.id}-${o.endpoint}`))]})]}),p=e.jsxs(e.Fragment,{children:[e.jsx("label",{htmlFor:"rr-scene",className:"form-label",children:n("scene_id")}),e.jsx("input",{min:0,value:t.id,type:"number",className:"form-control",id:"rr-scene",onChange:o=>a({id:o.target.valueAsNumber,endpoint:void 0})})]});return e.jsx(e.Fragment,{children:m.length>0?c:p})}function $(s){const{sceneRecall:n,sceneRemove:a,sceneRemoveAll:m,target:t}=s,{t:l}=f("scene"),[c,p]=b.useState({id:0,endpoint:void 0}),o=c.id===void 0,i=g(t),{friendly_name:u}=t;return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"mb-3",children:e.jsx(E,{onSceneSelected:p,value:c,scenes:i})}),e.jsxs("div",{className:"d-flex",children:[e.jsxs("div",{className:"btn-group ms-auto pe-1",children:[e.jsx("button",{disabled:o,onClick:()=>n(u,c.id,c.endpoint),type:"submit",className:"btn btn-success",children:l("recall")}),e.jsx(j,{disabled:o,prompt:!0,onClick:()=>a(u,c.id,c.endpoint),type:"submit",className:"btn btn-danger",children:l("remove")})]}),e.jsx(j,{prompt:!0,onClick:()=>m(u,""),type:"submit",className:"btn btn-danger",children:l("remove_all")})]})]})}export{P as A,I as D,R as E,$ as R,v as o};
