import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useEffect, useState } from 'react';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { useSmartCardActions as useSmartLinkActions } from '../actions';
import { useSmartLinkConfig } from '../config';
import { useSmartLinkRenderers } from '../renderers';
import { useSmartCardState as useSmartLinkState } from '../store';
export function useSmartLink(id, url) {
  var state = useSmartLinkState(url);
  var _useSmartLinkContext = useSmartLinkContext(),
    store = _useSmartLinkContext.store,
    isPreviewPanelAvailable = _useSmartLinkContext.isPreviewPanelAvailable,
    openPreviewPanel = _useSmartLinkContext.openPreviewPanel;
  var actions = useSmartLinkActions(id, url);
  var config = useSmartLinkConfig();
  var renderers = useSmartLinkRenderers();

  // NB: used to propagate errors from hooks to error boundaries.
  var _useState = useState(null),
    _useState2 = _slicedToArray(_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
  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
  };
}