import { SimpleLocalClient } from '../../core/index.js';
import { FlexitBacnetMapper } from './flexit_bacnet.mapper.js';
import type { IFlexitBacnetConfig, IFlexitBacnetSnapshot } from './flexit_bacnet.types.js';
import { flexitBacnetProfile } from './flexit_bacnet.types.js';

export class FlexitBacnetClient extends SimpleLocalClient<IFlexitBacnetConfig> {
  constructor(private readonly configArg: IFlexitBacnetConfig) {
    super(flexitBacnetProfile, configArg);
  }

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

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