import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function isUndefined(x) {
  return x === undefined;
}
var ProviderFactory = /*#__PURE__*/function () {
  function ProviderFactory() {
    _classCallCheck(this, ProviderFactory);
    _defineProperty(this, "providers", new Map());
    _defineProperty(this, "subscribers", new Map());
  }
  _createClass(ProviderFactory, [{
    key: "destroy",
    value: function destroy() {
      this.providers.clear();
      this.subscribers.clear();
    }
  }, {
    key: "isEmpty",
    value: function isEmpty() {
      return !this.providers.size && !this.subscribers.size;
    }
  }, {
    key: "setProvider",
    value: function setProvider(name, provider) {
      // Do not trigger notifyUpdate if provider is the same.
      if (this.providers.get(name) === provider) {
        return;
      }
      if (!isUndefined(provider)) {
        this.providers.set(name, provider);
      } else {
        this.providers.delete(name);
      }
      this.notifyUpdated(name, provider);
    }
  }, {
    key: "removeProvider",
    value: function removeProvider(name) {
      this.providers.delete(name);
      this.notifyUpdated(name);
    }
  }, {
    key: "subscribe",
    value: function subscribe(name, handler) {
      var handlers = this.subscribers.get(name) || [];
      handlers.push(handler);
      this.subscribers.set(name, handlers);
      var provider = this.providers.get(name);
      if (provider) {
        handler(name, provider);
      }
    }
  }, {
    key: "unsubscribe",
    value: function unsubscribe(name, handler) {
      var handlers = this.subscribers.get(name);
      if (!handlers) {
        return;
      }
      var index = handlers.indexOf(handler);
      if (index !== -1) {
        handlers.splice(index, 1);
      }
      if (handlers.length === 0) {
        this.subscribers.delete(name);
      } else {
        this.subscribers.set(name, handlers);
      }
    }
  }, {
    key: "unsubscribeAll",
    value: function unsubscribeAll(name) {
      var handlers = this.subscribers.get(name);
      if (!handlers) {
        return;
      }
      this.subscribers.delete(name);
    }
  }, {
    key: "hasProvider",
    value: function hasProvider(name) {
      return this.providers.has(name);
    }
  }, {
    key: "notifyUpdated",
    value: function notifyUpdated(name, provider) {
      var handlers = this.subscribers.get(name);
      if (!handlers) {
        return;
      }
      handlers.forEach(function (handler) {
        handler(name, provider);
      });
    }
  }], [{
    key: "create",
    value: function create(providers) {
      var providerFactory = new ProviderFactory();
      var keys = Object.keys(providers);
      keys.forEach(function (name) {
        providerFactory.setProvider(name, providers[name]);
      });
      return providerFactory;
    }
  }]);
  return ProviderFactory;
}();
export { ProviderFactory as default };