import {
  SdkBase,
  E_RESPONSE_STATUS,
  E_RESPONSE_NO_VALUE,
  E_INVALID_JSON,
  E_RESPONSE_UNKNOWN,
  SdkOptions,
} from './SdkBase.js'
import { RouteBuilder } from './RouteBuilder.js'
import { IFile } from '../../models/store/File.js'
import { SdkError } from './Errors.js'
import { ListFileKind } from './FilesSdk.js'
import { ContextListOptions, ContextListResult } from '../../events/BaseEvents.js'

export class SharedSdk extends SdkBase {
  /**
   * Lists shared with the user files.
   *
   * @param kinds the list of kinds to list.
   * @param options Optional query options.
   * @param request Optional request options.
   */
  async list(
    organization: string,
    kinds?: ListFileKind[],
    options?: ContextListOptions,
    request: SdkOptions = {}
  ): Promise<ContextListResult<IFile>> {
    const { token } = request
    const url = this.sdk.getUrl(RouteBuilder.shared(organization))
    this.sdk.appendListOptions(url, options)
    if (Array.isArray(kinds)) {
      kinds.forEach((k) => url.searchParams.append('kind', k))
    }
    const result = await this.sdk.http.get(url.toString(), { token })
    this.inspectCommonStatusCodes(result)
    const E_PREFIX = 'Unable to list organizations. '
    if (result.status !== 200) {
      this.logInvalidResponse(result)
      let e = this.createGenericSdkError(result.body)
      if (!e) {
        e = new SdkError(`${E_PREFIX}${E_RESPONSE_STATUS}${result.status}`, result.status)
        e.response = result.body
      }
      throw e
    }
    if (!result.body) {
      throw new SdkError(`${E_PREFIX}${E_RESPONSE_NO_VALUE}`, 0)
    }
    let data: ContextListResult<IFile>
    try {
      data = JSON.parse(result.body)
    } catch {
      const err = new SdkError(`${E_PREFIX}${E_INVALID_JSON}.`, 0)
      err.response = result.body
      throw err
    }
    if (!Array.isArray(data.items)) {
      const err = new SdkError(`${E_PREFIX}${E_RESPONSE_UNKNOWN}.`, 0)
      err.response = result.body
      throw err
    }
    return data
  }
}
