/**
 * Blob URL helpers — used by the photo edit flow (which creates blob: URLs
 * from cropped images and needs to revoke them once the canvas swaps to a
 * new src) and by persistence (which strips blob: URLs out of the saved
 * blob because they don't survive a reload).
 *
 * Before this module these checks were inline string-prefix tests —
 * `previousSrc.startsWith('blob:')`, `el.src.startsWith('blob:')`. Wrapping
 * the prefix check in a name makes it greppable and prevents accidental
 * "starts with 'blob'" false positives (matching e.g. "blob_url_str").
 */

/**
 * True if the given value is a string that looks like a `blob:` URL.
 * Returns false for non-strings rather than throwing — callers usually
 * test optional fields like `element.src` and shouldn't have to null-check
 * twice.
 */
export function isBlobUrl(url) {
  return typeof url === 'string' && url.startsWith('blob:');
}

/**
 * Revoke a blob: URL if it is one; no-op for any other input. The actual
 * `URL.revokeObjectURL` call is wrapped in try/catch because some browsers
 * throw on already-revoked URLs and we don't want to crash the photo-edit
 * cleanup path over a double-revoke.
 */
export function revokeBlobUrl(url) {
  if (!isBlobUrl(url)) return;
  try {
    URL.revokeObjectURL(url);
  } catch {
    /* Already-revoked, missing URL global, etc. — ignore. */
  }
}
