import { useEffect, useMemo } from 'react';
import { setGlobalTheme } from '@atlaskit/tokens/set-global-theme';
import { themeStringToObject } from '@atlaskit/tokens/theme-state-transformer';
export var userType = /*#__PURE__*/function (userType) {
  userType["ATLASSIAN_ACCOUNT"] = "atlassianAccount";
  userType["HASHED_EMAIL"] = "hashedEmail";
  userType["LOOM"] = "loom";
  userType["TRELLO"] = "trello";
  userType["OPSGENIE"] = "opsgenie";
  userType["JA_USER_ID"] = "jiraAlign";
  userType["HALP"] = "halp";
  return userType;
}({});
export var EMBEDDED_CONFLUENCE_MODE = /*#__PURE__*/function (EMBEDDED_CONFLUENCE_MODE) {
  EMBEDDED_CONFLUENCE_MODE["VIEW_MODE"] = "view";
  EMBEDDED_CONFLUENCE_MODE["EDIT_MODE"] = "edit";
  return EMBEDDED_CONFLUENCE_MODE;
}({});
export var CONFLUENCE_EXTENSION_KEYS = /*#__PURE__*/function (CONFLUENCE_EXTENSION_KEYS) {
  CONFLUENCE_EXTENSION_KEYS["PAGE"] = "integration-confluence-object-provider";
  CONFLUENCE_EXTENSION_KEYS["CANVAS"] = "canvas-native-object-provider";
  return CONFLUENCE_EXTENSION_KEYS;
}({});
export var useConfluencePageData = function useConfluencePageData(url, extensionKey) {
  var parsedData = useMemo(function () {
    if (!url || typeof url !== 'string' || !url.startsWith('http://') && !url.startsWith('https://') || !extensionKey || !Object.values(CONFLUENCE_EXTENSION_KEYS).find(function (key) {
      return key === extensionKey;
    })) {
      return undefined;
    }
    var urlObj;
    try {
      urlObj = new URL(url);
    } catch (_unused) {
      return undefined;
    }
    var _urlObj = urlObj,
      searchParams = _urlObj.searchParams,
      hash = _urlObj.hash;
    var hostname = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('hostname')) || '';
    var contentId = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('contentId')) || '';
    var spaceKey = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('spaceKey')) || '';

    // Early return for invalid URLs
    if (!contentId || !hostname) {
      return undefined;
    }
    var themeState = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('themeState')) || '';
    var themeMode = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('themeMode');
    var enableInlineComments = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('enableInlineComments')) === 'true';
    var enablePageComments = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('enablePageComments')) === 'true';
    var userId = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('userId')) || '';
    var userIdType = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('userInfo')) || undefined;
    var parentProduct = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('parentProduct')) || '';
    var inlineComments = enableInlineComments ? ['inline-comments'] : [];
    var pageComments = enablePageComments ? ['page-comments'] : [];

    // Calculate theme state object here
    var calculatedThemeStateObject;
    if (themeState) {
      calculatedThemeStateObject = themeStringToObject(decodeURIComponent(themeState));
    } else if (themeMode) {
      calculatedThemeStateObject = {
        colorMode: themeMode
      };
    }
    var calculatedUserInfo = userId && userIdType ? {
      userId: userId,
      userIdType: userIdType
    } : undefined;
    return {
      hostname: hostname,
      spaceKey: spaceKey,
      contentId: contentId,
      parentProduct: parentProduct,
      userInfo: calculatedUserInfo,
      hash: hash || '',
      enableInlineComments: enableInlineComments,
      enablePageComments: enablePageComments,
      themeStateObject: calculatedThemeStateObject,
      allowedFeatures: {
        edit: ['delete-draft'],
        view: ['byline-contributors', 'byline-extensions', 'page-reactions'].concat(pageComments, inlineComments)
      },
      mode: EMBEDDED_CONFLUENCE_MODE.VIEW_MODE,
      locale: 'en'
    };
  }, [url, extensionKey]);

  // Handle theme side effect - only when theme object changes
  useEffect(function () {
    if (parsedData !== null && parsedData !== void 0 && parsedData.themeStateObject) {
      setGlobalTheme(parsedData.themeStateObject);
    }
  }, [parsedData === null || parsedData === void 0 ? void 0 : parsedData.themeStateObject]);
  return parsedData;
};