import*as Common from"../common/common.js";import{RemoteObject}from"./RemoteObject.js";import{Capability,SDKModel}from"./SDKModel.js";export class IOModel extends SDKModel{constructor(e){super(e)}async read(e,o,r){const t=await this.target().ioAgent().invoke_read({handle:e,offset:r,size:o});if(t.getError())throw new Error(t.getError());return t.eof?null:t.base64Encoded?Common.Base64.decode(t.data):t.data}async close(e){(await this.target().ioAgent().invoke_close({handle:e})).getError()&&console.error("Could not close stream.")}async resolveBlob(e){const o=e instanceof RemoteObject?e.objectId:e;if(!o)throw new Error("Remote object has undefined objectId");const r=await this.target().ioAgent().invoke_resolveBlob({objectId:o});if(r.getError())throw new Error(r.getError());return`blob:${r.uuid}`}async readToString(e){const o=[],r=new TextDecoder;for(;;){const t=await this.read(e,1048576);if(null===t){o.push(r.decode());break}t instanceof ArrayBuffer?o.push(r.decode(t,{stream:!0})):o.push(t)}return o.join("")}}SDKModel.register(IOModel,Capability.IO,!0);