"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.handleSSRErrorsAnalytics = exports.SyncedBlocksSSRErrorCodeMap = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _analytics = require("../analytics");
var _coreUtils = require("../core-utils");
/**
 * Error codes for synced blocks preloading
 * Used for tracking and monitoring errors during SSR preload phase
 */
var SyncedBlocksSSRErrorCodeMap = exports.SyncedBlocksSSRErrorCodeMap = {
  /**
   * Occurs when the fetch operation returns fewer results than requested.
   * This indicates incomplete data retrieval or potential data loss.
   * Included in error: list of missing block identifiers
   */
  FETCH_INCOMPLETE: 'SYNCED_BLOCKS_FETCH_INCOMPLETE',
  /**
   * Occurs when the batch fetch operation throws an error during execution.
   * This indicates a network error, timeout, or API failure.
   * Included in error: resource IDs that failed to fetch
   */
  BATCH_FETCH_FAILED: 'SYNCED_BLOCKS_BATCH_FETCH_FAILED',
  /**
   * Occurs when the fetch operation times out without returning results.
   * This is specific to the bulk fetch operation with a configurable timeout.
   */
  FETCH_TIMEOUT: 'SYNCED_BLOCKS_FETCH_TIMEOUT'
};

/**
 * This is the metadata structure for SSR errors stored in the window object.
 */

var isSyncedBlocksSSRErrorObj = function isSyncedBlocksSSRErrorObj(maybeSSRErrorObj) {
  if (!maybeSSRErrorObj || (0, _typeof2.default)(maybeSSRErrorObj) !== 'object') {
    return false;
  }
  var errorWrapper = maybeSSRErrorObj;

  // Check for meta object with valid code
  var hasValidMeta = errorWrapper.meta && (0, _typeof2.default)(errorWrapper.meta) === 'object' && Object.values(SyncedBlocksSSRErrorCodeMap).includes(errorWrapper.meta.code);
  return Boolean(hasValidMeta);
};

/**
 * Get the first SSR error for synced blocks from the window.__SSR_ERROR__ array.
 *
 * Expected structure of error objects stored in confluence window.__SSR_ERROR__ for synced blocks SSR errors:
 *
 * @example
 * {
 *   error: {
 *     message: "Synced blocks fetch operation timed out",
 *     stack: "Error: Synced blocks fetch operation timed out\n    at /tmp/script.js:xxx:xxx"
 *   },
 *   meta: {
 *     code: "SYNCED_BLOCKS_FETCH_TIMEOUT",
 *     contentId: "XXXXXX",
 *     cloudId: "XXXX-XXXX-XXXX-XXXX-XXXX",
 *     isBlog: false,
 *     identifiers: [
 *       {
 *         resourceId: "confluence-page/XXXXXX/XXXX-XXXX-XXXX-XXXX-XXXX",
 *         blockInstanceId: "XXXX-XXXX-XXXX-XXXX-XXXX"
 *       }
 *     ]
 *   }
 * }
 *
 * @returns The first SSR error metadata, or undefined if no error found
 */
var getSyncedBlocksSSRError = function getSyncedBlocksSSRError() {
  if ((0, _coreUtils.isSSR)()) {
    return undefined;
  }
  var ssrErrors = window.__SSR_ERROR__;
  if (Array.isArray(ssrErrors)) {
    var syncedBlockErrors = ssrErrors.filter(isSyncedBlocksSSRErrorObj).map(function (errorWrapper) {
      return errorWrapper.meta;
    });
    return syncedBlockErrors.length > 0 ? syncedBlockErrors[0] : undefined;
  }
  return undefined;
};

/**
 * Fire analytics event for the first unprocessed SSR error that occurred during page rendering.
 * This function only runs ONCE per browser session to avoid duplicate analytics events.
 *
 * @param fireAnalyticsEvent - Function to fire analytics events
 */
var handleSSRErrorsAnalytics = exports.handleSSRErrorsAnalytics = function () {
  var called = false;
  // I want this fireAnalyticsEvent only takes in SyncedBlockSSRErrorAEP
  return function (fireAnalyticsEvent) {
    if (called) {
      return;
    }
    called = true;
    if (!fireAnalyticsEvent) {
      return;
    }
    var error = getSyncedBlocksSSRError();
    if (error) {
      var _error$identifiers, _error$missingIdentif;
      fireAnalyticsEvent({
        action: _analytics.ACTION.ERROR,
        actionSubject: _analytics.ACTION_SUBJECT.SYNCED_BLOCK,
        actionSubjectId: _analytics.ACTION_SUBJECT_ID.SYNCED_BLOCK_SSR_ERROR,
        eventType: _analytics.EVENT_TYPE.OPERATIONAL,
        attributes: {
          code: error.code,
          isBlog: error.isBlog,
          syncedBlocksCount: ((_error$identifiers = error.identifiers) === null || _error$identifiers === void 0 ? void 0 : _error$identifiers.length) || 0,
          missingSyncedBlocksCount: ((_error$missingIdentif = error.missingIdentifiers) === null || _error$missingIdentif === void 0 ? void 0 : _error$missingIdentif.length) || 0,
          errorMessage: error.errorMessage,
          errorName: error.errorName
        }
      });
    }
  };
}();