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) {
  const state = useSmartLinkState(url);
  const {
    store,
    isPreviewPanelAvailable,
    openPreviewPanel
  } = useSmartLinkContext();
  const actions = useSmartLinkActions(id, url);
  const config = useSmartLinkConfig();
  const renderers = useSmartLinkRenderers();

  // NB: used to propagate errors from hooks to error boundaries.
  const [error, setError] = useState(null);
  // Register the current card.
  const register = () => {
    actions.register().catch(err => 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,
    actions,
    config,
    renderers,
    error,
    isPreviewPanelAvailable,
    openPreviewPanel
  };
}