import { SimpleLocalClient } from '../../core/index.js';
import { GoalzeroMapper } from './goalzero.mapper.js';
import type { IGoalzeroConfig, IGoalzeroSnapshot } from './goalzero.types.js';
import { goalzeroProfile } from './goalzero.types.js';

export class GoalzeroClient extends SimpleLocalClient<IGoalzeroConfig> {
  constructor(private readonly configArg: IGoalzeroConfig) {
    super(goalzeroProfile, configArg);
  }

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