"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DiscoverSearchSessionManager = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _operators = require("rxjs/operators");
var _public = require("@kbn/kibana-utils-plugin/public");
var _constants = require("../../../constants");
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

/**
 * Helps with state management of search session and {@link SEARCH_SESSION_ID_QUERY_PARAM} in the URL
 */
class DiscoverSearchSessionManager {
  /**
   * Notifies about `searchSessionId` changes in the URL,
   * skips if `searchSessionId` matches current search session id
   */

  constructor(deps) {
    (0, _defineProperty2.default)(this, "newSearchSessionIdFromURL$", void 0);
    (0, _defineProperty2.default)(this, "searchSessionId$", void 0);
    (0, _defineProperty2.default)(this, "deps", void 0);
    (0, _defineProperty2.default)(this, "getSearchSessionIdFromURL", () => (0, _public.getQueryParams)(this.deps.history.location)[_constants.SEARCH_SESSION_ID_QUERY_PARAM]);
    this.deps = deps;
    this.newSearchSessionIdFromURL$ = (0, _public.createQueryParamObservable)(this.deps.history, _constants.SEARCH_SESSION_ID_QUERY_PARAM).pipe((0, _operators.filter)(searchSessionId => {
      if (!searchSessionId) return true;
      return !this.deps.session.isCurrentSession(searchSessionId);
    }));
    this.searchSessionId$ = this.deps.session.getSession$();
  }

  /**
   * Get next session id by either starting or restoring a session.
   * When navigating away from the restored session {@link SEARCH_SESSION_ID_QUERY_PARAM} is removed from the URL using history.replace
   */
  getNextSearchSessionId() {
    var _searchSessionIdFromU;
    let searchSessionIdFromURL = this.getSearchSessionIdFromURL();
    if (searchSessionIdFromURL) {
      if (this.deps.session.isRestore() && this.deps.session.isCurrentSession(searchSessionIdFromURL)) {
        // navigating away from a restored session
        this.removeSearchSessionIdFromURL({
          replace: true
        });
        searchSessionIdFromURL = undefined;
      } else {
        this.deps.session.restore(searchSessionIdFromURL);
      }
    }
    return (_searchSessionIdFromU = searchSessionIdFromURL) !== null && _searchSessionIdFromU !== void 0 ? _searchSessionIdFromU : this.deps.session.start();
  }

  /**
   * Get current search session id
   */
  getCurrentSearchSessionId() {
    return this.deps.session.getSessionId();
  }

  /**
   * Removes Discovers {@link SEARCH_SESSION_ID_QUERY_PARAM} from the URL
   * @param replace - methods to change the URL
   */
  removeSearchSessionIdFromURL({
    replace = true
  } = {
    replace: true
  }) {
    if (this.hasSearchSessionIdInURL()) {
      (0, _public.removeQueryParam)(this.deps.history, _constants.SEARCH_SESSION_ID_QUERY_PARAM, replace);
    }
  }

  /**
   * If there is a {@link SEARCH_SESSION_ID_QUERY_PARAM} currently in the URL
   */
  hasSearchSessionIdInURL() {
    return !!this.getSearchSessionIdFromURL();
  }
}
exports.DiscoverSearchSessionManager = DiscoverSearchSessionManager;