import{r as d,u as _,j as o}from"./index-ClCsMWbS.js";import{S as j}from"./SelectField-WiB6Edsu.js";const b=d.memo(({devices:l,value:r,label:p,onChange:c,groups:i=[],...f})=>{const{t:a}=_("common"),[u,m]=d.useState("");d.useEffect(()=>{if(typeof r=="string"){const s=l.find(n=>n.ieee_address===r);m(s?.friendly_name??"")}else{const s=i.find(n=>r===n.id);m(s?.friendly_name??"")}},[r,l,i]);const h=s=>{const{value:n}=s.target;if(n.startsWith("0x"))c(l.find(e=>e.ieee_address===n));else{const e=Number.parseInt(n,10);c(i.find(t=>e===t.id))}},x=d.useMemo(()=>{const s=[],n=l.map(e=>o.jsxs("option",{title:e.definition?.description,value:e.ieee_address,children:[e.friendly_name," ",e.definition?.model?`(${e.definition?.model})`:""]},e.friendly_name)).sort((e,t)=>e.key.localeCompare(t.key));if(i?.length){const e=i.map(t=>o.jsx("option",{value:t.id,children:t.friendly_name},t.friendly_name)).sort((t,y)=>t.key.localeCompare(y.key));s.push(o.jsx("optgroup",{label:a(t=>t.groups),children:e},"Groups")),s.push(o.jsx("optgroup",{label:a(t=>t.devices),children:n},"Devices"))}else s.push(...n);return s},[l,i,a]);return o.jsxs(j,{name:"device_picker",label:p,value:r,title:u,onChange:h,className:"select validator w-96",...f,children:[o.jsx("option",{value:"",disabled:!0,children:a(s=>s.select_device)}),x]})});export{b as D};
