import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useEffect, useState } from 'react';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { fg } from '@atlaskit/platform-feature-flags';
import { useSmartCardActions as useSmartLinkActions } from '../actions';
import { useSmartLinkConfig } from '../config';
import { useSmartLinkRenderers } from '../renderers';
import { useSmartCardState as useSmartLinkState } from '../store';

/**
 * Hook for smart link state and actions.
 * @param id - Unique identifier for the smart link
 * @param url - The URL to resolve
 * @param appearance - Card appearance hint for ORS to optimize response payload.
 *                     When 'inline', ORS returns minimal data (title, status).
 *                     When 'block' or 'embed', ORS returns full data including summary.
 */
export function useSmartLink(id, url, appearance) {
  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 with appearance hint for optimized ORS response.
  var register = function register() {
    actions.register(fg('platform_smartlink_inline_resolve_optimization') ? appearance : undefined).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
  };
}