1import{t as e}from"./dist-Cgg5UYwv.js";import{$ as t,B as n,F as r,I as i,Q as a,R as o,U as s,V as c,Y as l,Z as u,et as d,q as f,z as p}from"./index-C3AHYAm6.js";var m=s(`<span class=peach-pairing-spinner>`),h=s(`<p class="peach-pairing-error peach-pairing-error--muted">`),g=s(`<button class=peach-pairing-toggle>I have a code to enter →`),_=s(`<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=s(`<button class=peach-pairing-toggle>← Show my code instead`),y=s(`<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=s(`<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=s(`<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=s(`<div class="peach-pairing-code peach-pairing-code--loading">`),C=s(`<button class="peach-pairing-code peach-pairing-code--expired">refresh`),w=s(`<div class=peach-pairing-code aria-label="pairing code">`),T=!0;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(`show`),[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 r({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 r({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 r({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 i=n.data?.deviceName??`Peach`,a={deviceId:i,displayName:i,x25519PublicKey:``,pairedAt:Date.now()};V(a),o.onPaired(a)}return d(()=>{e.isNativePlatform()||chrome.runtime.onMessage.addListener(W),G()}),t(()=>{e.isNativePlatform()||chrome.runtime.onMessage.removeListener(W),U()}),(()=>{var e=b(),t=e.firstChild,a=t.firstChild,d=t.nextSibling;return a.$$click=()=>{r({type:`STOP_PAIRING_SERVER`}),o.onClose()},p(d,l(f,{get when(){return B()},children:e=>(()=>{var t=x(),n=t.firstChild.nextSibling.nextSibling,r=n.nextSibling;return p(n,()=>e().displayName),i(r,`click`,o.onClose,!0),t})()}),null),p(d,l(f,{get when(){return n(()=>!B())()&&s()===`show`},get children(){var e=_(),t=e.firstChild.nextSibling;return t.nextSibling,p(e,l(f,{get when(){return O()},get fallback(){return l(f,{get when(){return!M()},get fallback(){return(()=>{var e=C();return e.$$click=()=>void q(),e})()},get children(){var e=S();return p(e,l(E,{})),e}})},children:e=>(()=>{var t=w();return p(t,()=>D(e())),t})()}),t),p(e,l(f,{get when(){return M()},get children(){var e=h();return p(e,M),e}}),null),p(e,l(f,{when:T,get children(){var e=g();return e.$$click=()=>void J(`enter`),e}}),null),e}}),null),p(d,l(f,{get when(){return n(()=>!B())()&&s()===`enter`},get children(){var e=y(),t=e.firstChild.nextSibling,n=t.nextSibling;return n.nextSibling,t.$$input=Y,c(t,`spellcheck`,!1),n.$$click=()=>void Z(),p(n,()=>I()?`Connecting…`:`Connect →`),p(e,l(f,{get when(){return R()},get children(){var e=h();return p(e,R),e}}),null),p(e,l(f,{when:T,get children(){var e=v();return e.$$click=()=>void J(`show`),u(()=>e.disabled=I()),e}}),null),u(e=>{var r=I(),i=X().length!==6||I();return r!==e.e&&(t.disabled=e.e=r),i!==e.t&&(n.disabled=e.t=i),e},{e:void 0,t:void 0}),u(()=>t.value=P()),e}}),null),e})()}o([`click`,`input`]);export{O as SyncPairingView};