import { SimpleLocalClient } from '../../core/index.js';
import { EmulatedHueMapper } from './emulated_hue.mapper.js';
import type { IEmulatedHueConfig, IEmulatedHueSnapshot } from './emulated_hue.types.js';
import { emulatedHueProfile } from './emulated_hue.types.js';

export class EmulatedHueClient extends SimpleLocalClient<IEmulatedHueConfig> {
  constructor(private readonly configArg: IEmulatedHueConfig) {
    super(emulatedHueProfile, configArg);
  }

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

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