import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["provider"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { useCallback, useMemo } from 'react';
import { useIntl } from 'react-intl';
import { messages } from '../../../messages';
import ImageIcon from '../../../view/common/image-icon';
import { showFlag } from '../../../view/Flag';
var FLAG_ICON_SIZE = '16px';
var renderIcon = function renderIcon(iconUrlOrElement) {
  if (!iconUrlOrElement) {
    return;
  }
  var icon = iconUrlOrElement && typeof iconUrlOrElement === 'string' ? /*#__PURE__*/React.createElement(ImageIcon, {
    height: FLAG_ICON_SIZE,
    testId: "sl-flag",
    url: iconUrlOrElement,
    width: FLAG_ICON_SIZE
  }) : iconUrlOrElement;
  return icon;
};

// Safe wrapper — returns null if IntlProvider is not in the tree
var useIntlSafe = function useIntlSafe() {
  try {
    return useIntl();
  } catch (_unused) {
    return null;
  }
};
var useActionFlags = function useActionFlags() {
  var intl = useIntlSafe();
  var showConnectFlag = useCallback(function () {
    var _provider$text;
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var provider = _ref.provider,
      flagArgs = _objectWithoutProperties(_ref, _excluded);
    if (!intl) {
      return;
    }
    var context = (_provider$text = provider === null || provider === void 0 ? void 0 : provider.text) !== null && _provider$text !== void 0 ? _provider$text : intl.formatMessage(messages.connect_link_account_success_flag_title_default);
    showFlag(_objectSpread({
      icon: renderIcon(provider === null || provider === void 0 ? void 0 : provider.icon),
      title: intl.formatMessage(messages.connect_link_account_success_flag_title, {
        context: context
      }),
      description: intl.formatMessage(messages.connect_link_account_success_flag_description)
    }, flagArgs));
  }, [intl]);
  return useMemo(function () {
    return {
      showConnectFlag: showConnectFlag
    };
  }, [showConnectFlag]);
};
export default useActionFlags;