"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setClientIdForFile = exports.getClientIdForFile = exports.extractClientIdsFromHtml = exports.clearClientIdCache = void 0;
/**
 * Cache for storing fileId to clientId mapping during paste operations.
 * This avoids the need to store clientId in ADF schema attributes.
 *
 * Used for cross-client copy/paste scenarios
 *
 * Entries are one-time use (consumed on read), with a max size limit
 * to guard against build-up if entries are never consumed (should not happpen).
 */

var CLIENT_ID_CACHE_MAX_SIZE = 100;
var clientIdCache = new Map();
var setClientIdForFile = exports.setClientIdForFile = function setClientIdForFile(fileId, clientId) {
  clientIdCache.set(fileId, clientId);

  // Evict oldest entries if cache exceeds max size
  if (clientIdCache.size > CLIENT_ID_CACHE_MAX_SIZE) {
    var oldestKey = clientIdCache.keys().next().value;
    if (oldestKey !== undefined) {
      clientIdCache.delete(oldestKey);
    }
  }
};

/**
 * Retrieve and consume clientId for a file ID (called during copyFile)
 * Returns undefined if not found.
 */
var getClientIdForFile = exports.getClientIdForFile = function getClientIdForFile(fileId) {
  var clientId = clientIdCache.get(fileId);
  // Consume the entry after reading (one-time use)
  if (clientId) {
    clientIdCache.delete(fileId);
  }
  return clientId;
};

/**
 * Clear all cached clientIds (for testing purposes)
 */
var clearClientIdCache = exports.clearClientIdCache = function clearClientIdCache() {
  clientIdCache.clear();
};

/**
 * Used to store fileId to clientId mappings on paste of Editor PM Node
 */
var extractClientIdsFromHtml = exports.extractClientIdsFromHtml = function extractClientIdsFromHtml(html) {
  if (!html || !html.includes('data-client-id')) {
    return;
  }
  var mediaTagRegex = /<[^>]*data-node-type="media(?:Inline)?"[^>]*>/g;
  var match;
  while ((match = mediaTagRegex.exec(html)) !== null) {
    var _exec, _exec2;
    var tag = match[0];
    var fileId = (_exec = /data-id="([^"]*)"/.exec(tag)) === null || _exec === void 0 ? void 0 : _exec[1];
    var clientId = (_exec2 = /data-client-id="([^"]*)"/.exec(tag)) === null || _exec2 === void 0 ? void 0 : _exec2[1];
    if (fileId && clientId) {
      setClientIdForFile(fileId, clientId);
    }
  }
};