import type { TestimonialContent } from '$lib/types';

function hashString(value: string) {
  let hash = 0;

  for (let index = 0; index < value.length; index += 1) {
    hash = (hash * 31 + value.charCodeAt(index)) >>> 0;
  }

  return hash;
}

export function getSeededTestimonialIndex(
  testimonials: Array<Pick<TestimonialContent, 'reviewer'>>,
  seedKey: string
) {
  if (!testimonials.length) {
    return 0;
  }

  if (!seedKey) {
    return 0;
  }

  return hashString(seedKey) % testimonials.length;
}
