import{a1 as k,a6 as _,u as w,r as u,j as n,a7 as h,B as l,s as j,a as b,I as f,dh as y,a9 as x,dB as C}from"./index-CE-9RlbR.js";import{I as N}from"./InputField-DRmYNLGX.js";import{g as v}from"./index-CDoSh7QA.js";import"./envs-CRUy-_iG.js";const z=k.create(t=>{const{addInstallCode:i}=t,o=_(),{t:s}=w(["settings","common"]),[r,c]=u.useState("");return u.useEffect(()=>{const a=d=>{d.key==="Escape"&&(d.preventDefault(),o.remove())};return window.addEventListener("keydown",a),()=>window.removeEventListener("keydown",a)},[o]),n.jsx(h,{isOpen:o.visible,title:s(a=>a.add_install_code),footer:n.jsxs(n.Fragment,{children:[n.jsx(l,{className:"btn btn-neutral",onClick:o.remove,children:s(a=>a.cancel,{ns:"common"})}),n.jsx(l,{className:"btn btn-primary ms-1",onClick:async()=>{r&&(o.remove(),await i(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 B(t){return await new Promise((i,o)=>{const s=new FileReader;s.onloadend=()=>i(s.result),s.onerror=r=>o(r),s.readAsDataURL(t)})}async function S(t){try{const i=await fetch(t);if(i.ok){const o=await i.blob();return await B(o)}}catch{}}function q({sourceIdx:t,devices:i}){const[o,s]=u.useState("none"),[r,c]=u.useState({}),{t:a}=w(["settings","common","zigbee"]),d=u.useCallback(async e=>{c(m=>({...m,[e.ieee_address]:"init"}));const p=v(e);let g;for(const m of p)if(g=await S(m),g)break;return g?(await j(t,"bridge/request/device/options",{id:e.ieee_address,options:{icon:g}}),c(m=>({...m,[e.ieee_address]:"done"}))):(console.error("Error localising image"),c(m=>({...m,[e.ieee_address]:"error"}))),!0},[t]);switch(u.useEffect(()=>{if(o==="start"){for(const e of i)e.type!=="Coordinator"&&d(e).catch(p=>{console.log(p)});s("inprogress")}},[o,i,d]),o){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:i.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(p=>p[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 i=b(e=>e.setBackupPreparing),o=b(f(e=>e.bridgeInfo[t])),s=b(f(e=>e.backup[t])),r=b(f(e=>e.preparingBackup[t])),c=b(f(e=>e.devices[t])),{t:a}=w(["settings","common"]),d=u.useCallback(()=>{if(s){const e=`z2m-backup.${o.version}.${Date.now()}.zip`;y(`data:application/zip;base64,${s}`,e)}},[s,o.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 j(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(b.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()=>{i(t),await j(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 j(t,"bridge/request/install_code/add",{value:e})}),children:a(e=>e.add_install_code)}),n.jsx(q,{sourceIdx:t,devices:c})]})}export{A as default};
