import { IProjectExecutionLog } from '../models/ProjectExecution.js'
import { IRequestLog } from '../models/RequestLog.js'
import HttpProjectProxy, { IHttpProjectProxyInit, IHttpProjectStoreProxyInit } from './HttpProjectProxy.js'
import { IProxyResult } from './Proxy.js'
import RequestProxy, { IRequestProxyInit } from './RequestProxy.js'

export default class ProxyService {
  /**
   * Executes a single HTTP request.
   *
   * @param init The request to execute with additional configuration.
   * @returns The execution log for the request.
   */
  async proxyRequest(init: IRequestProxyInit): Promise<IProxyResult<IRequestLog>> {
    const proxy = new RequestProxy()
    await proxy.configure(init)
    return proxy.execute()
  }

  /**
   * Executes an HttpProject.
   *
   * @param init The project run configuration.
   * @param token The user access token to read data from the store. Required with `IHttpProjectStoreProxyInit`.
   * @param storeUri The store's base URI. Required with `IHttpProjectStoreProxyInit`.
   * @returns The key under which the proxy is cached.
   */
  async proxyHttpProject(
    init: IHttpProjectProxyInit | IHttpProjectStoreProxyInit,
    token?: string,
    storeUri?: string
  ): Promise<IProxyResult<IProjectExecutionLog>> {
    const proxy = new HttpProjectProxy()
    await proxy.configure(init, token, storeUri)
    return proxy.execute()
  }
}
