import { SimpleLocalClient } from '../../core/index.js';
import { EgardiaMapper } from './egardia.mapper.js';
import type { IEgardiaConfig, IEgardiaSnapshot } from './egardia.types.js';
import { egardiaProfile } from './egardia.types.js';

export class EgardiaClient extends SimpleLocalClient<IEgardiaConfig> {
  constructor(private readonly configArg: IEgardiaConfig) {
    super(egardiaProfile, configArg);
  }

  public async getSnapshot(forceRefreshArg = false): Promise<IEgardiaSnapshot> {
    const snapshot = await super.getSnapshot(forceRefreshArg);
    if (snapshot.rawData === undefined && snapshot.entities.length) {
      return EgardiaMapper.toSnapshot({ config: { ...this.configArg, snapshot }, online: snapshot.online, source: snapshot.source, error: snapshot.error });
    }

    return EgardiaMapper.toSnapshot({ config: this.configArg, rawData: snapshot.rawData, online: snapshot.online, source: snapshot.source, error: snapshot.error });
  }
}
