"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isVisible = exports.isFinalState = exports.isAccessible = exports.hasResolved = exports.hasAuthScopeOverrides = exports.getThirdPartyARI = exports.getSubproduct = exports.getStatusDetails = exports.getServices = exports.getResourceType = exports.getProduct = exports.getObjectName = exports.getObjectIconUrl = exports.getObjectAri = exports.getFirstPartyIdentifier = exports.getExtensionKey = exports.getDefinitionId = exports.getDatasources = exports.getClickUrl = exports.getCanBeDatasource = exports.getByDefinitionId = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _extractVisitUrl = require("../extractors/common/primitives/extractVisitUrl");
var getByDefinitionId = exports.getByDefinitionId = function getByDefinitionId(definitionId, store) {
  var urls = Object.keys(store);
  return urls.filter(function (url) {
    var details = store[url].details;
    return details && details.meta.definitionId === definitionId;
  });
};
var getClickUrl = exports.getClickUrl = function getClickUrl(url, jsonLd) {
  if (jsonLd && jsonLd.data) {
    var visitUrl = (0, _extractVisitUrl.extractVisitUrl)(jsonLd.data);
    if (visitUrl) {
      return visitUrl;
    }
  }
  return url;
};
var getDefinitionId = exports.getDefinitionId = function getDefinitionId(details) {
  var _details$meta;
  return details === null || details === void 0 || (_details$meta = details.meta) === null || _details$meta === void 0 ? void 0 : _details$meta.definitionId;
};
var getExtensionKey = exports.getExtensionKey = function getExtensionKey(details) {
  var _details$meta2;
  return details === null || details === void 0 || (_details$meta2 = details.meta) === null || _details$meta2 === void 0 ? void 0 : _details$meta2.key;
};

/**
 * @deprecated Use extractAri from @atlaskit/link-extractors instead
 */
var getObjectAri = exports.getObjectAri = function getObjectAri(details) {
  return (details === null || details === void 0 ? void 0 : details.data) && 'atlassian:ari' in details.data && details.data['atlassian:ari'] || undefined;
};
var getThirdPartyARI = exports.getThirdPartyARI = function getThirdPartyARI(details) {
  if ((0, _platformFeatureFlags.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') {
      var ari = details.data['atlassian:ari'];
      if (ari.includes('ari:third-party')) {
        return ari;
      }
    }
  }
  return undefined;
};
var getFirstPartyIdentifier = exports.getFirstPartyIdentifier = function getFirstPartyIdentifier() {
  if (!(0, _platformFeatureFlags.fg)('platform_smartlink_3pclick_analytics')) {
    return undefined;
  }
  var product = getProductFromWindowURL();
  var currentURL = window.location.href;
  if (product === 'confluence') {
    if (currentURL.includes('content.id')) {
      var contentId = extractContentIdFromURL(currentURL);
      if (contentId) {
        return "ConfluenceContentId:".concat(contentId);
      }
    }
    var pageId = extractPageIdFromURL(currentURL);
    if (pageId) {
      return "ConfluencePageId:".concat(pageId);
    }
  } else if (product === 'jira') {
    var issueKey = extractJiraIssueIdFromURL(currentURL);
    if (issueKey) {
      return "JiraIssueKey:".concat(issueKey);
    }
  }
  return undefined;
};

/**
 * @deprecated Use extractNameFromJsonLd from @atlaskit/link-extractors/extract-name-from-json-ld instead
 */
var getObjectName = exports.getObjectName = function getObjectName(details) {
  return (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
 */
var getObjectIconUrl = exports.getObjectIconUrl = function getObjectIconUrl(details) {
  if (details !== null && details !== void 0 && details.data && 'icon' in details.data && details.data.icon) {
    if ((0, _typeof2.default)(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;
};
var getResourceType = exports.getResourceType = function getResourceType(details) {
  var _details$meta3;
  return details === null || details === void 0 || (_details$meta3 = details.meta) === null || _details$meta3 === void 0 ? void 0 : _details$meta3.resourceType;
};
var getProduct = exports.getProduct = function getProduct(details) {
  var _details$meta4;
  return details === null || details === void 0 || (_details$meta4 = details.meta) === null || _details$meta4 === void 0 ? void 0 : _details$meta4.product;
};
var getSubproduct = exports.getSubproduct = function getSubproduct(details) {
  var _details$meta5;
  return details === null || details === void 0 || (_details$meta5 = details.meta) === null || _details$meta5 === void 0 ? void 0 : _details$meta5.subproduct;
};
var getServices = exports.getServices = function getServices(details) {
  return details && details.meta.auth || [];
};
var hasResolved = exports.hasResolved = function hasResolved(details) {
  return details && isAccessible(details) && isVisible(details);
};
var isAccessible = exports.isAccessible = function isAccessible(_ref) {
  var access = _ref.meta.access;
  return access === 'granted';
};
var isVisible = exports.isVisible = function isVisible(_ref2) {
  var visibility = _ref2.meta.visibility;
  return visibility === 'restricted' || visibility === 'public';
};
var getStatusDetails = exports.getStatusDetails = function getStatusDetails(details) {
  var _details$meta6;
  return details === null || details === void 0 || (_details$meta6 = details.meta) === null || _details$meta6 === void 0 || (_details$meta6 = _details$meta6.requestAccess) === null || _details$meta6 === void 0 ? void 0 : _details$meta6.accessType;
};
var isFinalState = exports.isFinalState = function isFinalState(status) {
  return ['unauthorized', 'forbidden', 'errored', 'resolved', 'not_found'].indexOf(status) > -1;
};
var getDatasources = exports.getDatasources = function getDatasources(details) {
  return details === null || details === void 0 ? void 0 : details.datasources;
};
var getCanBeDatasource = exports.getCanBeDatasource = function getCanBeDatasource(details) {
  var datasources = getDatasources(details);
  return !!datasources && datasources.length > 0;
};
var hasAuthScopeOverrides = exports.hasAuthScopeOverrides = function hasAuthScopeOverrides(details) {
  return !!(details !== null && details !== void 0 && details.meta.hasScopeOverrides);
};

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

    // Following this pattern for confluence URL -> /wiki/spaces/{space}/pages/{pageId}/{title}
    var 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}
    var pageIdParam = urlObj.searchParams.get('pageId');
    if (pageIdParam) {
      return pageIdParam;
    }

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

    // Pattern: ?content.id={contentId} (for app connector URLs and plugin servlet URLs)
    var 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
    var match = url.match(/[?&]content\.id=(\d+)/);
    if (match && match[1]) {
      return match[1];
    }
  } catch (_unused2) {
    return undefined;
  }
  return undefined;
};
var extractJiraIssueIdFromURL = function extractJiraIssueIdFromURL(url) {
  try {
    var urlObj = new URL(url);

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

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

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

    // Query parameter ?issueKey={issueKey}
    var issueKeyParam = urlObj.searchParams.get('issueKey');
    if (issueKeyParam && /^[A-Za-z0-9]+-\d+$/.test(issueKeyParam)) {
      return issueKeyParam;
    }
  } catch (_unused3) {
    return undefined;
  }
  return undefined;
};
var getProductFromWindowURL = function getProductFromWindowURL() {
  try {
    var currentUrl = window.location.href;
    var urlObj = new URL(currentUrl);
    var hostname = urlObj.hostname.toLowerCase();
    var 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 (_unused4) {
    return undefined;
  }
  return undefined;
};