"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.store = exports.State = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var State = exports.State = function (State) {
  State[State["IDLE"] = 0] = "IDLE";
  State[State["BECOMING_VISIBLE"] = 1] = "BECOMING_VISIBLE";
  State[State["VISIBLE"] = 2] = "VISIBLE";
  State[State["LEAVING_VISIBLE"] = 3] = "LEAVING_VISIBLE";
  return State;
}({});
var TooltipStore = _createClass(function TooltipStore() {
  var _this = this;
  _classCallCheck(this, TooltipStore);
  _defineProperty(this, "state", void 0);
  _defineProperty(this, "contextId", void 0);
  _defineProperty(this, "becomingVisibleTimeout", void 0);
  _defineProperty(this, "leavingVisibleTimeout", void 0);
  _defineProperty(this, "subscriptions", void 0);
  _defineProperty(this, "subscribe", function (fn) {
    _this.subscriptions.push(fn);
    return function () {
      _this.subscriptions.splice(_this.subscriptions.indexOf(fn), 1);
    };
  });
  _defineProperty(this, "notify", function () {
    _this.subscriptions.forEach(function (fn) {
      return fn();
    });
  });
  _defineProperty(this, "setState", function (newState, contextId) {
    var prevState = _this.state;
    if (prevState === State.BECOMING_VISIBLE) {
      window.clearTimeout(_this.becomingVisibleTimeout);
    } else if (prevState === State.LEAVING_VISIBLE) {
      window.clearTimeout(_this.leavingVisibleTimeout);
      _this.contextId = null;
    }
    _this.state = newState;
    if (newState === State.IDLE) {
      _this.contextId = null;
    }
    if (contextId) {
      _this.contextId = contextId;
    }
    _this.notify();
  });
  _defineProperty(this, "startBecomingVisibleTimeout", function (id, openDelay) {
    window.clearTimeout(_this.becomingVisibleTimeout);
    _this.setState(State.BECOMING_VISIBLE, id);
    _this.becomingVisibleTimeout = window.setTimeout(function () {
      _this.setState(State.VISIBLE, id);
    }, openDelay);
  });
  _defineProperty(this, "clearBecomingVisibleTimeout", function () {
    window.clearTimeout(_this.becomingVisibleTimeout);
  });
  _defineProperty(this, "startLeavingVisibleTimeout", function (closeDelay) {
    window.clearTimeout(_this.leavingVisibleTimeout);
    _this.setState(State.LEAVING_VISIBLE);
    _this.leavingVisibleTimeout = window.setTimeout(function () {
      _this.setState(State.IDLE);
    }, closeDelay);
  });
  _defineProperty(this, "isVisible", function () {
    return [State.LEAVING_VISIBLE, State.VISIBLE].includes(_this.state);
  });
  this.state = State.IDLE;
  this.contextId = null;
  this.becomingVisibleTimeout = undefined;
  this.leavingVisibleTimeout = undefined;
  this.subscriptions = [];
});
var store = exports.store = new TooltipStore();