import * as O from 'fp-ts/lib/Option';
import wretch from 'wretch';
import { Lazy } from '../../../utils';
import sessionStore from './session.store';
class AppConfigurationService {
  constructor() {
    this._appRootUrl = new Lazy(() => this.resolveRootUrl());
    this.getAppRootUrl = (withSlash = true) => this._appRootUrl.value + (withSlash ? '/' : '');
    this.getAppHostUrl = () => `${window.location.origin}${this.getAppRootUrl()}`.replace(/\/$/, '');
  }
  async loadConfigurationAsync(configurationPath) {
    const configuration = await wretch(configurationPath).get().json();
    sessionStore.update({ appConfiguration: O.fromNullable(configuration) });
  }
  resolveRootUrl() {
    const match = O.fromNullable(window.location.pathname.match(/^\/benefits\/embed\/v1.0\/web/));
    if (O.isNone(match)) {
      throw Error('Unsupported host URL');
    }
    return match.value[0];
  }
}
export default new AppConfigurationService();
