import { SimpleLocalClient } from '../../core/index.js';
import { GoveeLightLocalMapper } from './govee_light_local.mapper.js';
import type { IGoveeLightLocalConfig, IGoveeLightLocalSnapshot } from './govee_light_local.types.js';
import { goveeLightLocalProfile } from './govee_light_local.types.js';

export class GoveeLightLocalClient extends SimpleLocalClient<IGoveeLightLocalConfig> {
  constructor(private readonly configArg: IGoveeLightLocalConfig) {
    super(goveeLightLocalProfile, localRuntimeConfig(configArg));
  }

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

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

const localRuntimeConfig = (configArg: IGoveeLightLocalConfig): IGoveeLightLocalConfig => configArg.rawData !== undefined || configArg.entities?.length || configArg.snapshot ? { ...configArg, host: undefined, path: undefined, transport: 'snapshot' } : configArg;
