"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 _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _actions = require("../actions");
var _config = require("../config");
var _renderers = require("../renderers");
var _store = require("../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.
 */
function useSmartLink(id, url, appearance) {
  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 with appearance hint for optimized ORS response.
  var register = function register() {
    actions.register((0, _platformFeatureFlags.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
  (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
  };
}