"use client";
import{createContext as I,useContext as K,useEffect as M,useMemo as X,useRef as V}from"react";import{QueryClient as Y,QueryClientProvider as k}from"@tanstack/react-query";import H from"mitt";import{MittContext as L,SocketIOContext as R,CentrifugeIOContext as P,FallbackContext as _,PieConfigContext as v}from"@swarm.ing/pieui";var G="piecard",U="piecard/fire";import{jsx as A,jsxs as h,Fragment as c}from"react/jsx-runtime";var F={apiServer:"",centrifugeServer:"",enableRenderingLog:!1,pageProcessor:""},W={current:null},b=()=>{if(!W.current)W.current=new Y({defaultOptions:{queries:{retry:!1,staleTime:1/0}}});return W.current},Z=I(null),$=()=>{let p=K(Z);if(!p)throw Error("usePieStorybookEmitter must be used inside <PieStorybookProviders />");return p},s=({children:p,emitter:w,pieConfig:D})=>{let z=X(()=>w??H(),[w]),T=X(()=>({...F,...D??{}}),[D]);return A(k,{client:b(),children:A(v.Provider,{value:T,children:A(_.Provider,{value:A(c,{}),children:A(Z.Provider,{value:z,children:A(L.Provider,{value:z,children:A(R.Provider,{value:null,children:A(P.Provider,{value:null,children:p})})})})})})})},N=(p)=>h(s,{children:[A(Q,{}),A(p,{})]}),y=(p,w)=>`pie${w}_${p}`,q=(p,w,D,z)=>{p.emit(y(w,D),z)},f=({card:p,method:w,payload:D,label:z})=>{let T=$();return A("button",{type:"button",onClick:()=>q(T,p,w,D),style:{padding:"6px 12px",marginRight:8,marginBottom:8,fontSize:12,border:"1px solid #ccc",borderRadius:4,cursor:"pointer",background:"#f7f7f7"},children:z??`${p}.${w}`})},O=()=>{return globalThis.__STORYBOOK_ADDONS_CHANNEL__??null},Q=()=>{let p=$(),w=V(p);return w.current=p,M(()=>{let D=O();if(!D)return;let z=(...T)=>{let J=T[0];if(!J||!J.card||!J.method)return;q(w.current,J.card,J.method,J.payload)};return D.on(U,z),()=>D.off(U,z)},[]),null};export{N as withPieCard,$ as usePieStorybookEmitter,q as firePieMethod,s as PieStorybookProviders,Q as PieStorybookChannelBridge,f as PieMethodTrigger,U as PIE_STORYBOOK_FIRE_EVENT,G as PIECARD_PARAM_KEY};
