import { useEffect, useMemo } from 'react';
import { setGlobalTheme } from '@atlaskit/tokens/set-global-theme';
import { themeStringToObject } from '@atlaskit/tokens/theme-state-transformer';
export let 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 let 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 let 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 const useConfluencePageData = (url, extensionKey) => {
  const parsedData = useMemo(() => {
    if (!url || typeof url !== 'string' || !url.startsWith('http://') && !url.startsWith('https://') || !extensionKey || !Object.values(CONFLUENCE_EXTENSION_KEYS).find(key => key === extensionKey)) {
      return undefined;
    }
    let urlObj;
    try {
      urlObj = new URL(url);
    } catch {
      return undefined;
    }
    const {
      searchParams,
      hash
    } = urlObj;
    const hostname = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('hostname')) || '';
    const contentId = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('contentId')) || '';
    const spaceKey = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get('spaceKey')) || '';

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

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

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