import { SimpleLocalClient } from '../../core/index.js';
import { EmulatedKasaMapper } from './emulated_kasa.mapper.js';
import type { IEmulatedKasaConfig, IEmulatedKasaSnapshot } from './emulated_kasa.types.js';
import { emulatedKasaProfile } from './emulated_kasa.types.js';

export class EmulatedKasaClient extends SimpleLocalClient<IEmulatedKasaConfig> {
  constructor(private readonly configArg: IEmulatedKasaConfig) {
    super(emulatedKasaProfile, configArg);
  }

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

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