import { SimpleLocalClient } from '../../core/index.js';
import { FlicMapper } from './flic.mapper.js';
import type { IFlicConfig, IFlicSnapshot } from './flic.types.js';
import { flicProfile } from './flic.types.js';

export class FlicClient extends SimpleLocalClient<IFlicConfig> {
  constructor(private readonly configArg: IFlicConfig) {
    super(flicProfile, configArg);
  }

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

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