import * as O from 'fp-ts/Option';
import { getOrDefault } from '../../../utils';
import userApi from '../apis/user-api/user.api';
import { UserProfileDto } from '../apis/user-api/user-api.dto';
import benefitsAppStore from '../store/benefits-app.store';
export class BenefitsAppService {
  static set profileId(profileId) {
    this._profileId = O.fromNullable(profileId);
  }
  static get profileId() {
    if (O.isNone(this._profileId)) {
      throw Error(`ProfileId is missing.`);
    }
    return this._profileId.value;
  }
  static set accountId(accountId) {
    this._accountId = O.fromNullable(accountId);
  }
  static get accountIdOrDefault() {
    return getOrDefault(this._accountId, '');
  }
  async loadSignedInUserProfileAsync() {
    const user = UserProfileDto.toDomain(await userApi.getUserProfile());
    BenefitsAppService.accountId = user.basicProfile.accountId;
    benefitsAppStore.update((state) => {
      state.user = O.fromNullable(user);
    });
  }
  static get partnerIdOrDefault() {
    return getOrDefault(this._partnerId, '');
  }
}
BenefitsAppService._profileId = O.none;
BenefitsAppService._accountId = O.none;
BenefitsAppService._partnerId = O.none;
export default new BenefitsAppService();
