import { SimpleLocalClient } from '../../core/index.js';
import { HikvisioncamMapper } from './hikvisioncam.mapper.js';
import type { IHikvisioncamConfig, IHikvisioncamSnapshot } from './hikvisioncam.types.js';
import { hikvisioncamProfile } from './hikvisioncam.types.js';

export class HikvisioncamClient extends SimpleLocalClient<IHikvisioncamConfig> {
  constructor(private readonly configArg: IHikvisioncamConfig) {
    super(hikvisioncamProfile, configArg);
  }

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

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