import { pipe } from 'fp-ts/lib/function';
import * as O from 'fp-ts/Option';
import iframe from '../../../utils/iframe';
class NavigationService {
  constructor() {
    this._baseUrl = O.none;
    this._history = O.none;
  }
  navigateInternally(route) {
    const path = `${this.baseUrl}${this.toRouteWithLeadingSlash(route)}`;
    iframe.navigation(path);
    this.history.value.push(path);
  }
  toRouteWithLeadingSlash(route) {
    return route.length > 0 && route[0] === '/' ? route : `/${route}`;
  }
  set baseUrl(baseUrl) {
    this._baseUrl = pipe(O.fromNullable(baseUrl), O.map((value) => value.replace(/\/$/, '')));
  }
  get baseUrl() {
    if (O.isNone(this._baseUrl)) {
      throw Error(`Base URL is missing.`);
    }
    return this._baseUrl.value;
  }
  set history(history) {
    this._history = history;
  }
  get history() {
    if (O.isNone(this._history)) {
      throw Error(`Router history is missing.`);
    }
    return this._history;
  }
}
export default new NavigationService();
