import { SimpleLocalClient } from '../../core/index.js';
import { Enigma2Mapper } from './enigma2.mapper.js';
import type { IEnigma2Config, IEnigma2Snapshot } from './enigma2.types.js';
import { enigma2Profile } from './enigma2.types.js';

export class Enigma2Client extends SimpleLocalClient<IEnigma2Config> {
  constructor(private readonly configArg: IEnigma2Config) {
    super(enigma2Profile, configArg);
  }

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

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