@tinkerlandai/indiana-sdk 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +177 -8
  3. package/README.zh-CN.md +94 -6
  4. package/dist/{ai-usage-BzydP8eN.d.ts → ai-usage-B4W5m4Ie.d.ts} +21 -4
  5. package/dist/chunk-4JBIKJMU.js +4 -0
  6. package/dist/chunk-5TEZEOHX.js +1 -0
  7. package/dist/chunk-5WY3AXSS.js +1 -0
  8. package/dist/chunk-HU2HIDVT.js +11 -0
  9. package/dist/chunk-OHDY27EI.js +1 -0
  10. package/dist/chunk-OK2Q6IJD.js +1 -0
  11. package/dist/chunk-PCVQHHEE.js +1 -0
  12. package/dist/chunk-RMJJOSVZ.js +1 -0
  13. package/dist/chunk-S2MSIA57.js +17 -0
  14. package/dist/chunk-S5TVMQRC.js +1 -0
  15. package/dist/configuration-sync-Bnu8s4pc.d.ts +1314 -0
  16. package/dist/{customer-page-aZb5Twue.d.ts → customer-page-B4FGCVpt.d.ts} +3 -0
  17. package/dist/funnel-runtime-host-L3WILUHK.js +1 -0
  18. package/dist/funnel-runtime-start-Zbd5ro5r.d.ts +30920 -0
  19. package/dist/{growth-rules-D64pEvg9.d.ts → growth-rules-B4YYy64R.d.ts} +8 -1
  20. package/dist/index.d.ts +14950 -5193
  21. package/dist/index.js +1 -1
  22. package/dist/native-shell.js +1 -1
  23. package/dist/openrouter.d.ts +1 -1
  24. package/dist/openrouter.js +1 -1
  25. package/dist/page.d.ts +6 -8
  26. package/dist/page.js +1 -1
  27. package/dist/paywall-TCE5DE5T.js +1 -0
  28. package/dist/popup.d.ts +5 -2
  29. package/dist/popup.js +6 -6
  30. package/dist/server.d.ts +4 -3
  31. package/dist/server.js +1 -1
  32. package/dist/src-VGIVVM27.js +1 -0
  33. package/dist/{visual-editor-agent-FDCV3BXP.js → visual-editor-agent-WW65CMKW.js} +1 -1
  34. package/dist/visual.d.ts +4 -2
  35. package/dist/visual.js +1 -1
  36. package/package.json +1 -1
  37. package/dist/chunk-3OSMEG44.js +0 -17
  38. package/dist/chunk-GA6X57BO.js +0 -1
  39. package/dist/chunk-GTBD4BJ4.js +0 -11
  40. package/dist/chunk-QUW4UOKZ.js +0 -4
  41. package/dist/chunk-RMFH5TWV.js +0 -1
  42. package/dist/chunk-YNUVRZRQ.js +0 -1
  43. package/dist/funnel-runtime-host-RZRB2CY6.js +0 -1
  44. package/dist/funnel-runtime-start-JHA9ES24.d.ts +0 -9199
  45. package/dist/paywall-MYBEOVTQ.js +0 -1
  46. package/dist/src-E2GXK6WA.js +0 -1
  47. /package/dist/{chunk-ZT523F45.js → chunk-BIFEEVQO.js} +0 -0
@@ -63,6 +63,7 @@ declare const couponInputSchema: z.ZodObject<{
63
63
  firstPurchaseOnly: z.ZodBoolean;
64
64
  condition: z.ZodRecord<z.ZodString, z.ZodJSONSchema>;
65
65
  }, z.core.$strict>;
66
+ selfClaimEnabled: z.ZodDefault<z.ZodBoolean>;
66
67
  startsAt: z.ZodNullable<z.ZodISODateTime>;
67
68
  endsAt: z.ZodNullable<z.ZodISODateTime>;
68
69
  totalLimit: z.ZodNullable<z.ZodNumber>;
