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