"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useResolvedUserPreferences = useResolvedUserPreferences;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
/**
 * This hook returns the latest user preference
 * @param {UserPreferencesProvider} userPreferencesProvider an instance of UserPreferencesProvider
 * @example const { resolvedUserPreferences } = edUserPreferences(userPreferencesProvider)
 * @returns {ResolvedUserPreferences | null} the latest user preference
 */
function useResolvedUserPreferences(userPreferencesProvider) {
  var _useState = (0, _react.useState)((userPreferencesProvider === null || userPreferencesProvider === void 0 ? void 0 : userPreferencesProvider.getPreferences()) || null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    resolvedUserPreferences = _useState2[0],
    setResolvedUserPreferences = _useState2[1];
  (0, _react.useEffect)(function () {
    return userPreferencesProvider === null || userPreferencesProvider === void 0 ? void 0 : userPreferencesProvider.onUpdate(function () {
      setResolvedUserPreferences(userPreferencesProvider.getPreferences());
    });
  }, [userPreferencesProvider]);
  return {
    resolvedUserPreferences: resolvedUserPreferences
  };
}