import { useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid';

/**
 * Return a memoised unique ID as a string.
 *
 * Returns externalId if provided, otherwise generates a random ID
 * (with an optional prefix).
 */
export function useExternalId(prefix, externalId) {
  return useMemo(function () {
    if (externalId) {
      return externalId;
    }
    if (prefix) {
      return "".concat(prefix, "-").concat(uuidv4());
    }
    return uuidv4();
  }, [externalId, prefix]);
}