/* esm.sh - @assistant-ui/core@0.3.31/dist/runtime/utils/attachment-add-operations */
var a=class{operations=new Set;uploading=new Map;start(){let t={cancelled:!1,attachmentIds:new Set};return this.operations.add(t),t}accept(t,e){if(t.cancelled)return!1;t.attachmentIds.add(e.id);let s=this.uploading.get(e.id);return s?.operation!==t&&(s||e.status.type==="running")&&this.uploading.set(e.id,{operation:t,waiters:s?.waiters??new Set}),e.status.type!=="running"&&this.settle(e.id,t),!0}finish(t){this.operations.delete(t);for(let e of t.attachmentIds)this.settle(e,t)}isCancelled(t){return t.cancelled}cancel(t){for(let e of[...this.operations])e.attachmentIds.has(t)&&(e.cancelled=!0,this.operations.delete(e));this.settle(t)}cancelAll(t){for(let e of[...this.operations])t&&[...e.attachmentIds].some(s=>t.has(s))||(e.cancelled=!0,this.operations.delete(e));for(let e of[...this.uploading.keys()])t?.has(e)||this.settle(e)}whenSendable(t){let e=this.uploading.get(t);if(e)return new Promise(s=>e.waiters.add(s))}settle(t,e){let s=this.uploading.get(t);if(s&&!(e&&s.operation!==e)){this.uploading.delete(t);for(let i of s.waiters)i()}}},o=async(t,e)=>{if(Symbol.asyncIterator in t){let s,i=!1;for await(let n of t){if(s===void 0&&(s=n.id),n.id!==s){i||(console.warn("[assistant-ui] AttachmentAdapter.add() yielded multiple attachment ids; only the first id is accepted"),i=!0);continue}if(!e(n))break}}else e(await t)};export{a as AttachmentAddOperations,o as drainAttachmentAdd};
//# sourceMappingURL=attachment-add-operations.mjs.map