import _defineProperty from "@babel/runtime/helpers/defineProperty";
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 { AnalyticsContext } from '@atlaskit/analytics-next';
import { getResolvedAttributes } from '@atlaskit/link-analytics/resolved-attributes';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { getUrl } from '@atlaskit/linking-common';
import { fg } from '@atlaskit/platform-feature-flags';
import { useSmartCardState as useSmartLinkState } from '../../state/store';
import { context } from './analytics';
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
  }, 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 resolvedAttributes = getExtendedResolvedAttributes({
    url: url,
    displayCategory: display === 'url' ? 'link' : 'smartLink'
  }, response, status, error);
  return {
    source: source,
    attributes: _objectSpread(_objectSpread({}, 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
 */
export var useSmartLinkAnalyticsUtils = function useSmartLinkAnalyticsUtils() {
  var _useSmartLinkContext = useSmartLinkContext(),
    store = _useSmartLinkContext.store;
  var getByUrl = useCallback(function (url, props) {
    var state = store ? getUrl(store, url) : undefined;
    return getSmartLinkAnalyticsContext({
      display: props === null || props === void 0 ? void 0 : props.display,
      id: props === null || props === void 0 ? void 0 : props.id,
      response: state === null || state === void 0 ? void 0 : state.details,
      source: props === null || props === void 0 ? void 0 : props.source,
      status: state === null || state === void 0 ? void 0 : state.status,
      url: url,
      error: state === null || state === void 0 ? void 0 : state.error
    });
  }, [store]);
  return useMemo(function () {
    return {
      getByUrl: getByUrl
    };
  }, [getByUrl]);
};

/**
 * Provides an analytics context data to supply attributes to events based on a URL
 * and the link state in the store
 * @deprecated Use useSmartLinkAnalyticsUtils instead
 */
export var useSmartLinkAnalyticsContext = function useSmartLinkAnalyticsContext(_ref2) {
  var display = _ref2.display,
    id = _ref2.id,
    source = _ref2.source,
    url = _ref2.url;
  var _useSmartLinkContext2 = useSmartLinkContext(),
    store = _useSmartLinkContext2.store;
  var state = store ? getUrl(store, url) : undefined;
  return 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
 */
export var SmartLinkAnalyticsContext = function SmartLinkAnalyticsContext(_ref3) {
  var children = _ref3.children,
    display = _ref3.display,
    id = _ref3.id,
    source = _ref3.source,
    url = _ref3.url;
  var _useSmartLinkState = useSmartLinkState(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: fg('platform_bandicoots-smartlink-unresolved-error-key') ? error : undefined
  });
  return /*#__PURE__*/React.createElement(AnalyticsContext, {
    data: data
  }, children);
};