import { fg } from '@atlaskit/platform-feature-flags';
import { extractVisitUrl } from '../extractors/common/primitives/extractVisitUrl';
export const getByDefinitionId = (definitionId, store) => {
  const urls = Object.keys(store);
  return urls.filter(url => {
    const {
      details
    } = store[url];
    return details && details.meta.definitionId === definitionId;
  });
};
export const getClickUrl = (url, jsonLd) => {
  if (jsonLd && jsonLd.data) {
    const visitUrl = extractVisitUrl(jsonLd.data);
    if (visitUrl) {
      return visitUrl;
    }
  }
  return url;
};
export const getDefinitionId = details => {
  var _details$meta;
  return details === null || details === void 0 ? void 0 : (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.definitionId;
};
export const getExtensionKey = details => {
  var _details$meta2;
  return details === null || details === void 0 ? void 0 : (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key;
};

/**
 * @deprecated Use extractAri from @atlaskit/link-extractors instead
 */
export const getObjectAri = details => (details === null || details === void 0 ? void 0 : details.data) && 'atlassian:ari' in details.data && details.data['atlassian:ari'] || undefined;
export const getThirdPartyARI = details => {
  if (fg('platform_smartlink_3pclick_analytics')) {
    if (details !== null && details !== void 0 && details.entityData && 'thirdPartyAri' in details.entityData && details.entityData.thirdPartyAri) {
      return details.entityData.thirdPartyAri;
    }
    if (details !== null && details !== void 0 && details.data && 'atlassian:ari' in details.data && typeof details.data['atlassian:ari'] === 'string') {
      const ari = details.data['atlassian:ari'];
      if (ari.includes('ari:third-party')) {
        return ari;
      }
    }
  }
  return undefined;
};
export const getFirstPartyIdentifier = () => {
  if (!fg('platform_smartlink_3pclick_analytics')) {
    return undefined;
  }
  const product = getProductFromWindowURL();
  const currentURL = window.location.href;
  if (product === 'confluence') {
    if (currentURL.includes('content.id')) {
      const contentId = extractContentIdFromURL(currentURL);
      if (contentId) {
        return `ConfluenceContentId:${contentId}`;
      }
    }
    const pageId = extractPageIdFromURL(currentURL);
    if (pageId) {
      return `ConfluencePageId:${pageId}`;
    }
  } else if (product === 'jira') {
    const issueKey = extractJiraIssueIdFromURL(currentURL);
    if (issueKey) {
      return `JiraIssueKey:${issueKey}`;
    }
  }
  return undefined;
};

/**
 * @deprecated Use extractNameFromJsonLd from @atlaskit/link-extractors/extract-name-from-json-ld instead
 */
export const getObjectName = details => (details === null || details === void 0 ? void 0 : details.data) && 'name' in details.data && details.data.name || undefined;

/**
 * @deprecated Use extractUrlFromIconJsonLd from @atlaskit/link-extractors instead
 */
export const getObjectIconUrl = details => {
  if (details !== null && details !== void 0 && details.data && 'icon' in details.data && details.data.icon) {
    if (typeof details.data.icon === 'object' && 'url' in details.data.icon && details.data.icon.url && typeof details.data.icon.url === 'string') {
      return details.data.icon.url;
    }
  }
  return undefined;
};
export const getResourceType = details => {
  var _details$meta3;
  return details === null || details === void 0 ? void 0 : (_details$meta3 = details.meta) === null || _details$meta3 === void 0 ? void 0 : _details$meta3.resourceType;
};
export const getProduct = details => {
  var _details$meta4;
  return details === null || details === void 0 ? void 0 : (_details$meta4 = details.meta) === null || _details$meta4 === void 0 ? void 0 : _details$meta4.product;
};
export const getSubproduct = details => {
  var _details$meta5;
  return details === null || details === void 0 ? void 0 : (_details$meta5 = details.meta) === null || _details$meta5 === void 0 ? void 0 : _details$meta5.subproduct;
};
export const getServices = details => details && details.meta.auth || [];
export const hasResolved = details => details && isAccessible(details) && isVisible(details);
export const isAccessible = ({
  meta: {
    access
  }
}) => access === 'granted';
export const isVisible = ({
  meta: {
    visibility
  }
}) => visibility === 'restricted' || visibility === 'public';
export const getStatusDetails = details => {
  var _details$meta6, _details$meta6$reques;
  return details === null || details === void 0 ? void 0 : (_details$meta6 = details.meta) === null || _details$meta6 === void 0 ? void 0 : (_details$meta6$reques = _details$meta6.requestAccess) === null || _details$meta6$reques === void 0 ? void 0 : _details$meta6$reques.accessType;
};
export const isFinalState = status => {
  return ['unauthorized', 'forbidden', 'errored', 'resolved', 'not_found'].indexOf(status) > -1;
};
export const getDatasources = details => details === null || details === void 0 ? void 0 : details.datasources;
export const getCanBeDatasource = details => {
  const datasources = getDatasources(details);
  return !!datasources && datasources.length > 0;
};
export const hasAuthScopeOverrides = details => !!(details !== null && details !== void 0 && details.meta.hasScopeOverrides);

// Helper function to extract page ID from standard Confluence page URLs
const extractPageIdFromURL = url => {
  try {
    const urlObj = new URL(url);

    // Following this pattern for confluence URL -> /wiki/spaces/{space}/pages/{pageId}/{title}
    const pagesMatch = urlObj.pathname.match(/\/wiki\/spaces\/[^\/]+\/pages\/(\d+)(?:\/|$)/);
    if (pagesMatch && pagesMatch[1]) {
      return pagesMatch[1];
    }

    // Following this pattern for confluence URL -> /wiki/pages/viewpage.action?pageId={pageId}
    const pageIdParam = urlObj.searchParams.get('pageId');
    if (pageIdParam) {
      return pageIdParam;
    }

    // Following this pattern for confluence URL -> /wiki/display/{space}/{pageId}
    const displayMatch = urlObj.pathname.match(/\/wiki\/display\/[^\/]+\/(\d+)/);
    if (displayMatch && displayMatch[1]) {
      return displayMatch[1];
    }
  } catch {
    return undefined;
  }
  return undefined;
};
const extractContentIdFromURL = url => {
  try {
    const urlObj = new URL(url);

    // Pattern: ?content.id={contentId} (for app connector URLs and plugin servlet URLs)
    const contentId = urlObj.searchParams.get('content.id');
    if (contentId) {
      return contentId;
    }

    // Also support URLs where content.id is in the query string but not parsed by URL (edge case)
    // e.g., /wiki/plugins/servlet/ac/com.atlassian.perspectiveretros/perspective-retros?content.id=5662725850
    const match = url.match(/[?&]content\.id=(\d+)/);
    if (match && match[1]) {
      return match[1];
    }
  } catch {
    return undefined;
  }
  return undefined;
};
const extractJiraIssueIdFromURL = url => {
  try {
    const urlObj = new URL(url);

    // Support any case for Jira keys (e.g., AI3W-864, ai3w-864, Ai3W-864)
    const browseMatch = urlObj.pathname.match(/\/browse\/([A-Za-z0-9]+-\d+)/);
    if (browseMatch && browseMatch[1]) {
      return browseMatch[1];
    }

    // /jira/browse/{issueKey}
    const jiraBrowseMatch = urlObj.pathname.match(/\/jira\/browse\/([A-Za-z0-9]+-\d+)/);
    if (jiraBrowseMatch && jiraBrowseMatch[1]) {
      return jiraBrowseMatch[1];
    }

    // Query parameter ?selectedIssue={issueKey}
    const selectedIssue = urlObj.searchParams.get('selectedIssue');
    if (selectedIssue && /^[A-Za-z0-9]+-\d+$/.test(selectedIssue)) {
      return selectedIssue;
    }

    // Query parameter ?issueKey={issueKey}
    const issueKeyParam = urlObj.searchParams.get('issueKey');
    if (issueKeyParam && /^[A-Za-z0-9]+-\d+$/.test(issueKeyParam)) {
      return issueKeyParam;
    }
  } catch {
    return undefined;
  }
  return undefined;
};
const getProductFromWindowURL = () => {
  try {
    const currentUrl = window.location.href;
    const urlObj = new URL(currentUrl);
    const hostname = urlObj.hostname.toLowerCase();
    const pathname = urlObj.pathname.toLowerCase();

    // Check for Confluence patterns
    if (hostname.includes('confluence') || pathname.includes('/wiki/') || pathname.includes('/display/') || pathname.includes('/spaces/')) {
      return 'confluence';
    }

    // Check for Jira patterns
    if (hostname.includes('jira') || pathname.includes('/browse/') || pathname.includes('/projects/') || pathname.includes('/secure/')) {
      return 'jira';
    }
  } catch {
    return undefined;
  }
  return undefined;
};