import { SimpleLocalClient } from '../../core/index.js';
import { HolidayMapper } from './holiday.mapper.js';
import type { IHolidayConfig, IHolidaySnapshot } from './holiday.types.js';
import { holidayProfile } from './holiday.types.js';

export class HolidayClient extends SimpleLocalClient<IHolidayConfig> {
  constructor(private readonly configArg: IHolidayConfig) {
    super(holidayProfile, configArg);
  }

  public async getSnapshot(forceRefreshArg = false): Promise<IHolidaySnapshot> {
    if (this.configArg.rawData !== undefined || this.configArg.rawText !== undefined || this.configArg.entities?.length) {
      return HolidayMapper.toSnapshot({ config: this.configArg, rawData: this.configArg.rawData ?? this.configArg.rawText, online: this.configArg.online ?? true, source: 'manual' });
    }

    const snapshot = await super.getSnapshot(forceRefreshArg);
    if (snapshot.rawData === undefined && snapshot.entities.length) {
      return HolidayMapper.toSnapshot({ config: { ...this.configArg, snapshot }, online: snapshot.online, source: snapshot.source, error: snapshot.error });
    }

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