/**
 * Element-id generator. Centralizes the `element-${ts}-${random}` pattern
 * that appeared in 5+ places across `useDesignEditor.js` and
 * `App.handleAddImage` (with subtle variations — some included an index
 * for batch uniqueness, some didn't).
 *
 * Two forms:
 *   - `makeElementId()`         → unique within a single tick
 *   - `makeElementId(suffix)`   → unique even when called in a tight loop,
 *                                  by mixing a caller-supplied suffix
 *                                  (typically a loop index) into the id.
 *
 * Why both forms
 * ──────────────
 * `Date.now()` has millisecond resolution, so a `for` loop creating
 * elements would collide on the timestamp. Adding the loop index resolves
 * that without depending on `performance.now()` (which has been deliberately
 * coarsened by browsers to mitigate Spectre, so its sub-ms resolution
 * isn't reliable). The random suffix protects against process-restart
 * collisions in test environments where Date.now is mocked.
 */

/**
 * Generate a unique element id.
 *
 * @param {number|string} [suffix] - Optional disambiguator for batch
 *   creation. When omitted, the timestamp + random alone are unique
 *   enough.
 * @returns {string} `element-${timestamp}-[suffix-]${random}`
 */
export function makeElementId(suffix) {
  const ts = Date.now();
  const rand = Math.random().toString(36).slice(2, 11);
  if (suffix !== undefined) return `element-${ts}-${suffix}-${rand}`;
  return `element-${ts}-${rand}`;
}
