import { DoFetch, PlatformError } from '../common';

export enum DataEventObjectType {
  MODEL = 'model',
}

export interface ModelData {
  id: string;
  object: DataEventObjectType,
  owned_by: string;
  permission: string[];
}

export function listModels(
  this: NodeJS.EventEmitter,
  doFetch: DoFetch,
) {
  doFetch('GET', '/models')
    .then(async (response) => {
      if (!response.ok) {
        this.emit('error', new PlatformError(
          // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
          `Request from platform returned with status: ${response.status}`,
          response,
        ));
        this.emit('end');
        return;
      }

      const responseData = await response.json() as Record<string, unknown>;
      this.emit('data', responseData.data as ModelData[]);
      this.emit('end');
    })
    .catch((err) => {
      this.emit('error', err as Error);
      this.emit('end');
    });

  return this;
}
