/* esm.sh - @assistant-ui/core@0.3.31/dist/runtime/utils/attachment-send-operations */
import{isAttachmentComplete as n}from"../../types/attachment.mjs";var a=class{entries=new WeakMap;removals=new WeakMap;beginRemoval(e){let r=this.removals.get(e);return r?.phase==="pending"?!1:(r?r.phase="pending":this.removals.set(e,{phase:"pending",dispatched:!1,retried:!1,abandoned:!1}),!0)}abandonRemoval(e){let r=this.removals.get(e);r&&(r.abandoned=!0),this.removals.delete(e)}isRemoved(e){return this.removals.has(e)}shouldRemoveOnClear(e){let r=this.removals.get(e);return!r||r.dispatched&&r.retried&&r.phase==="failed"}isRemovalPending(e){return this.removals.get(e)?.phase==="pending"}async remove(e,r,s){let t=this.removals.get(e);try{if(!n(e)){if(!r)throw new Error("Attachments are not supported");await r.remove(e)}t.phase="complete"}catch(i){throw t.phase="failed",s?.(i,i instanceof Error?i.message:String(i)),this.retryDispatched(t),i}}dispatchRemoval(e,r){let s=this.removals.get(e);s&&(s.dispatched=!0,s.retry=r,this.retryDispatched(s))}retryDispatched(e){e.abandoned||!e.dispatched||e.phase!=="failed"||e.retried||!e.retry||(e.retried=!0,e.retry())}restore(e){let r=this.removals.get(e);if(!r||r.dispatched||r.phase!=="failed")return e;let s={...e},t=this.entries.get(e);return t&&this.entries.set(s,t),s}restoreUnsent(e){let r=this.removals.get(e);if(!(r?.dispatched||r?.phase==="complete"))return this.restore(e)}adoptFrom(e,r){let s=e.removals.get(r);s&&this.removals.set(r,s);let t=e.entries.get(r);t&&this.entries.set(r,t)}isSameRemoval(e,r){let s=this.removals.get(e);return s!==void 0&&s===this.removals.get(r)}reconcileFailures(e,r,s){let t=new Map;return s.forEach((i,o)=>{i.status==="rejected"&&t.set(r[o].id,i.reason)}),e.map(i=>{if(!t.has(i.id)||n(i)||this.isRemoved(i))return i;let o=t.get(i.id);return this.transfer(i,{...i,status:{type:"incomplete",reason:"error",message:o instanceof Error?o.message:String(o)}})})}async send(e,r,s){if(n(e))return e;let t=this.entries.get(e)??{};if(t.result)return t.result;if(!r)throw new Error("Attachments are not supported");this.entries.set(e,t);let i=await r.send(e,s?{signal:s}:void 0);return t.result=i,i}transfer(e,r){let s=this.entries.get(e);s&&this.entries.set(r,s);let t=this.removals.get(e);return t&&this.removals.set(r,t),r}};export{a as AttachmentSendOperations};
//# sourceMappingURL=attachment-send-operations.mjs.map