import { SimpleLocalClient } from '../../core/index.js';
import { EmonitorMapper } from './emonitor.mapper.js';
import type { IEmonitorConfig, IEmonitorSnapshot } from './emonitor.types.js';
import { emonitorProfile } from './emonitor.types.js';

export class EmonitorClient extends SimpleLocalClient<IEmonitorConfig> {
  constructor(private readonly configArg: IEmonitorConfig) {
    super(emonitorProfile, configArg);
  }

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

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