{"version":3,"file":"join.mjs","names":[],"sources":["../../src/operations/join.ts"],"sourcesContent":["import type { SingleConnection } from \"~/client/connection\";\nimport { awaitResponse } from \"~/await/await-response\";\nimport { MessageError } from \"~/client/errors\";\nimport { JoinMessage } from \"~/message/twitch-types/membership/join\";\nimport { NoticeMessage } from \"~/message/twitch-types/notice\";\n\nexport class JoinError extends MessageError {\n  public readonly failedChannelName: string;\n\n  public constructor(\n    failedChannelName: string,\n    message: string,\n    cause?: Error,\n  ) {\n    super(message, cause);\n    this.failedChannelName = failedChannelName;\n  }\n}\n\nexport async function awaitJoinResponse(\n  conn: SingleConnection,\n  channelName: string,\n): Promise<JoinMessage> {\n  return awaitResponse(conn, {\n    success: (message): message is JoinMessage =>\n      message instanceof JoinMessage &&\n      message.channel.login === channelName &&\n      message.joinedUsername === conn.configuration.username,\n    failure: (message) =>\n      message instanceof NoticeMessage &&\n      message.channel?.login === channelName &&\n      message.id === \"msg_channel_suspended\",\n    errorType: (message, cause) => new JoinError(channelName, message, cause),\n    errorMessage: `Failed to join channel ${channelName}`,\n  });\n}\n\nexport function joinNothingToDo(\n  conn: SingleConnection,\n  channelName: string,\n): boolean {\n  return (\n    conn.wantedChannels.has(channelName) && conn.joinedChannels.has(channelName)\n  );\n}\n\nexport async function joinChannel(\n  conn: SingleConnection,\n  channelName: string,\n): Promise<JoinMessage | undefined> {\n  if (joinNothingToDo(conn, channelName)) {\n    return;\n  }\n\n  conn.wantedChannels.add(channelName);\n  conn.sendRaw(`JOIN #${channelName}`);\n  const response = await awaitJoinResponse(conn, channelName);\n  conn.joinedChannels.add(channelName);\n  return response;\n}\n"],"mappings":";;;;;AAMA,IAAa,YAAb,cAA+B,aAAa;CAC1C;CAEA,YACE,mBACA,SACA,OACA;AACA,QAAM,SAAS,MAAM;AACrB,OAAK,oBAAoB;;;AAI7B,eAAsB,kBACpB,MACA,aACsB;AACtB,QAAO,cAAc,MAAM;EACzB,UAAU,YACR,mBAAmB,eACnB,QAAQ,QAAQ,UAAU,eAC1B,QAAQ,mBAAmB,KAAK,cAAc;EAChD,UAAU,YACR,mBAAmB,iBACnB,QAAQ,SAAS,UAAU,eAC3B,QAAQ,OAAO;EACjB,YAAY,SAAS,UAAU,IAAI,UAAU,aAAa,SAAS,MAAM;EACzE,cAAc,0BAA0B;EACzC,CAAC;;AAGJ,SAAgB,gBACd,MACA,aACS;AACT,QACE,KAAK,eAAe,IAAI,YAAY,IAAI,KAAK,eAAe,IAAI,YAAY;;AAIhF,eAAsB,YACpB,MACA,aACkC;AAClC,KAAI,gBAAgB,MAAM,YAAY,CACpC;AAGF,MAAK,eAAe,IAAI,YAAY;AACpC,MAAK,QAAQ,SAAS,cAAc;CACpC,MAAM,WAAW,MAAM,kBAAkB,MAAM,YAAY;AAC3D,MAAK,eAAe,IAAI,YAAY;AACpC,QAAO"}