import{Y as k,a3 as _,u as f,r as m,j as n,a4 as h,B as l,s as g,a as u,E as p,bb as y,a6 as x,dh as C}from"./index-ClCsMWbS.js";import{I as N}from"./InputField-CqTVHKf8.js";import{g as v}from"./index-C8QGyin-.js";import"./envs-CSqIi4cL.js";const z=k.create(t=>{const{addInstallCode:o}=t,s=_(),{t:i}=f(["settings","common"]),[r,c]=m.useState("");return m.useEffect(()=>{const a=d=>{d.key==="Escape"&&(d.preventDefault(),s.remove())};return window.addEventListener("keydown",a),()=>window.removeEventListener("keydown",a)},[s]),n.jsx(h,{isOpen:s.visible,title:i(a=>a.add_install_code),footer:n.jsxs(n.Fragment,{children:[n.jsx(l,{className:"btn btn-neutral",onClick:s.remove,children:i(a=>a.cancel,{ns:"common"})}),n.jsx(l,{className:"btn btn-primary ms-1",onClick:async()=>{r&&(s.remove(),await o(r))},children:i(a=>a.add_install_code)})]}),children:n.jsx("div",{className:"flex flex-col gap-2",children:n.jsx(N,{name:"install_code",label:i(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,s)=>{const i=new FileReader;i.onloadend=()=>o(i.result),i.onerror=r=>s(r),i.readAsDataURL(t)})}async function B(t){const o=await fetch(t);if(o.ok){const s=await o.blob();return await S(s)}return Promise.reject(o.status)}function E({sourceIdx:t,devices:o}){const[s,i]=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(s==="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"}))}).then();i("inprogress")}},[s,o,d]),s){case"none":return n.jsx(l,{className:"btn btn-outline btn-primary join-item",onClick:()=>i("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 A({sourceIdx:t}){const o=u(e=>e.setBackupPreparing),s=u(p(e=>e.bridgeInfo[t])),i=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(()=>{const e=`z2m-backup.${s.version}.${Date.now()}.zip`;y(`data:application/zip;base64,${i}`,e)},[i,s.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"})}):i?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(E,{sourceIdx:t,devices:c})]})}export{A as default};
