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