import { SimpleLocalClient } from '../../core/index.js';
import { HitronCodaMapper } from './hitron_coda.mapper.js';
import type { IHitronCodaConfig, IHitronCodaSnapshot } from './hitron_coda.types.js';
import { hitronCodaProfile } from './hitron_coda.types.js';

export class HitronCodaClient extends SimpleLocalClient<IHitronCodaConfig> {
  constructor(private readonly configArg: IHitronCodaConfig) {
    super(hitronCodaProfile, configArg);
  }

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

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