"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.preparePrintDesignData = exports.prepareDesignDataSnapshots = exports.getMissingViewBox = exports.getMatchAt = exports.embeddedSvgImageToInlineSvgContent = exports.alterObjectValues = exports.alterArrayValues = void 0;
var getMatchAt = exports.getMatchAt = function getMatchAt(str, regexp) {
  var idx = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  var defaultValue = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
  var matches = ('' + str).match(regexp);
  return matches && matches[idx] || defaultValue;
};
var embeddedSvgImageToInlineSvgContent = exports.embeddedSvgImageToInlineSvgContent = function embeddedSvgImageToInlineSvgContent(svgStr) {
  return svgStr.replace(/<image([^>]+)xlink:href="data:image\/svg\+xml;base64,([^"]+)"[^>]*>/g, function (_, attributesStr, base64Str) {
    var x = getMatchAt(attributesStr, /x="([^"]+)"/, 1, 0);
    var y = getMatchAt(attributesStr, /y="([^"]+)"/, 1, 0);
    var transform = getMatchAt(attributesStr, /transform="([^"]+)"/, 1, '');
    var svgContent = atob(base64Str).replace(/(<\?xml[^>]+>)|(<!--[^>]+-->)/gi, '');
    return "<g transform=\"".concat(transform, " translate(").concat(x, ",").concat(y, ")\">").concat(svgContent, "</g>");
  });
};
var alterObjectValues = exports.alterObjectValues = function alterObjectValues(obj, fn) {
  Object.keys(obj).forEach(function (key) {
    obj[key] = fn(obj[key], key);
  });
  return obj;
};
var alterArrayValues = exports.alterArrayValues = function alterArrayValues(arr, fn) {
  arr.forEach(function (value, key) {
    arr[key] = fn(value, key);
  });
  return arr;
};
var prepareDesignDataSnapshots = exports.prepareDesignDataSnapshots = function prepareDesignDataSnapshots(svgArrMap) {
  return alterObjectValues(svgArrMap, function (arr) {
    return alterArrayValues(arr, embeddedSvgImageToInlineSvgContent);
  });
};
var preparePrintDesignData = exports.preparePrintDesignData = function preparePrintDesignData(svgMap) {
  return alterObjectValues(svgMap, embeddedSvgImageToInlineSvgContent);
};

/**
 * Calculates the bounding viewBox for an SVG string.
 * @param {string} svgString - The raw SVG string from the API.
 * @returns {string} The calculated viewBox (e.g., "0 0 100 100").
 */
var getMissingViewBox = exports.getMissingViewBox = function getMissingViewBox(svgString, fallbackSize) {
  var padding = 2;

  // 1. Parse the SVG string into a DOM element
  var parser = new DOMParser();
  var doc = parser.parseFromString(svgString, 'image/svg+xml');
  var svgElement = doc.querySelector('svg');
  if (!svgElement) {
    throw new Error('Invalid SVG string provided.');
  }

  // 2. Prepare the SVG to be rendered invisibly
  if (svgElement.style) {
    svgElement.style.position = 'absolute';
    svgElement.style.visibility = 'hidden';
    svgElement.style.width = 'auto';
    svgElement.style.height = 'auto';
  } else {
    svgElement.setAttribute('style', 'position: absolute; visibility: hidden; width: auto; height: auto;');
  }

  // 3. Wrap all children in a <g> tag.
  // Calling getBBox() directly on the <svg> root can be unreliable across
  // different browsers. A wrapper group guarantees we measure the actual content.
  var wrapperGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
  while (svgElement.firstChild) {
    wrapperGroup.appendChild(svgElement.firstChild);
  }
  svgElement.appendChild(wrapperGroup);

  // 4. Attach to the DOM so the browser can calculate layout dimensions
  document.body.appendChild(svgElement);

  // 5. Calculate the bounding box
  var viewBox = "0 0 ".concat(fallbackSize.width, " ").concat(fallbackSize.height);
  if (!wrapperGroup.getBBox) {
    return viewBox;
  }
  try {
    var bbox = wrapperGroup.getBBox();
    var x = bbox.x - padding;
    var y = bbox.y - padding;
    var width = bbox.width + padding * 2;
    var height = bbox.height + padding * 2;
    // Construct the viewBox string: min-x, min-y, width, height
    viewBox = "".concat(x, " ").concat(y, " ").concat(width, " ").concat(height);
  } finally {
    // 6. Clean up by removing the hidden SVG from the DOM
    document.body.removeChild(svgElement);
  }
  return viewBox;
};