{"version":3,"file":"ActionMenuApi.mjs","names":[],"sources":["../src/ActionMenuApi.ts"],"sourcesContent":["import { AgentContext, CredoError, injectable } from '@credo-ts/core'\nimport { DidCommConnectionService, DidCommMessageSender, getOutboundDidCommMessageContext } from '@credo-ts/didcomm'\nimport type {\n  ClearActiveMenuOptions,\n  FindActiveMenuOptions,\n  PerformActionOptions,\n  RequestMenuOptions,\n  SendMenuOptions,\n} from './ActionMenuApiOptions'\n\nimport { ActionMenuRole } from './ActionMenuRole'\nimport { ActionMenuService } from './services'\n\n/**\n * @public\n */\n@injectable()\nexport class ActionMenuApi {\n  private connectionService: DidCommConnectionService\n  private messageSender: DidCommMessageSender\n  private actionMenuService: ActionMenuService\n  private agentContext: AgentContext\n\n  public constructor(\n    connectionService: DidCommConnectionService,\n    messageSender: DidCommMessageSender,\n    actionMenuService: ActionMenuService,\n    agentContext: AgentContext\n  ) {\n    this.connectionService = connectionService\n    this.messageSender = messageSender\n    this.actionMenuService = actionMenuService\n    this.agentContext = agentContext\n  }\n\n  /**\n   * Start Action Menu protocol as requester, asking for root menu. Any active menu will be cleared.\n   *\n   * @param options options for requesting menu\n   * @returns Action Menu record associated to this new request\n   */\n  public async requestMenu(options: RequestMenuOptions) {\n    const connection = await this.connectionService.getById(this.agentContext, options.connectionId)\n\n    const { message, record } = await this.actionMenuService.createRequest(this.agentContext, {\n      connection,\n    })\n\n    const outboundMessageContext = await getOutboundDidCommMessageContext(this.agentContext, {\n      message,\n      associatedRecord: record,\n      connectionRecord: connection,\n    })\n\n    await this.messageSender.sendMessage(outboundMessageContext)\n\n    return record\n  }\n\n  /**\n   * Send a new Action Menu as responder. This menu will be sent as response if there is an\n   * existing menu thread.\n   *\n   * @param options options for sending menu\n   * @returns Action Menu record associated to this action\n   */\n  public async sendMenu(options: SendMenuOptions) {\n    const connection = await this.connectionService.getById(this.agentContext, options.connectionId)\n\n    const { message, record } = await this.actionMenuService.createMenu(this.agentContext, {\n      connection,\n      menu: options.menu,\n    })\n\n    const outboundMessageContext = await getOutboundDidCommMessageContext(this.agentContext, {\n      message,\n      associatedRecord: record,\n      connectionRecord: connection,\n    })\n\n    await this.messageSender.sendMessage(outboundMessageContext)\n\n    return record\n  }\n\n  /**\n   * Perform action in active Action Menu, as a requester. The related\n   * menu will be closed.\n   *\n   * @param options options for requesting menu\n   * @returns Action Menu record associated to this selection\n   */\n  public async performAction(options: PerformActionOptions) {\n    const connection = await this.connectionService.getById(this.agentContext, options.connectionId)\n\n    const actionMenuRecord = await this.actionMenuService.find(this.agentContext, {\n      connectionId: connection.id,\n      role: ActionMenuRole.Requester,\n    })\n    if (!actionMenuRecord) {\n      throw new CredoError(`No active menu found for connection id ${options.connectionId}`)\n    }\n\n    const { message, record } = await this.actionMenuService.createPerform(this.agentContext, {\n      actionMenuRecord,\n      performedAction: options.performedAction,\n    })\n\n    const outboundMessageContext = await getOutboundDidCommMessageContext(this.agentContext, {\n      message,\n      associatedRecord: record,\n      connectionRecord: connection,\n    })\n\n    await this.messageSender.sendMessage(outboundMessageContext)\n\n    return record\n  }\n\n  /**\n   * Find the current active menu for a given connection and the specified role.\n   *\n   * @param options options for requesting active menu\n   * @returns Active Action Menu record, or null if no active menu found\n   */\n  public async findActiveMenu(options: FindActiveMenuOptions) {\n    return this.actionMenuService.find(this.agentContext, {\n      connectionId: options.connectionId,\n      role: options.role,\n    })\n  }\n\n  /**\n   * Clears the current active menu for a given connection and the specified role.\n   *\n   * @param options options for clearing active menu\n   * @returns Active Action Menu record, or null if no active menu record found\n   */\n  public async clearActiveMenu(options: ClearActiveMenuOptions) {\n    const actionMenuRecord = await this.actionMenuService.find(this.agentContext, {\n      connectionId: options.connectionId,\n      role: options.role,\n    })\n\n    return actionMenuRecord ? await this.actionMenuService.clearMenu(this.agentContext, { actionMenuRecord }) : null\n  }\n}\n"],"mappings":";;;;;;;;;;AAiBO,0BAAM,cAAc;CAMzB,AAAO,YACL,mBACA,eACA,mBACA,cACA;AACA,OAAK,oBAAoB;AACzB,OAAK,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