import{a1 as k,a6 as _,u as f,r as m,j as n,a7 as y,B as l,s as g,a as u,I as p,bo as h,a9 as x,dA as C}from"./index-a36IMXuN.js";import{I as N}from"./InputField-IMyvy6Mz.js";import{g as v}from"./index-CsCgDwc5.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 a=d=>{d.key==="Escape"&&(d.preventDefault(),i.remove())};return window.addEventListener("keydown",a),()=>window.removeEventListener("keydown",a)},[i]),n.jsx(y,{isOpen:i.visible,title:s(a=>a.add_install_code),footer:n.jsxs(n.Fragment,{children:[n.jsx(l,{className:"btn btn-neutral",onClick:i.remove,children:s(a=>a.cancel,{ns:"common"})}),n.jsx(l,{className:"btn btn-primary ms-1",onClick:async()=>{r&&(i.remove(),await o(r))},children:s(a=>a.add_install_code)})]}),children:n.jsx("div",{className:"flex flex-col gap-2",children:n.jsx(N,{name:"install_code",label:s(a=>a.install_code),onChange:a=>c(a.target.value),value:r,type:"text",required:!0})})})});async function S(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 B(t){const o=await fetch(t);if(o.ok){const i=await o.blob();return await S(i)}return Promise.reject(o.status)}function q({sourceIdx:t,devices:o}){const[i,s]=m.useState("none"),[r,c]=m.useState({}),{t:a}=f(["settings","common","zigbee"]),d=m.useCallback(async e=>{c(w=>({...w,[e.ieee_address]:"init"}));const b=v(e),j=await B(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 n.jsx(l,{className:"btn btn-outline btn-primary join-item",onClick:()=>s("start"),children:a(e=>e.localise_images)});case"inprogress":return n.jsx("ul",{className:"menu menu-xs bg-base-200 max-w-xs w-full join-item",children:o.map(e=>n.jsxs("li",{className:"flex-row justify-between items-center border-b py-0.5",children:[e.friendly_name,n.jsx("span",{className:"badge badge-xs",children:a(b=>b[r[e.ieee_address]],{ns:"common"})})]},`${t}-${e.ieee_address}-${e.friendly_name}`))});case"done":return n.jsx("div",{children:a(e=>e.unknown,{ns:"zigbee"})})}return n.jsx("div",{children:a(e=>e.unknown,{ns:"zigbee"})})}function $({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:a}=f(["settings","common"]),d=m.useCallback(()=>{if(s){const e=`z2m-backup.${i.version}.${Date.now()}.zip`;h(`data:application/zip;base64,${s}`,e)}},[s,i.version]);return n.jsxs("div",{className:"join join-vertical",children:[n.jsx(x,{className:"btn btn-outline btn-error join-item mb-2",onClick:async()=>await g(t,"bridge/request/restart",""),title:a(e=>e.restart_zigbee2mqtt),modalDescription:a(e=>e.dialog_confirmation_prompt,{ns:"common"}),modalCancelLabel:a(e=>e.cancel,{ns:"common"}),children:a(e=>e.restart_zigbee2mqtt)}),n.jsx(l,{className:"btn btn-outline btn-primary join-item",onClick:async()=>await C(u.getState(),"state.json"),children:a(e=>e.download_state)}),r?n.jsx(l,{className:"btn btn-primary join-item disabled",children:n.jsx("span",{className:"loading loading-dots loading-xl"})}):s?n.jsx(l,{className:"btn btn-outline btn-primary join-item",onClick:d,children:a(e=>e.download_z2m_backup)}):n.jsx(l,{className:"btn btn-outline btn-primary join-item",onClick:async()=>{o(t),await g(t,"bridge/request/backup","")},children:a(e=>e.request_z2m_backup)}),n.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:a(e=>e.add_install_code)}),n.jsx(q,{sourceIdx:t,devices:c})]})}export{$ as default};
