"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _messages = require("../../../messages");
var _imageIcon = _interopRequireDefault(require("../../../view/common/image-icon"));
var _Flag = require("../../../view/Flag");
var _excluded = ["provider"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2.default)(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; }
var FLAG_ICON_SIZE = '16px';
var renderIcon = function renderIcon(iconUrlOrElement) {
  if (!iconUrlOrElement) {
    return;
  }
  var icon = iconUrlOrElement && typeof iconUrlOrElement === 'string' ? /*#__PURE__*/_react.default.createElement(_imageIcon.default, {
    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 (0, _reactIntl.useIntl)();
  } catch (_unused) {
    return null;
  }
};
var useActionFlags = function useActionFlags() {
  var intl = useIntlSafe();
  var showConnectFlag = (0, _react.useCallback)(function () {
    var _provider$text;
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var provider = _ref.provider,
      flagArgs = (0, _objectWithoutProperties2.default)(_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.messages.connect_link_account_success_flag_title_default);
    (0, _Flag.showFlag)(_objectSpread({
      icon: renderIcon(provider === null || provider === void 0 ? void 0 : provider.icon),
      title: intl.formatMessage(_messages.messages.connect_link_account_success_flag_title, {
        context: context
      }),
      description: intl.formatMessage(_messages.messages.connect_link_account_success_flag_description)
    }, flagArgs));
  }, [intl]);
  return (0, _react.useMemo)(function () {
    return {
      showConnectFlag: showConnectFlag
    };
  }, [showConnectFlag]);
};
var _default = exports.default = useActionFlags;