import{i as e}from"./rolldown-runtime-B1FJdls4.js";import{m as t}from"./vendor-dnd-C1cICjTT.js";import{n,t as r}from"./axisEvents-Dhd0R1o7.js";var i=`pi-mc-device-id`;function a(){let e=``;for(let t=0;t<8;t++)e+=`abcdefghijklmnopqrstuvwxyz0123456789`[Math.floor(Math.random()*36)];return e}var o=null;function s(){return typeof window<`u`}function c(){if(o)return o;if(!s())return`server`;try{let e=window.localStorage.getItem(i);if(e)return o=e,e}catch{}let e=a();o=e;try{window.localStorage.setItem(i,e)}catch{}return e}function l(e){return`dev:${c()}:${e}`}var u=e(t(),1),d=new Map,f=!1,p=null;function m(){return p||(p=h(),p)}async function h(){try{let e=[`dev:${c()}:`,`mc-`,`terminal-cells`,`pi-mc-`],t=await Promise.allSettled(e.map(e=>fetch(`${_}?prefix=${encodeURIComponent(e)}`)));for(let e of t)if(!(e.status!==`fulfilled`||!e.value.ok))try{let t=(await e.value.json()).items??[];for(let{key:e,value:n}of t)d.set(e,n)}catch{}}catch{}finally{f=!0}}function g(e){if(d.has(e))return d.get(e);if(f&&(e.startsWith(`dev:`)||e.startsWith(`mc-`)||e.startsWith(`terminal-cells`)||e.startsWith(`pi-mc-`)))return null}var _=`/api/ui-state`,v=new Set,y=new Map;async function b(e){if(d.has(e))return d.get(e);if(v.has(e))return null;let t=y.get(e);if(t)return t;if(f&&(e.startsWith(`dev:`)||e.startsWith(`mc-`)||e.startsWith(`terminal-cells`)||e.startsWith(`pi-mc-`)))return v.add(e),null;let n=(async()=>{try{let t=await fetch(`${_}?key=${encodeURIComponent(e)}`);if(!t.ok)return t.status===404&&v.add(e),null;let n=(await t.json()).value??null;return n===null?v.add(e):(v.delete(e),d.set(e,n)),n}catch{return null}finally{y.delete(e)}})();return y.set(e,n),n}async function x(e,t){v.delete(e),d.set(e,t);try{await fetch(_,{method:`PUT`,headers:{"Content-Type":`application/json`},body:JSON.stringify({key:e,value:t}),keepalive:!0})}catch{}}function S(e,t){v.delete(e),d.set(e,t);try{let n=new Blob([JSON.stringify({key:e,value:t})],{type:`application/json`});navigator.sendBeacon(_,n)}catch{}}async function C(e){d.delete(e),v.add(e);try{await fetch(`${_}?key=${encodeURIComponent(e)}`,{method:`DELETE`})}catch{}}function w(){let e=new Set;return{getItem:async t=>{let n=await b(t);if(n!==null)return n;if(!e.has(t)){e.add(t);try{let e=localStorage.getItem(t);if(e!==null)return await x(t,e),localStorage.removeItem(t),e}catch{}}return null},setItem:async(e,t)=>x(e,t),removeItem:async e=>C(e)}}function T(e,t,i={}){let a=i.enabled??!0,o=(0,u.useCallback)(e=>Array.isArray(t)&&!Array.isArray(e)||typeof t==`object`&&t&&!Array.isArray(t)&&(typeof e!=`object`||!e||Array.isArray(e))?t:e,[t]),s=a?g(e):void 0,c=(0,u.useMemo)(()=>{if(s!=null)try{return o(JSON.parse(s))}catch{return o(s)}return t},[]),[l,f]=(0,u.useState)(c),[p,m]=(0,u.useState)(a&&s!==void 0),h=(0,u.useRef)(null),_=(0,u.useRef)(c),v=(0,u.useRef)(e),y=(0,u.useRef)(!1),C=(0,u.useRef)(JSON.stringify(c));(0,u.useEffect)(()=>{let n=()=>{f(t),_.current=t,C.current=JSON.stringify(t)};if(!a){v.current=e,y.current=!1,h.current&&clearTimeout(h.current),n(),m(!1);return}let r=v.current!==e;r&&(v.current=e,m(!1));let i=g(e);if(i!==void 0){if(i!==null)try{let e=o(JSON.parse(i));f(e),_.current=e,C.current=i}catch{let e=o(i);f(e),_.current=e,C.current=i}else r&&n();m(!0);return}let s=!1;return b(e).then(e=>{if(!s){if(e!==null)try{let t=o(JSON.parse(e));f(t),_.current=t,C.current=e}catch{let t=o(e);f(t),_.current=t,C.current=e}else r&&n();m(!0)}}),()=>{s=!0}},[e,a]);let w=(0,u.useCallback)(t=>{f(n=>{let i=typeof t==`function`?t(n):t;_.current=i;let o=JSON.stringify(i);return o===C.current?n:(C.current=o,a?(y.current=!0,queueMicrotask(()=>{r(`mc:ui-state-changed`,{key:e,value:o})}),h.current&&clearTimeout(h.current),h.current=setTimeout(()=>{y.current=!1,x(e,o)},300),i):i)})},[e,a]);return(0,u.useEffect)(()=>{if(a)return n(`mc:ui-state-changed`,t=>{let{key:n,value:r}=t??{key:``,value:``};if(n===e){d.set(e,r);try{let e=o(JSON.parse(r));if(JSON.stringify(_.current)===r)return;_.current=e,C.current=r,f(e)}catch{}}})},[e,a]),(0,u.useEffect)(()=>{if(!a)return;let t=()=>{y.current&&(y.current=!1,h.current&&clearTimeout(h.current),S(e,JSON.stringify(_.current)))};return window.addEventListener(`beforeunload`,t),()=>{window.removeEventListener(`beforeunload`,t),h.current&&(clearTimeout(h.current),y.current&&(y.current=!1,x(e,JSON.stringify(_.current))))}},[e,a]),[l,w,p]}function E(e,t){let[n,r]=(0,u.useState)(null),i=n?.logicalKey===e;return(0,u.useEffect)(()=>{r({logicalKey:e,storageKey:l(e)})},[e]),T(i?n.storageKey:`__pending-device:${e}`,t,{enabled:i})}export{E as a,x as i,b as n,T as o,m as r,c as s,w as t};