import { SimpleLocalClient } from '../../core/index.js';
import { GenericMapper } from './generic.mapper.js';
import type { IGenericConfig, IGenericSnapshot } from './generic.types.js';
import { genericProfile } from './generic.types.js';

export class GenericClient extends SimpleLocalClient<IGenericConfig> {
  constructor(private readonly configArg: IGenericConfig) {
    super(genericProfile, configArg);
  }

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