import React, { useMemo } from 'react';
import { AnalyticsContext } from '@atlaskit/analytics-next';
import FeatureGates from '@atlaskit/feature-gate-js-client';
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';
const getExtendedResolvedAttributes = (linkDetails, details, linkStatus, error) => {
  var _details$meta$definit, _details$meta, _details$meta$resourc, _details$meta2;
  return {
    definitionId: (_details$meta$definit = details === null || details === void 0 ? 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 ? 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)
  };
};
const getSmartLinkAnalyticsContext = ({
  display,
  id,
  response,
  source,
  status,
  url,
  error
}) => {
  const isHyperlinkConnectExperimentEnabled = FeatureGates.getExperimentValue('platform_linking_bluelink_connect_confluence', 'isEnabled', false) || FeatureGates.getExperimentValue('platform_linking_bluelink_connect_jira', 'isEnabled', false);
  const resolvedAttributes = isHyperlinkConnectExperimentEnabled ? getExtendedResolvedAttributes({
    url,
    displayCategory: display === 'url' ? 'link' : 'smartLink'
  }, response, status, error) : getExtendedResolvedAttributes({
    url
  }, response, status, error);
  return {
    source,
    attributes: {
      ...context,
      display,
      id,
      ...resolvedAttributes
    }
  };
};
/**
 * Provides an analytics context data to supply attributes to events based on a URL
 * and the link state in the store
 */
export const useSmartLinkAnalyticsContext = ({
  display,
  id,
  source,
  url
}) => {
  const {
    store
  } = useSmartLinkContext();
  const state = store ? getUrl(store, url) : undefined;
  return useMemo(() => {
    return getSmartLinkAnalyticsContext({
      display,
      id,
      response: state === null || state === void 0 ? void 0 : state.details,
      source,
      status: state === null || state === void 0 ? void 0 : state.status,
      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 const SmartLinkAnalyticsContext = ({
  children,
  display,
  id,
  source,
  url
}) => {
  const {
    details,
    status,
    error
  } = useSmartLinkState(url);
  const data = getSmartLinkAnalyticsContext({
    display,
    id,
    response: details,
    source,
    status,
    url,
    error: fg('platform_bandicoots-smartlink-unresolved-error-key') ? error : undefined
  });
  return /*#__PURE__*/React.createElement(AnalyticsContext, {
    data: data
  }, children);
};