import { SimpleLocalClient } from '../../core/index.js';
import { EmulatedRokuMapper } from './emulated_roku.mapper.js';
import type { IEmulatedRokuConfig, IEmulatedRokuSnapshot } from './emulated_roku.types.js';
import { emulatedRokuProfile } from './emulated_roku.types.js';

export class EmulatedRokuClient extends SimpleLocalClient<IEmulatedRokuConfig> {
  constructor(private readonly configArg: IEmulatedRokuConfig) {
    super(emulatedRokuProfile, configArg);
  }

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

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