"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSmartLink = useSmartLink;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _linkProvider = require("@atlaskit/link-provider");
var _actions = require("../actions");
var _config = require("../config");
var _renderers = require("../renderers");
var _store = require("../store");
function useSmartLink(id, url) {
  var state = (0, _store.useSmartCardState)(url);
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    store = _useSmartLinkContext.store,
    isPreviewPanelAvailable = _useSmartLinkContext.isPreviewPanelAvailable,
    openPreviewPanel = _useSmartLinkContext.openPreviewPanel;
  var actions = (0, _actions.useSmartCardActions)(id, url);
  var config = (0, _config.useSmartLinkConfig)();
  var renderers = (0, _renderers.useSmartLinkRenderers)();

  // NB: used to propagate errors from hooks to error boundaries.
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    error = _useState2[0],
    setError = _useState2[1];
  // Register the current card.
  var register = function register() {
    actions.register().catch(function (err) {
      return setError(err);
    });
  };
  // AFP-2511 TODO: Fix automatic suppressions below
  // eslint-disable-next-line react-hooks/exhaustive-deps
  (0, _react.useEffect)(register, [url, store]);

  // Provide the state and card actions to consumers.
  return {
    state: state,
    actions: actions,
    config: config,
    renderers: renderers,
    error: error,
    isPreviewPanelAvailable: isPreviewPanelAvailable,
    openPreviewPanel: openPreviewPanel
  };
}