import { SimpleLocalClient } from '../../core/index.js';
import { EnoceanMapper } from './enocean.mapper.js';
import type { IEnoceanConfig, IEnoceanSnapshot } from './enocean.types.js';
import { enoceanProfile } from './enocean.types.js';

export class EnoceanClient extends SimpleLocalClient<IEnoceanConfig> {
  constructor(private readonly configArg: IEnoceanConfig) {
    super(enoceanProfile, configArg);
  }

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

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