"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSmartLinkAnalyticsContext = exports.SmartLinkAnalyticsContext = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _analyticsNext = require("@atlaskit/analytics-next");
var _featureGateJsClient = _interopRequireDefault(require("@atlaskit/feature-gate-js-client"));
var _resolvedAttributes = require("@atlaskit/link-analytics/resolved-attributes");
var _linkProvider = require("@atlaskit/link-provider");
var _linkingCommon = require("@atlaskit/linking-common");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _store = require("../../state/store");
var _analytics = require("./analytics");
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 getExtendedResolvedAttributes = function getExtendedResolvedAttributes(linkDetails, details, linkStatus, error) {
  var _details$meta$definit, _details$meta, _details$meta$resourc, _details$meta2;
  return _objectSpread({
    definitionId: (_details$meta$definit = details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.definitionId) !== null && _details$meta$definit !== void 0 ? _details$meta$definit : null,
    resourceType: (_details$meta$resourc = details === null || details === void 0 || (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.resourceType) !== null && _details$meta$resourc !== void 0 ? _details$meta$resourc : null
  }, (0, _resolvedAttributes.getResolvedAttributes)(linkDetails, details, linkStatus, error));
};
var getSmartLinkAnalyticsContext = function getSmartLinkAnalyticsContext(_ref) {
  var display = _ref.display,
    id = _ref.id,
    response = _ref.response,
    source = _ref.source,
    status = _ref.status,
    url = _ref.url,
    error = _ref.error;
  var isHyperlinkConnectExperimentEnabled = _featureGateJsClient.default.getExperimentValue('platform_linking_bluelink_connect_confluence', 'isEnabled', false) || _featureGateJsClient.default.getExperimentValue('platform_linking_bluelink_connect_jira', 'isEnabled', false);
  var resolvedAttributes = isHyperlinkConnectExperimentEnabled ? getExtendedResolvedAttributes({
    url: url,
    displayCategory: display === 'url' ? 'link' : 'smartLink'
  }, response, status, error) : getExtendedResolvedAttributes({
    url: url
  }, response, status, error);
  return {
    source: source,
    attributes: _objectSpread(_objectSpread({}, _analytics.context), {}, {
      display: display,
      id: id
    }, resolvedAttributes)
  };
};
/**
 * Provides an analytics context data to supply attributes to events based on a URL
 * and the link state in the store
 */
var useSmartLinkAnalyticsContext = exports.useSmartLinkAnalyticsContext = function useSmartLinkAnalyticsContext(_ref2) {
  var display = _ref2.display,
    id = _ref2.id,
    source = _ref2.source,
    url = _ref2.url;
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    store = _useSmartLinkContext.store;
  var state = store ? (0, _linkingCommon.getUrl)(store, url) : undefined;
  return (0, _react.useMemo)(function () {
    return getSmartLinkAnalyticsContext({
      display: display,
      id: id,
      response: state === null || state === void 0 ? void 0 : state.details,
      source: source,
      status: state === null || state === void 0 ? void 0 : state.status,
      url: url,
      error: state === null || state === void 0 ? void 0 : state.error
    });
  }, [display, id, source, state === null || state === void 0 ? void 0 : state.details, state === null || state === void 0 ? void 0 : state.status, url, state === null || state === void 0 ? void 0 : state.error]);
};

/**
 * Provides an analytics context to supply attributes to events based on a URL
 * and the link state in the store
 */
var SmartLinkAnalyticsContext = exports.SmartLinkAnalyticsContext = function SmartLinkAnalyticsContext(_ref3) {
  var children = _ref3.children,
    display = _ref3.display,
    id = _ref3.id,
    source = _ref3.source,
    url = _ref3.url;
  var _useSmartLinkState = (0, _store.useSmartCardState)(url),
    details = _useSmartLinkState.details,
    status = _useSmartLinkState.status,
    error = _useSmartLinkState.error;
  var data = getSmartLinkAnalyticsContext({
    display: display,
    id: id,
    response: details,
    source: source,
    status: status,
    url: url,
    error: (0, _platformFeatureFlags.fg)('platform_bandicoots-smartlink-unresolved-error-key') ? error : undefined
  });
  return /*#__PURE__*/_react.default.createElement(_analyticsNext.AnalyticsContext, {
    data: data
  }, children);
};