{"version":3,"sources":["../../src/api/AttachmentRuntime.ts"],"sourcesContent":["import { SubscribableWithState } from \"./subscribable/Subscribable\";\n\nimport { ComposerRuntimeCoreBinding } from \"./ComposerRuntime\";\nimport {\n  Attachment,\n  CompleteAttachment,\n  PendingAttachment,\n  Unsubscribe,\n} from \"../types\";\nimport { AttachmentRuntimePath } from \"./RuntimePathTypes\";\n\ntype MessageAttachmentState = CompleteAttachment & {\n  readonly source: \"message\";\n};\n\ntype ThreadComposerAttachmentState = PendingAttachment & {\n  readonly source: \"thread-composer\";\n};\n\ntype EditComposerAttachmentState = Attachment & {\n  readonly source: \"edit-composer\";\n};\n\nexport type AttachmentState =\n  | ThreadComposerAttachmentState\n  | EditComposerAttachmentState\n  | MessageAttachmentState;\n\ntype AttachmentSnapshotBinding<Source extends AttachmentRuntimeSource> =\n  SubscribableWithState<\n    AttachmentState & { source: Source },\n    AttachmentRuntimePath & { attachmentSource: Source }\n  >;\n\ntype AttachmentRuntimeSource = AttachmentState[\"source\"];\n\nexport type AttachmentRuntime<\n  TSource extends AttachmentRuntimeSource = AttachmentRuntimeSource,\n> = {\n  readonly path: AttachmentRuntimePath & { attachmentSource: TSource };\n  readonly source: TSource;\n  getState(): AttachmentState & { source: TSource };\n  remove(): Promise<void>;\n  subscribe(callback: () => void): Unsubscribe;\n};\n\nexport abstract class AttachmentRuntimeImpl<\n  Source extends AttachmentRuntimeSource = AttachmentRuntimeSource,\n> implements AttachmentRuntime\n{\n  public get path() {\n    return this._core.path;\n  }\n\n  public abstract get source(): Source;\n\n  constructor(private _core: AttachmentSnapshotBinding<Source>) {}\n\n  protected __internal_bindMethods() {\n    this.getState = this.getState.bind(this);\n    this.remove = this.remove.bind(this);\n    this.subscribe = this.subscribe.bind(this);\n  }\n\n  public getState(): AttachmentState & { source: Source } {\n    return this._core.getState();\n  }\n\n  public abstract remove(): Promise<void>;\n\n  public subscribe(callback: () => void) {\n    return this._core.subscribe(callback);\n  }\n}\n\nabstract class ComposerAttachmentRuntime<\n  Source extends \"thread-composer\" | \"edit-composer\",\n> extends AttachmentRuntimeImpl<Source> {\n  constructor(\n    core: AttachmentSnapshotBinding<Source>,\n    private _composerApi: ComposerRuntimeCoreBinding,\n  ) {\n    super(core);\n  }\n\n  public remove() {\n    const core = this._composerApi.getState();\n    if (!core) throw new Error(\"Composer is not available\");\n    return core.removeAttachment(this.getState().id);\n  }\n}\n\nexport class ThreadComposerAttachmentRuntimeImpl extends ComposerAttachmentRuntime<\"thread-composer\"> {\n  public get source(): \"thread-composer\" {\n    return \"thread-composer\";\n  }\n}\n\nexport class EditComposerAttachmentRuntimeImpl extends ComposerAttachmentRuntime<\"edit-composer\"> {\n  public get source(): \"edit-composer\" {\n    return \"edit-composer\";\n  }\n}\n\nexport class MessageAttachmentRuntimeImpl extends AttachmentRuntimeImpl<\"message\"> {\n  public get source(): \"message\" {\n    return \"message\";\n  }\n\n  constructor(core: AttachmentSnapshotBinding<\"message\">) {\n    super(core);\n  }\n\n  public remove(): never {\n    throw new Error(\"Message attachments cannot be removed\");\n  }\n}\n"],"mappings":";AA8CO,IAAe,wBAAf,MAGP;AAAA,EAOE,YAAoB,OAA0C;AAA1C;AAAA,EAA2C;AAAA,EAN/D,IAAW,OAAO;AAChB,WAAO,KAAK,MAAM;AAAA,EACpB;AAAA,EAMU,yBAAyB;AACjC,SAAK,WAAW,KAAK,SAAS,KAAK,IAAI;AACvC,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AACnC,SAAK,YAAY,KAAK,UAAU,KAAK,IAAI;AAAA,EAC3C;AAAA,EAEO,WAAiD;AACtD,WAAO,KAAK,MAAM,SAAS;AAAA,EAC7B;AAAA,EAIO,UAAU,UAAsB;AACrC,WAAO,KAAK,MAAM,UAAU,QAAQ;AAAA,EACtC;AACF;AAEA,IAAe,4BAAf,cAEU,sBAA8B;AAAA,EACtC,YACE,MACQ,cACR;AACA,UAAM,IAAI;AAFF;AAAA,EAGV;AAAA,EAEO,SAAS;AACd,UAAM,OAAO,KAAK,aAAa,SAAS;AACxC,QAAI,CAAC,KAAM,OAAM,IAAI,MAAM,2BAA2B;AACtD,WAAO,KAAK,iBAAiB,KAAK,SAAS,EAAE,EAAE;AAAA,EACjD;AACF;AAEO,IAAM,sCAAN,cAAkD,0BAA6C;AAAA,EACpG,IAAW,SAA4B;AACrC,WAAO;AAAA,EACT;AACF;AAEO,IAAM,oCAAN,cAAgD,0BAA2C;AAAA,EAChG,IAAW,SAA0B;AACnC,WAAO;AAAA,EACT;AACF;AAEO,IAAM,+BAAN,cAA2C,sBAAiC;AAAA,EACjF,IAAW,SAAoB;AAC7B,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,MAA4C;AACtD,UAAM,IAAI;AAAA,EACZ;AAAA,EAEO,SAAgB;AACrB,UAAM,IAAI,MAAM,uCAAuC;AAAA,EACzD;AACF;","names":[]}