import{w as f,M as _,b as j,r as l,j as e,O as h,B as d,W as w,C as k,a as p,a8 as C,aA as y,R as N,c2 as v,c3 as I,c4 as S}from"./index-DtyjNqrf.js";import{I as q}from"./InputField-CYbmpJqW.js";import{g as z}from"./index-Q3KOJDn9.js";import"./envs-DFlyG_dH.js";const A=f.create(r=>{const{addInstallCode:s}=r,o=_(),{t:a}=j(["settings","common"]),[c,m]=l.useState("");return e.jsx(h,{isOpen:o.visible,title:a("add_install_code"),footer:e.jsxs(e.Fragment,{children:[e.jsx(d,{className:"btn btn-neutral",onClick:o.remove,children:a("common:cancel")}),e.jsx(d,{className:"btn btn-primary ms-1",onClick:async()=>{c&&(o.remove(),await s(c))},children:a("add_install_code")})]}),children:e.jsx("div",{className:"flex flex-col gap-2",children:e.jsx(q,{name:"install_code",label:a("install_code"),onChange:i=>m(i.target.value),value:c,type:"text",required:!0})})})});async function B(r){return await new Promise((s,o)=>{const a=new FileReader;a.onloadend=()=>s(a.result),a.onerror=c=>o(c),a.readAsDataURL(r)})}async function M(r){const s=await fetch(r);if(s.ok){const o=await s.blob();return await B(o)}return Promise.reject(s.status)}function D(r){const[s,o]=l.useState("none"),{devices:a}=r,[c,m]=l.useState({}),{sendMessage:i}=l.useContext(w),{t:u}=j("settings"),t=l.useCallback(async n=>{m(x=>({...x,[n.ieee_address]:"init"}));const b=z(n),g=await M(b);return await i("bridge/request/device/options",{id:n.ieee_address,options:{icon:g}}),m(x=>({...x,[n.ieee_address]:"done"})),!0},[i]);switch(l.useEffect(()=>{if(s==="start"){for(const n of a)n.type!=="Coordinator"&&t(n).catch(b=>{console.error("Error localising image",b),m(g=>({...g,[n.ieee_address]:"error"}))}).then();o("inprogress")}},[s,a,t]),s){case"none":return e.jsx(d,{className:"btn btn-primary join-item",onClick:()=>o("start"),children:u("localise_images")});case"inprogress":return e.jsx("ul",{className:"menu menu-xs bg-base-200 max-w-xs w-full join-item",children:a.map(n=>e.jsxs("li",{className:"flex-row justify-between items-center border-b py-0.5",children:[n.friendly_name,e.jsx("span",{className:"badge badge-xs",children:u(`common:${c[n.ieee_address]}`)})]},n.ieee_address))});case"done":return e.jsx("div",{children:u("common:unknown")})}return e.jsx("div",{children:u("zigbee:unknown")})}function $(){const r=k(),{sendMessage:s}=l.useContext(w),o=p(t=>t.bridgeInfo),a=p(t=>t.backup),c=p(t=>t.preparingBackup),m=p(t=>t.devices),{t:i}=j(["settings","common"]),u=l.useCallback(()=>{const t=C(new Date).replace(/[\s_:]/g,"-"),n=`z2m-backup.${o.version}.${t}.zip`;y(`data:application/zip;base64,${a}`,n)},[a,o.version]);return e.jsxs("div",{className:"join join-vertical",children:[e.jsx(N,{className:"btn btn-error join-item mb-2",onClick:async()=>await s("bridge/request/restart",""),title:i("restart_zigbee2mqtt"),modalDescription:i("common:dialog_confirmation_prompt"),modalCancelLabel:i("common:cancel"),children:i("restart_zigbee2mqtt")}),e.jsx(d,{className:"btn btn-primary join-item",onClick:async()=>await v(I.getState(),"state.json"),children:i("download_state")}),c?e.jsx(d,{className:"btn btn-primary join-item disabled",children:e.jsx("span",{className:"loading loading-dots loading-xl"})}):a?e.jsx(d,{className:"btn btn-primary join-item",onClick:u,children:i("download_z2m_backup")}):e.jsx(d,{className:"btn btn-primary join-item",onClick:async()=>{r(S()),await s("bridge/request/backup","")},children:i("request_z2m_backup")}),e.jsx(d,{className:"btn btn-primary join-item",onClick:async()=>await f.show(A,{addInstallCode:async t=>await s("bridge/request/install_code/add",{value:t})}),children:i("add_install_code")}),e.jsx(D,{devices:m})]})}export{$ as default};
