import { SimpleLocalClient } from '../../core/index.js';
import { HdmiCecMapper } from './hdmi_cec.mapper.js';
import type { IHdmiCecConfig, IHdmiCecSnapshot } from './hdmi_cec.types.js';
import { hdmiCecProfile } from './hdmi_cec.types.js';

export class HdmiCecClient extends SimpleLocalClient<IHdmiCecConfig> {
  constructor(private readonly configArg: IHdmiCecConfig) {
    super(hdmiCecProfile, configArg);
  }

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

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