/**
 * LINE Messaging API
 * This document describes LINE Messaging API.
 *
 * The version of the OpenAPI document: 0.0.1
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

/* tslint:disable:no-unused-locals */
import { AcquireChatControlRequest } from "../model/acquireChatControlRequest.js";
import { DetachModuleRequest } from "../model/detachModuleRequest.js";
import { GetModulesResponse } from "../model/getModulesResponse.js";

import * as Types from "../../types.js";
import { ensureJSON, buildPath } from "../../utils.js";
import { Readable } from "node:stream";

import HTTPFetchClient, {
  convertResponseToReadable,
  mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
// This file is autogenerated - Please do not edit
// ===============================================

interface httpClientConfig {
  /**
   * Base URL for requests.
   * Defaults to 'https://api.line.me'.
   * You can override this for testing or to use a mock server.
   */
  baseURL?: string;
  /**
   * Channel access token used for authorization.
   */
  channelAccessToken: string;
  /**
   * Extra headers merged into every request.
   */
  defaultHeaders?: Record<string, string>;
}

/**
 * @see {@link LineBotClient} for a unified interface that wraps this client.
 */

export class LineModuleClient {
  private httpClient: HTTPFetchClient;

  /**
   * Initializes a new `LineModuleClient`.
   *
   * @param config Configuration for this API client.
   * @param config.baseURL The base URL for requests. Defaults to `https://api.line.me`.
   * @param config.channelAccessToken The channel access token used for authorization.
   * @param config.defaultHeaders Extra headers merged into every request.
   *
   * @example
   * const client = new LineModuleClient({
   *   channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN!,
   * });
   */
  constructor(config: httpClientConfig) {
    const baseURL = config.baseURL || "https://api.line.me";
    const defaultHeaders = mergeHeaders(config.defaultHeaders, {
      Authorization: "Bearer " + config.channelAccessToken,
    });
    this.httpClient = new HTTPFetchClient({
      defaultHeaders: defaultHeaders,
      baseURL: baseURL,
    });
  }

  /**
   * If the Standby Channel wants to take the initiative (Chat Control), it calls the Acquire Control API. The channel that was previously an Active Channel will automatically switch to a Standby Channel.
   * Calls `POST https://api.line.me/v2/bot/chat/{chatId}/control/acquire`.
   * To inspect the HTTP status code or response headers, use {@link acquireChatControlWithHttpInfo}.
   * @param chatId The `userId`, `roomId`, or `groupId`
   * @param acquireChatControlRequest
   * @returns A promise resolving to the response body.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#acquire-control-api">LINE Developers documentation</a>
   */
  public async acquireChatControl(
    chatId: string,
    acquireChatControlRequest?: AcquireChatControlRequest,
  ): Promise<Types.MessageAPIResponseBase> {
    return (
      await this.acquireChatControlWithHttpInfo(
        chatId,
        acquireChatControlRequest,
      )
    ).body;
  }

  /**
   * If the Standby Channel wants to take the initiative (Chat Control), it calls the Acquire Control API. The channel that was previously an Active Channel will automatically switch to a Standby Channel.
   * Calls `POST https://api.line.me/v2/bot/chat/{chatId}/control/acquire`.
   * This method returns the response body together with the underlying `httpResponse`.
   * @param chatId The `userId`, `roomId`, or `groupId`
   * @param acquireChatControlRequest
   * @returns A promise resolving to the response body together with the underlying `httpResponse`.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#acquire-control-api">LINE Developers documentation</a>
   */
  public async acquireChatControlWithHttpInfo(
    chatId: string,
    acquireChatControlRequest?: AcquireChatControlRequest,
  ): Promise<Types.ApiResponseType<Types.MessageAPIResponseBase>> {
    const requestPath = buildPath("/v2/bot/chat/{chatId}/control/acquire", {
      chatId: chatId,
    });

    const params = acquireChatControlRequest;

    const res = await this.httpClient.post(requestPath, params);
    const text = await res.text();
    const parsedBody = text ? JSON.parse(text) : null;
    return { httpResponse: res, body: parsedBody };
  }
  /**
   * The module channel admin calls the Detach API to detach the module channel from a LINE Official Account.
   * Calls `POST https://api.line.me/v2/bot/channel/detach`.
   * To inspect the HTTP status code or response headers, use {@link detachModuleWithHttpInfo}.
   * @param detachModuleRequest
   * @returns A promise resolving to the response body.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#unlink-detach-module-channel-by-operation-mc-admin">LINE Developers documentation</a>
   */
  public async detachModule(
    detachModuleRequest?: DetachModuleRequest,
  ): Promise<Types.MessageAPIResponseBase> {
    return (await this.detachModuleWithHttpInfo(detachModuleRequest)).body;
  }

  /**
   * The module channel admin calls the Detach API to detach the module channel from a LINE Official Account.
   * Calls `POST https://api.line.me/v2/bot/channel/detach`.
   * This method returns the response body together with the underlying `httpResponse`.
   * @param detachModuleRequest
   * @returns A promise resolving to the response body together with the underlying `httpResponse`.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#unlink-detach-module-channel-by-operation-mc-admin">LINE Developers documentation</a>
   */
  public async detachModuleWithHttpInfo(
    detachModuleRequest?: DetachModuleRequest,
  ): Promise<Types.ApiResponseType<Types.MessageAPIResponseBase>> {
    const params = detachModuleRequest;

    const res = await this.httpClient.post("/v2/bot/channel/detach", params);
    const text = await res.text();
    const parsedBody = text ? JSON.parse(text) : null;
    return { httpResponse: res, body: parsedBody };
  }
  /**
   * Gets a list of basic information about the bots of multiple LINE Official Accounts that have attached module channels.
   * Calls `GET https://api.line.me/v2/bot/list`.
   * To inspect the HTTP status code or response headers, use {@link getModulesWithHttpInfo}.
   * @param start Value of the continuation token found in the next property of the JSON object returned in the response. If you can't get all basic information about the bots in one request, include this parameter to get the remaining array.
   * @param limit Specify the maximum number of bots that you get basic information from. The default value is 100. Max value: 100
   * @returns A promise resolving to the response body.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#get-multiple-bot-info-api">LINE Developers documentation</a>
   */
  public async getModules(
    start?: string,
    limit?: number,
  ): Promise<GetModulesResponse> {
    return (await this.getModulesWithHttpInfo(start, limit)).body;
  }

  /**
   * Gets a list of basic information about the bots of multiple LINE Official Accounts that have attached module channels.
   * Calls `GET https://api.line.me/v2/bot/list`.
   * This method returns the response body together with the underlying `httpResponse`.
   * @param start Value of the continuation token found in the next property of the JSON object returned in the response. If you can't get all basic information about the bots in one request, include this parameter to get the remaining array.
   * @param limit Specify the maximum number of bots that you get basic information from. The default value is 100. Max value: 100
   * @returns A promise resolving to the response body together with the underlying `httpResponse`.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#get-multiple-bot-info-api">LINE Developers documentation</a>
   */
  public async getModulesWithHttpInfo(
    start?: string,
    limit?: number,
  ): Promise<Types.ApiResponseType<GetModulesResponse>> {
    const queryParams = {
      start: start,
      limit: limit,
    };

    if (queryParams["start"] === undefined) {
      delete queryParams["start"];
    }

    if (queryParams["limit"] === undefined) {
      delete queryParams["limit"];
    }

    const res = await this.httpClient.get("/v2/bot/list", queryParams);
    const text = await res.text();
    const parsedBody = text ? JSON.parse(text) : null;
    return { httpResponse: res, body: parsedBody };
  }
  /**
   * To return the initiative (Chat Control) of Active Channel to Primary Channel, call the Release Control API.
   * Calls `POST https://api.line.me/v2/bot/chat/{chatId}/control/release`.
   * To inspect the HTTP status code or response headers, use {@link releaseChatControlWithHttpInfo}.
   * @param chatId The `userId`, `roomId`, or `groupId`
   * @returns A promise resolving to the response body.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#release-control-api">LINE Developers documentation</a>
   */
  public async releaseChatControl(
    chatId: string,
  ): Promise<Types.MessageAPIResponseBase> {
    return (await this.releaseChatControlWithHttpInfo(chatId)).body;
  }

  /**
   * To return the initiative (Chat Control) of Active Channel to Primary Channel, call the Release Control API.
   * Calls `POST https://api.line.me/v2/bot/chat/{chatId}/control/release`.
   * This method returns the response body together with the underlying `httpResponse`.
   * @param chatId The `userId`, `roomId`, or `groupId`
   * @returns A promise resolving to the response body together with the underlying `httpResponse`.
   * @see <a href="https://developers.line.biz/en/reference/partner-docs/#release-control-api">LINE Developers documentation</a>
   */
  public async releaseChatControlWithHttpInfo(
    chatId: string,
  ): Promise<Types.ApiResponseType<Types.MessageAPIResponseBase>> {
    const requestPath = buildPath("/v2/bot/chat/{chatId}/control/release", {
      chatId: chatId,
    });

    const res = await this.httpClient.post(requestPath);
    const text = await res.text();
    const parsedBody = text ? JSON.parse(text) : null;
    return { httpResponse: res, body: parsedBody };
  }
}