@@ -149,6 +150,8 @@ declare const offerSchema: z.ZodObject<{
149
150
  type Offer = z.infer<typeof offerSchema>;
150
151
  /** 领取失败的原因:未生效 / 不在有效期 / 不符合标签 / 不是首购 / 已达每人上限 / 已领完。 */
151
152
  declare const offerUnavailableReasonSchema: z.ZodEnum<{
153
+ unavailable: "unavailable";
154
+ claim_disabled: "claim_disabled";
152
155
  inactive: "inactive";
153
156
  outside_window: "outside_window";
154
157
  not_targeted: "not_targeted";
@@ -0,0 +1 @@
1
+ import{a as ne}from"./chunk-6X42DWOM.js";import"./chunk-OK2Q6IJD.js";import{a as p}from"./chunk-5TEZEOHX.js";import"./chunk-S5TVMQRC.js";import{Gd as ge,ni as me}from"./chunk-HU2HIDVT.js";import{Gb as be}from"./chunk-RMJJOSVZ.js";import"./chunk-OHDY27EI.js";import"./chunk-S2MSIA57.js";function Ce(C){let x=C.view;if(!x?.paywall)throw new p("INVALID_REQUEST","The current node is not a Paywall");let n=x.paywall.receiptId,Y=x.nodeId,te=x.visit,f=x.paywall.resolution,E=x.paywall.quote,y=x.paywall.operation,O=!1,U=new AbortController,h=x.paywall.purchaseRequestId,T=0,k=x.paywall.selectionGeneration??0,q=x.paywall.canReselect===!0,_=null,G=new Map,V=new Map;function R(t){if(t.purchase&&t.purchase.receiptId!==n)throw new p("INVALID_RESPONSE","Purchase receipt changed");let i=t.run.paywall;return t.purchase??(i?.receiptId===n?i:null)}function g(){if(O)throw new p("IDENTITY_CHANGED","Paywall presentation has closed")}function N(t,i,s){if(g(),i!==T||s!==void 0&&E?.operationId!==s)return!1;let w=k,a=t.run.paywall;if(a?.receiptId===n){if((a.selectionGeneration??0)<k)return!1;if(k=a.selectionGeneration??0,q=a.canReselect===!0,a.resolution.id!==f.id||a.resolution.offering.id!==f.offering.id)throw new p("INVALID_RESPONSE","The pinned Paywall binding changed")}let u=R(t),l=k>w,D=l?u?.quote??null:u?.quote??E,P=u?.operation??(D?.operationId===E?.operationId?y:null);if(D&&P&&D.operationId!==P.operationId)throw new p("INVALID_RESPONSE","Purchase operation changed");return a?.receiptId===n&&(f=a.resolution),(l||D?.operationId!==E?.operationId)&&(T++,a?.receiptId===n&&(h=a.purchaseRequestId)),E=D,(y?.operationId!==P?.operationId||y?.status==="pending"||P?.status==="complete")&&(y=P),!0}function J(t,i,s){return new Promise((w,a)=>{let u=!1,l=()=>{clearTimeout(ie),U.signal.removeEventListener("abort",$)},D=Z=>{u||(u=!0,l(),w(Z))},P=Z=>{u||(u=!0,l(),a(Z))},$=()=>P(new p("IDENTITY_CHANGED","Paywall presentation has closed")),ie=setTimeout(()=>P(new p("COMMAND_PENDING","The purchase command is still pending; retry the original request",202,s)),Math.max(0,i-Date.now()));U.signal.addEventListener("abort",$,{once:!0}),U.signal.aborted&&$(),t.then(D,P)})}function z(t){return new Promise(i=>{let s=()=>{clearTimeout(w),U.signal.removeEventListener("abort",s),i()},w=setTimeout(s,t);U.signal.addEventListener("abort",s,{once:!0}),U.signal.aborted&&s()})}async function j(t,i,s){g();let w=G.get(t);if(!w){let l=C.pendingCommands?.find(D=>D.requestId===t)??C.pendingCommand;if(l?.operation==="submit"&&l.requestId===t&&l.nodeId===Y&&l.visit===te){i.type==="paywall_quote"&&l.input.type==="paywall_quote"&&l.input.predecessor&&!i.predecessor&&(i={...i,predecessor:l.input.predecessor});let D=me.parse(l),P=me.parse({...l,input:i});if(P.operation!=="submit"||D.operation!=="submit"||JSON.stringify(P.input)!==JSON.stringify(D.input))throw new p("CONFLICT","A restored purchase request cannot change its input");i=P.input,w=D,G.set(t,w)}}if(w){if(JSON.stringify(w.input)!==JSON.stringify(i))throw new p("CONFLICT","A purchase request cannot change its input")}else{let l=C.view;if(!l||l.nodeId!==Y||l.visit!==te||l.paywall?.receiptId!==n)throw new p("PRECONDITION_FAILED","The Paywall visit has changed");w={operation:"submit",requestId:t,nodeId:Y,visit:te,revision:l.revision,input:i},G.set(t,w)}let a=Date.now()+3e4,u=await J(C.submit(w),a,t);for(;u.run.pendingRequest?.requestId===t&&Date.now()<a&&!O&&(await z(Math.min(1e3,a-Date.now())),!(O||Date.now()>=a));)u=await J(C.resume(t),a,t);if(O)throw new p("IDENTITY_CHANGED","Paywall presentation has closed");if(u.run.pendingRequest?.requestId===t)throw new p("COMMAND_PENDING","The purchase command is still pending; retry the original request",202,t);if(!N(u,s,i.type==="paywall_confirm"||i.type==="paywall_cancel"?i.operationId:void 0))throw new p("PRECONDITION_FAILED","The purchase selection has changed")}async function X(t){if(!E)throw new p("INVALID_REQUEST","A quoted purchase is required");let i=`${t}:${E.operationId}`,s=V.get(i);if(!s){let w=t==="confirm"?"paywall_confirm":"paywall_cancel",a=C.pendingCommands?.find(u=>u.operation==="submit"&&u.input.type===w&&"operationId"in u.input&&u.input.operationId===E.operationId)??C.pendingCommand;s=a?.operation==="submit"&&a.input.type===w&&"operationId"in a.input&&a.input.operationId===E.operationId?a.requestId:crypto.randomUUID(),V.set(i,s)}if(await j(s,{type:t==="confirm"?"paywall_confirm":"paywall_cancel",operationId:E.operationId},T),!y)throw new p("INVALID_RESPONSE","The accepted purchase result is missing");return structuredClone(y)}return{get view(){return structuredClone(f)},get quote(){return E?structuredClone(E):null},get operation(){return y?structuredClone(y):null},get webViewUrl(){return C.webViewUrl},get canReselect(){return q},async refresh(){g();let t=T;return N(await C.get(),t),structuredClone(f)},preview(t){if(h&&h!==t.requestId&&(_||!q))return Promise.reject(new p("COMMAND_PENDING","Retry the original purchase before starting another"));if(h===t.requestId&&_)return _;let i=f.document.buttons[t.actionId],s=i?.type==="checkout"?ge(i.product,t.selection,f.capabilities):null;if(!s)return Promise.reject(new p("PRODUCT_UNAVAILABLE","Choose an available Product in this purchase area"));let w=h!==t.requestId&&k>0?{operationId:E?.operationId??null,generation:k}:void 0;if(w&&(!q||k<=0))return Promise.reject(new p("COMMAND_PENDING","Original purchase requires authoritative termination"));h!==t.requestId&&T++;let a=T;h=t.requestId;let u=(async()=>{if(await j(t.requestId,{type:"paywall_quote",actionId:t.actionId,productVersionId:s,...w?{predecessor:w}:{},...t.offerId?{offerId:t.offerId}:{},...t.nativeOffer?{nativeOffer:t.nativeOffer}:{}},a),!E)throw new p("INVALID_RESPONSE","The accepted quote is missing");return structuredClone(E)})();return _=u,u.then(()=>{_===u&&(_=null)},l=>{_===u&&(_=null),h===t.requestId&&C.pendingCommand?.requestId!==t.requestId&&l instanceof p&&l.status!==void 0&&l.status<500&&![401,425].includes(l.status)&&(h=null)}),u},confirm:()=>X("confirm"),cancel:()=>X("cancel"),async read(){if(g(),!E)throw new p("INVALID_REQUEST","A quoted purchase is required");let t=E.operationId,i=T,s=C.pendingCommands?.find(l=>l.operation==="submit"&&(l.input.type==="paywall_confirm"||l.input.type==="paywall_cancel")&&l.input.operationId===t)??C.pendingCommand;s?.operation==="submit"&&(s.input.type==="paywall_confirm"||s.input.type==="paywall_cancel")&&s.input.operationId===t&&await C.settle?.(s.requestId);let w=await C.get(void 0,{receiptId:n,operationId:t});g();let a=R(w);if(a?.quote&&a.quote.operationId!==t||a?.operation&&a.operation.operationId!==t)throw new p("INVALID_RESPONSE","Purchase operation changed");let u=N(w,i,t)?y:a?.operation;if(!u)throw new p("INVALID_RESPONSE","The current purchase result is missing");return structuredClone(u)},destroy(){O=!0,U.abort(),G.clear(),V.clear()}}}async function Fe(C,x){let n=x,{mountScreenPreview:Y}=await import("./popup.js"),{showPaywall:te}=await import("./paywall-TCE5DE5T.js"),f=C.ownerDocument,E=f.activeElement,y=f.createElement("section"),O=f.createElement("div"),U=f.createElement("div"),h=f.createElement("div"),T=f.createElement("button");h.hidden=!0,T.type="button";let k=f.createElement("p"),q=f.createElement("p"),_=f.createElement("button"),G=f.createElement("button"),V=f.createElement("button"),R=f.createElement("dialog");y.dataset.indianaFunnel="",y.setAttribute("aria-busy","false"),y.style.cssText="box-sizing:border-box;width:100%;min-width:0;font:14px/1.5 system-ui,sans-serif;color:#172033;background:#fff",U.style.cssText="display:flex;gap:12px;align-items:center;flex-wrap:wrap;padding:12px 20px",k.setAttribute("role","status"),q.setAttribute("role","alert"),q.hidden=!0,_.type=G.type=V.type="button",U.append(_,k,q,V,T,G),y.append(O,h,U),C.append(y);let g=null,N=null,J=null,z="",j="",X="",t="",i=!1,s=0,w=!1,a="",u=null,l=Promise.resolve(),D=null,P=null,$={},ie=new Map,Z=new Set,se=new Set,ae=new Set,oe=null,H={},b=!1,ye,M=0,A=null,re="",le=new Map,he=e=>e?`${e.id}:${e.nodeId}:${e.visit}:${e.revision}:${e.paywall?.quote?.operationId??""}`:"",Q=(e,o=null)=>{i||(P=o,q.textContent=e instanceof Error?e.message:"Request failed",q.hidden=!1,V.hidden=n.client.pendingCommand===null,ne(n.onError,e))};function K(){let e=n.client.view;if(!e)throw new p("INVALID_REQUEST","No current Run is available");return{requestId:crypto.randomUUID(),nodeId:e.nodeId,visit:e.visit,revision:e.revision}}async function W(e,o=!1){if(!(s&&!o||i||(b||n.client.view?.recoveryRestricted)&&!o)){s++,y.setAttribute("aria-busy","true"),q.hidden=!0,P=null;try{if(!o&&oe){let r=n.client.view;await oe;let v=n.client.view;if(!r||!v||v.id!==r.id||v.nodeId!==r.nodeId||v.visit!==r.visit||v.stopped||!["running","waiting"].includes(v.status))throw new p("PRECONDITION_FAILED","The screen changed while its display was acknowledged")}if(i)return;let c=await e();i||await B(n.client.view??c.run)}catch(c){Q(c,n.client.pendingCommand?.requestId??null)}finally{s=Math.max(0,s-1),y.setAttribute("aria-busy",String(s>0)),i||(V.hidden=n.client.pendingCommand===null,_.disabled=s>0||!!n.client.pendingCommand||!n.client.view?.canBack)}}}function ve(e){if(b||Z.has(e)||se.has(e)||i)return;se.add(e);let o=async()=>{if(b||i||z!==e){se.delete(e);return}if(s||n.client.pendingCommand){setTimeout(()=>{o()},250);return}let c=(async()=>{await n.client.submit({operation:"shown",...K()}),Z.add(e)})();oe=c;let r=()=>{oe===c&&(oe=null),se.delete(e)};c.then(r,v=>{r(),s||Q(v)})};o()}function ue(){u&&clearTimeout(u),!(i||!J||!["running","waiting"].includes(J.status))&&(u=setTimeout(()=>{u=null,Ie().finally(ue)},J.pendingRequest?1500:5e3))}async function Ie(){if(!(i||w||s)){if(b){await ce(ye);return}w=!0;try{let e=n.client.view,o=$,c=await n.client.get(e?.screen&&Object.keys(o).length?{nodeId:e.nodeId,visit:e.visit,revision:e.revision,components:o}:void 0);!i&&o===$&&await B(n.client.view??c.run)}catch(e){Q(e)}finally{w=!1}}}async function Ee(e){let o=n.client.view,c=o?.screen,r=c?.customerData.nodes[e.nodeId];if(i||s||!o||!c||!r)return;if(!["refresh","next","previous","filter","sources_next","sources_previous"].includes(e.action)){let F=g?.document,ee=F?be(F,e.nodeId):null,we=ee?.type==="customer_data"?ee.data.outputs?.[e.action]:void 0;we&&c.outputs.includes(we)?await W(()=>n.client.submit({operation:"submit",...K(),input:{type:"screen",output:we,answers:H,selection:e.value?{[e.nodeId]:e.value}:{}}})):await n.onCustomerDataIntent?.(e,o);return}let m=e.action.startsWith("sources_")?r.resources.find(F=>F.key===e.value):null;if(e.action.startsWith("sources_")&&!m)throw new p("INVALID_REQUEST","Choose a current resource");let L=`${o.visit}:${e.nodeId}:${m?.key??""}`,d=ie.get(L)??[null],I=$[e.nodeId]??{page:1,after:null,filter:"all",...m?{resourceKey:m.key}:{}};if(e.action==="filter"){let F=e.value;if(!F||!(r.source==="offer_history"?["all","issued","reserved","redeemed","expired","revoked"]:["all","purchase","refund","source","usage","debt","settlement"]).includes(F))throw new p("INVALID_REQUEST","Unknown data filter");I={page:1,after:null,filter:F},d.splice(0,d.length,null)}else if(e.action==="next"||e.action==="sources_next"){let F=m?.sources.at(-1),ee=m?F?{kind:"ledger",cursor:{at:F.detail[0],id:F.id,type:"source"}}:null:c.dataCursors[e.nodeId];if(!ee||!m&&!r.hasNext)return;d[I.page]=ee,I={...I,page:I.page+1,after:ee}}else if(e.action==="previous"||e.action==="sources_previous"){if(I.page<=1)return;I={...I,page:I.page-1,after:d[I.page-2]??null}}ie.set(L,d),$={...$,[e.nodeId]:I},await W(()=>n.client.get({nodeId:o.nodeId,visit:o.visit,revision:o.revision,components:$}))}function Re(e){let o=e.screen?.presentation;if(!o||o.type==="inline"){R.open&&R.close(),y.parentElement!==C&&C.append(y);return}y.parentElement!==R&&(R.append(y),C.append(R));let c={sm:420,md:640,lg:900};R.style.cssText=`padding:0;border:0;max-width:calc(100vw - 32px);max-height:calc(100dvh - 32px);overflow:auto;width:${"size"in o?c[o.size]+"px":"100vw"}`,R.setAttribute("aria-label",g?.document?.name??e.screen?.content.document.name??"Funnel"),R.open||R.showModal()}async function De(e){if(i)return;J=e,P&&n.client.pendingCommand?.requestId!==P&&(q.hidden=!0,P=null);let o=e.locale.startsWith("zh");if(T.textContent=o?"\u56DE\u770B":"History",T.hidden=!n.client.review,_.textContent=b?o?"\u8FD4\u56DE\u5F53\u524D":"Return to current":o?"\u8FD4\u56DE":"Back",G.textContent=o?"\u5173\u95ED":"Close",V.textContent=o?"\u91CD\u8BD5":"Retry",_.disabled=!b&&(s>0||!!n.client.pendingCommand||!e.canBack),V.hidden=n.client.pendingCommand===null,k.textContent=e.pendingRequest?o?"\u6B63\u5728\u5904\u7406":"Processing":e.instruction?.type==="wait"?o?"\u7B49\u5F85\u4E2D":"Waiting":"",a!==`${e.revision}:${e.visit}:${e.status}:${e.stopped}`&&(a=`${e.revision}:${e.visit}:${e.status}:${e.stopped}`,ne(n.onState,structuredClone(e))),e.stopped||n.client.configurationReady===!1){b=!1,M++,A?.destroy(),A=null,h.replaceChildren(),h.hidden=!0,O.hidden=!1,g?.destroy(),g=null,N?.destroy(),N=null,O.replaceChildren(),z="",j="",t="",R.open&&R.close(),y.hidden=!0,ue();return}if(y.hidden=!1,b){ue();return}if(e.recoveryRestricted){g?.destroy(),g=null,N?.destroy(),N=null,O.replaceChildren(),z="",k.textContent=o?"\u539F\u8BB0\u5F55\u9700\u8981\u6838\u5BF9\uFF0C\u53EF\u53EA\u8BFB\u56DE\u770B":"Original records require reconciliation; read-only history is available";return}let c=e.screen||e.paywall?`${e.id}:${e.nodeId}:${e.visit}`:"";if(c!==z&&(g?.destroy(),g=null,N?.destroy(),N=null,O.replaceChildren(),z=c,j="",t="",H={...e.answers},$={},ie.clear()),Re(e),e.screen){let v=e.screen.content;g||(g=Y(O,{mode:"live",rendererUrl:n.client.webViewUrl,title:v.document.name,onAction(d,S={},I={}){b||(H={...H,...I},W(()=>n.client.submit({operation:"submit",...K(),input:{type:"screen",output:d,answers:H,selection:S}})))},onAnswers:d=>{b||(H={...H,...d})},onCustomerDataIntent:d=>{b||Ee(d).catch(Q)},onEscape:()=>{b||de()},onError:d=>Q(new Error(d)),onRendered:()=>ve(c)}));let m={capabilities:e.screen.capabilities,customerData:e.screen.customerData,parameters:e.parameters,serverInput:{bindings:e.screen.inputs,answers:H,values:e.screen.values}},L=JSON.stringify([v,e.locale,m.parameters,e.screen.inputs,e.screen.values]);if(L!==j)g.update(v.document,e.locale,m),j=L,X=JSON.stringify(m.capabilities),t=JSON.stringify(m.customerData);else{let d=JSON.stringify(m.capabilities);d!==X&&(g.updateCapabilities(m.capabilities),X=d),JSON.stringify(m.customerData)!==t&&(g.updateCustomerData(m.customerData),t=JSON.stringify(m.customerData))}await g.ready}else if(e.paywall&&!N){let v=await te(O,{canInteract:()=>!b&&!i&&!n.client.view?.recoveryRestricted,start:async()=>Ce(n.client),onPurchase:n.onPurchase,onCheckoutUrl:n.onCheckoutUrl,resolveOfferId:n.resolveOfferId,onEvent:n.onEvent,onOpenUrl:m=>n.onOpenUrl?n.onOpenUrl({receiptId:e.paywall.receiptId,url:m,openIn:"same_window"}):f.defaultView?.location.assign(m),onAction:async m=>{if(b)return;let L=await n.client.submit({operation:"submit",requestId:crypto.randomUUID(),nodeId:e.nodeId,visit:e.visit,revision:e.revision,input:{type:"screen",output:m,answers:{}}});await B(n.client.view??L.run)},onError:Q,onDismiss:()=>{!b&&!i&&n.client.view?.visit===e.visit&&de()}});i?v.host?.destroy():(N=v.host,N?.setSuspended(b)),ve(c)}let r=e.instruction;if(r&&(r.type==="open_url"||r.type==="event")&&!ae.has(r.receiptId)){let v=f.createElement("button");v.type="button",v.textContent=r.type==="open_url"?`${o?"\u524D\u5F80":"Continue to"} ${new URL(r.url).host}`:o?"\u7EE7\u7EED":"Continue",v.onclick=()=>{let m=K(),L=r.type==="open_url"&&!n.onOpenUrl&&r.openIn==="new_window"?Pe(r.url):null;W(async()=>{let d="success";if(r.type==="open_url")if(n.onOpenUrl)try{d=await n.onOpenUrl({receiptId:r.receiptId,url:r.url,openIn:r.openIn})===!1?"failed":"opened"}catch(I){ne(n.onError,I),d="failed"}else d=r.openIn==="new_window"&&!L?"failed":"opened";if(r.type==="event"){if(!n.onEvent)throw new p("EVENT_HANDLER_REQUIRED","Provide the event handler");await n.onEvent(r.event)}if(i||n.client.view?.nodeId!==m.nodeId||n.client.view.visit!==m.visit)throw new p("PRECONDITION_FAILED","The client action visit changed");let S=await n.client.submit({operation:"submit",...m,input:{type:"client_action",receiptId:r.receiptId,outcome:d}});return ae.add(r.receiptId),r.type==="open_url"&&!n.onOpenUrl&&r.openIn==="same_window"&&f.defaultView?.location.assign(r.url),S})},O.replaceChildren(v)}!e.pendingRequest&&e.status==="completed"&&!ae.has(`complete:${e.id}`)&&(ae.add(`complete:${e.id}`),ne(n.onComplete,structuredClone(e))),ue()}function Pe(e){let o=f.defaultView?.open(e,"_blank")??null;return o&&(o.opener=null),o}function B(e){return l=l.catch(()=>{}).then(()=>De(e)),l}async function ce(e){if(i||!n.client.review)return;b||(re=he(n.client.view),le.set(re,structuredClone(H))),b=!0,ye=e,O.hidden=!0,N?.setSuspended(!0),h.hidden=!1;let o=++M;try{let c=await n.client.review(e);if(i||o!==M||!b)return;if(!c.review)throw new p("INVALID_RESPONSE","Historical projection is missing");A?.destroy(),A=null,h.replaceChildren();let r=c.run.locale.startsWith("zh"),v=f.createElement("p");v.textContent=r?"\u53EA\u8BFB\u56DE\u770B":"Read-only history",h.append(v);for(let[d,S]of c.review.pages.entries()){let I=f.createElement("button");I.type="button",I.textContent=r?`\u7B2C ${d+1} \u9875`:`Page ${d+1}`,I.onclick=()=>{ce(S.receiptId)},h.append(I)}if(c.review.page){let d=c.review.page,S=f.createElement("div");if(S.inert=!0,h.append(S),A=Y(S,{mode:"live",rendererUrl:n.client.webViewUrl,title:d.document.name,onAction:()=>{},onAnswers:()=>{},onCustomerDataIntent:()=>{},onRendered:()=>{},onEscape:()=>{fe()}}),A.update(d.document,c.run.locale,{serverInput:{bindings:d.inputs,answers:d.answers,values:d.values}}),await A.ready,i||o!==M||!b)return}let m=r?{issued:"\u5DF2\u9886\u53D6",reserved:"\u5DF2\u9884\u7559",redeemed:"\u5DF2\u4F7F\u7528",expired:"\u5DF2\u8FC7\u671F",revoked:"\u5DF2\u64A4\u9500",unknown:"\u72B6\u6001\u5F85\u786E\u8BA4"}:{issued:"Issued",reserved:"Reserved",redeemed:"Used",expired:"Expired",revoked:"Revoked",unknown:"Status unconfirmed"},L=r?{pending:"\u5904\u7406\u4E2D",complete:"\u5DF2\u5B8C\u6210",cancelled:"\u5DF2\u53D6\u6D88",failed:"\u5931\u8D25",unknown:"\u72B6\u6001\u5F85\u786E\u8BA4"}:{pending:"Processing",complete:"Completed",cancelled:"Cancelled",failed:"Failed",unknown:"Status unconfirmed"};for(let d of[...c.review.coupons.map((S,I)=>`${r?"\u4F18\u60E0\u5238":"Coupon"} ${I+1}: ${m[S.status]}`),...c.review.purchases.map((S,I)=>`${r?"\u8D2D\u4E70":"Purchase"} ${I+1}: ${L[S.status]}`)]){let S=f.createElement("p");S.textContent=d,h.append(S)}n.client.view&&await B(n.client.view)}catch(c){if(o!==M||i)return;A?.destroy(),A=null,h.replaceChildren(),Q(c)}}async function fe(){if(!b||i||!n.client.review)return;let e=++M;try{if(await n.client.review(),i||e!==M)return;b=!1,M++,A?.destroy(),A=null,h.replaceChildren(),h.hidden=!0,O.hidden=!1;let o=n.client.view;he(o)!==re?(le.set(re,structuredClone(H)),z="",j=""):(le.delete(re),N?.setSuspended(!1)),o&&await B(o)}catch(o){if(e!==M||i)return;A?.destroy(),A=null,h.replaceChildren(),Q(o)}}let Ne=n.client.subscribe(e=>{n.client.configurationReady===!1&&(y.hidden=!0,g?.destroy(),g=null,N?.destroy(),N=null,O.replaceChildren(),z="",j="",t="",R.open&&R.close()),B(e).catch(Q)}),Oe=n.client.subscribeError?.(Q);function de(){return D||(i?Promise.resolve():n.client.view&&!["running","waiting"].includes(n.client.view.status)?(pe(),Promise.resolve()):(D=W(()=>n.client.submit({operation:"close",...K()}),!0).then(()=>{n.client.view?.status==="closed"&&pe()}).finally(()=>{D=null}),D))}function pe(){i||(i=!0,u&&clearTimeout(u),Ne(),Oe?.(),g?.destroy(),M++,A?.destroy(),N?.destroy(),R.close(),R.remove(),y.remove(),E instanceof HTMLElement&&E.isConnected&&E.focus(),ne(n.onDismiss,J?structuredClone(J):null))}_.onclick=()=>{if(b){fe();return}W(()=>n.client.submit({operation:"back",...K()}))},T.onclick=()=>{ce()},G.onclick=()=>{de()},V.onclick=()=>{W(()=>n.client.retry())},R.addEventListener("cancel",e=>{e.preventDefault(),de()});try{let e=await n.client.get();await B(n.client.view??e.run)}catch(e){throw pe(),e}return{destroy:pe,refresh:Ie,review:ce,exitReview:fe,get unappliedDrafts(){return new Map([...le].map(([e,o])=>[e,structuredClone(o)]))}}}export{Fe as showFunnelRun};