import { SimpleLocalClient } from '../../core/index.js';
import { EnergeniePowerSocketsMapper } from './energenie_power_sockets.mapper.js';
import type { IEnergeniePowerSocketsConfig, IEnergeniePowerSocketsSnapshot } from './energenie_power_sockets.types.js';
import { energeniePowerSocketsProfile } from './energenie_power_sockets.types.js';

export class EnergeniePowerSocketsClient extends SimpleLocalClient<IEnergeniePowerSocketsConfig> {
  constructor(private readonly configArg: IEnergeniePowerSocketsConfig) {
    super(energeniePowerSocketsProfile, configArg);
  }

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

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