import { SimpleLocalClient } from '../../core/index.js';
import { EmoncmsHistoryMapper } from './emoncms_history.mapper.js';
import type { IEmoncmsHistoryConfig, IEmoncmsHistorySnapshot } from './emoncms_history.types.js';
import { emoncmsHistoryProfile } from './emoncms_history.types.js';

export class EmoncmsHistoryClient extends SimpleLocalClient<IEmoncmsHistoryConfig> {
  constructor(private readonly configArg: IEmoncmsHistoryConfig) {
    super(emoncmsHistoryProfile, configArg);
  }

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

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