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