1import{t as e}from"./dist-C5po-C0e.js";import{A as t,F as n,G as r,J as i,K as a,L as o,M as s,N as c,P as l,U as u,V as d,k as f,q as p}from"./index-J2S54SZa.js";var m=o(`<span class=peach-pairing-spinner>`),h=o(`<p class="peach-pairing-error peach-pairing-error--muted">`),g=o(`<button class=peach-pairing-toggle>I have a code to enter →`),_=o(`<div class="peach-pairing-content peach-pairing-code-screen"><p class=peach-pairing-headline>Add a device</p><p class="peach-pairing-subline peach-pairing-subline--center">Open Peach on your other device, tap<br>Add a device, and enter this code.</p><p class=peach-pairing-hint>Both devices must be on the same WiFi.`),v=o(`<button class=peach-pairing-toggle>← Show my code instead`),y=o(`<div class="peach-pairing-content peach-pairing-code-screen"><p class=peach-pairing-headline>Add a device</p><input class=peach-pairing-code-input type=text inputmode=numeric autocomplete=off autocorrect=off placeholder="000 000"aria-label="enter pairing code"><button class="peach-btn peach-btn--accent peach-btn--md peach-lock-submit"></button><p class=peach-pairing-hint>Both devices must be on the same WiFi.`),b=o(`<div class="peach-overlay-view ov-enter-right"><div class=peach-overlay-header><button class="peach-btn peach-btn--ghost peach-btn--sm peach-icon-btn"aria-label=Close><svg width=7 height=12 viewBox="0 0 7 12"fill=none stroke=currentColor stroke-width=1.1 stroke-linecap=round stroke-linejoin=round><path d="M6 1L1 6L6 11"></path></svg></button><span class=peach-overlay-title>add a device</span><div class=peach-overlay-spacer></div></div><div class=peach-pairing-body>`),x=o(`<div class=peach-pairing-content><div class=peach-pairing-success-icon><svg width=32 height=32 viewBox="0 0 32 32"fill=none><circle cx=16 cy=16 r=14 stroke=var(--peach-health-ok) stroke-width=1.5></circle><path d="M10 16L14 20L22 12"stroke=var(--peach-health-ok) stroke-width=1.5 stroke-linecap=round stroke-linejoin=round></path></svg></div><p class=peach-pairing-headline>Paired!</p><p class="peach-pairing-subline peach-pairing-subline--center"></p><button class="peach-btn peach-btn--accent peach-btn--md peach-lock-submit">Done`),S=o(`<div class="peach-pairing-code peach-pairing-code--loading">`),C=o(`<button class="peach-pairing-code peach-pairing-code--expired">refresh`),w=o(`<div class=peach-pairing-code aria-label="pairing code">`),T=!e.isNativePlatform();function E(){return m()}function D(e){return e.length<=3?e:`${e.slice(0,3)} ${e.slice(3)}`}function O(o){let[s,m]=a(T?`show`:`enter`),[O,k]=a(null),[A,j]=a(null),[M,N]=a(null),[P,F]=a(``),[I,L]=a(!1),[R,z]=a(null),[B,V]=a(null),H=null;function U(){H!==null&&(clearTimeout(H),H=null)}function W(e){let t=e;if(t?.type===`PAIRING_COMPLETED`&&t.payload?.deviceName){let e={deviceId:t.payload.deviceName,displayName:t.payload.deviceName,x25519PublicKey:``,pairedAt:Date.now()};U(),k(null),j(null),V(e),o.onPaired(e)}}async function G(){N(null),k(null),j(null),U();let e=await f({type:`START_PAIRING_SERVER`});if(!e.ok||!e.data){N(e.error===`pro_required`?`Sync requires Peach Pro.`:`Couldn't start the pairing server.`);return}k(e.data.token),j(e.data.expiresAt);let t=Math.max(0,e.data.expiresAt-Date.now());H=setTimeout(()=>{k(null),j(null),N(`Code expired — tap to refresh.`)},t)}async function K(){U(),k(null),j(null),await f({type:`STOP_PAIRING_SERVER`})}async function q(){await K(),await G()}async function J(e){s()!==e&&(e===`enter`?(await K(),z(null),F(``),m(`enter`)):(z(null),m(`show`),await G()))}function Y(e){F(D(e.currentTarget.value.replace(/[^0-9]/g,``).slice(0,6))),R()&&z(null)}function X(){return P().replace(/\D/g,``)}async function Z(){let e=X();if(e.length!==6)return;L(!0),z(null);let t=await f({type:`CONNECT_WITH_CODE`,payload:{token:e}});L(!1);let n=t;if(!n.ok){z(n.error===`no peer responded to pairing code`||(n.error??``).includes(`resolve failed`)?`Couldn't find that device. Make sure both are on the same WiFi.`:n.error===`pair_rejected`?`The other device declined.`:n.error===`invalid_code`?`Enter all six digits.`:`Couldn't connect. Try again.`);return}let r=n.data?.deviceName??`Peach`,i={deviceId:r,displayName:r,x25519PublicKey:``,pairedAt:Date.now()};V(i),o.onPaired(i)}return i(()=>{e.isNativePlatform()||chrome.runtime.onMessage.addListener(W),T&&G()}),p(()=>{e.isNativePlatform()||chrome.runtime.onMessage.removeListener(W),U()}),(()=>{var e=b(),i=e.firstChild,a=i.firstChild,p=i.nextSibling;return a.$$click=()=>{f({type:`STOP_PAIRING_SERVER`}),o.onClose()},c(p,u(d,{get when(){return B()},children:e=>(()=>{var n=x(),r=n.firstChild.nextSibling.nextSibling,i=r.nextSibling;return c(r,()=>e().displayName),t(i,`click`,o.onClose,!0),n})()}),null),c(p,u(d,{get when(){return l(()=>!B())()&&s()===`show`},get children(){var e=_(),t=e.firstChild.nextSibling;return t.nextSibling,c(e,u(d,{get when(){return O()},get fallback(){return u(d,{get when(){return!M()},get fallback(){return(()=>{var e=C();return e.$$click=()=>void q(),e})()},get children(){var e=S();return c(e,u(E,{})),e}})},children:e=>(()=>{var t=w();return c(t,()=>D(e())),t})()}),t),c(e,u(d,{get when(){return M()},get children(){var e=h();return c(e,M),e}}),null),c(e,u(d,{when:T,get children(){var e=g();return e.$$click=()=>void J(`enter`),e}}),null),e}}),null),c(p,u(d,{get when(){return l(()=>!B())()&&s()===`enter`},get children(){var e=y(),t=e.firstChild.nextSibling,i=t.nextSibling;return i.nextSibling,t.$$input=Y,n(t,`spellcheck`,!1),i.$$click=()=>void Z(),c(i,()=>I()?`Connecting…`:`Connect →`),c(e,u(d,{get when(){return R()},get children(){var e=h();return c(e,R),e}}),null),c(e,u(d,{when:T,get children(){var e=v();return e.$$click=()=>void J(`show`),r(()=>e.disabled=I()),e}}),null),r(e=>{var n=I(),r=X().length!==6||I();return n!==e.e&&(t.disabled=e.e=n),r!==e.t&&(i.disabled=e.t=r),e},{e:void 0,t:void 0}),r(()=>t.value=P()),e}}),null),e})()}s([`click`,`input`]);export{O as SyncPairingView};