import { SimpleLocalClient } from '../../core/index.js';
import { HlkSw16Mapper } from './hlk_sw16.mapper.js';
import type { IHlkSw16Config, IHlkSw16Snapshot } from './hlk_sw16.types.js';
import { hlkSw16Profile } from './hlk_sw16.types.js';

export class HlkSw16Client extends SimpleLocalClient<IHlkSw16Config> {
  constructor(private readonly configArg: IHlkSw16Config) {
    super(hlkSw16Profile, configArg);
  }

  public async getSnapshot(forceRefreshArg = false): Promise<IHlkSw16Snapshot> {
    if (this.configArg.rawData !== undefined || this.configArg.rawText !== undefined || this.configArg.entities?.length) {
      return HlkSw16Mapper.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 HlkSw16Mapper.toSnapshot({ config: { ...this.configArg, snapshot }, online: snapshot.online, source: snapshot.source, error: snapshot.error });
    }

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