import { SimpleLocalClient } from '../../core/index.js';
import { HisenseAehw4a1Mapper } from './hisense_aehw4a1.mapper.js';
import type { IHisenseAehw4a1Config, IHisenseAehw4a1Snapshot } from './hisense_aehw4a1.types.js';
import { hisenseAehw4a1Profile } from './hisense_aehw4a1.types.js';

export class HisenseAehw4a1Client extends SimpleLocalClient<IHisenseAehw4a1Config> {
  constructor(private readonly configArg: IHisenseAehw4a1Config) {
    super(hisenseAehw4a1Profile, configArg);
  }

  public async getSnapshot(forceRefreshArg = false): Promise<IHisenseAehw4a1Snapshot> {
    if (this.configArg.rawData !== undefined || this.configArg.rawText !== undefined || this.configArg.entities?.length) {
      return HisenseAehw4a1Mapper.toSnapshot({ config: this.configArg, rawData: this.configArg.rawData ?? this.configArg.rawText, online: this.configArg.online ?? true, source: 'manual' });
    }

    const snapshot = await super.getSnapshot(forceRefreshArg);
    if (snapshot.rawData === undefined && snapshot.entities.length) {
      return HisenseAehw4a1Mapper.toSnapshot({ config: { ...this.configArg, snapshot }, online: snapshot.online, source: snapshot.source, error: snapshot.error });
    }

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