/*
*  author: Yagnik Poshiya
*  organization: Webbrains Technologies Private Limited
*/
'use client';

import { cn } from '@/lib/utils';

/** A single placeholder block. Sized by the caller. */
export function Skeleton({
  className,
  style,
}: {
  className?: string;
  style?: React.CSSProperties;
}) {
  return <div className={cn('ae-skeleton', className)} style={style} aria-hidden />;
}

type TableSkeletonProps = {
  /**
   * The same grid template the real rows use, so placeholder cells land in the
   * real columns and the table does not jump when data arrives.
   */
  gridClassName: string;
  /** Match the table's page size, so the list does not resize on load. */
  rows: number;
  /**
   * Width of the bar in each column, as a CSS width. Varying them stops the
   * block reading as a solid grey slab.
   */
  columnWidths: string[];
  /** Announced to screen readers instead of the silent placeholder rows. */
  label: string;
  className?: string;
};

export function TableSkeleton({
  gridClassName,
  rows,
  columnWidths,
  label,
  className,
}: TableSkeletonProps) {
  return (
    <div role="status" aria-live="polite" aria-busy="true" className={className}>
      <span className="sr-only">{label}</span>
      {Array.from({ length: rows }).map((_row, rowIndex) => (
        <div
          key={rowIndex}
          className={cn(
            'border-t border-neutral-200 px-4 py-3 dark:border-neutral-800',
            gridClassName,
          )}
        >
          {columnWidths.map((width, colIndex) => (
            <div key={colIndex} className="flex items-center">
              <Skeleton className="h-3.5" style={{ width }} />
            </div>
          ))}
        </div>
      ))}
    </div>
  );
}

type CardGridSkeletonProps = {
  /** Number of placeholder cards. Match what a full row of real cards looks like. */
  cards: number;
  /** The same grid template the real cards use. */
  gridClassName: string;
  label: string;
  className?: string;
};

export function CardGridSkeleton({
  cards,
  gridClassName,
  label,
  className,
}: CardGridSkeletonProps) {
  return (
    <div role="status" aria-live="polite" aria-busy="true" className={cn(gridClassName, className)}>
      <span className="sr-only">{label}</span>
      {Array.from({ length: cards }).map((_card, index) => (
        <div
          key={index}
          className="flex flex-col gap-3 rounded-xl border border-neutral-200 bg-white p-5 dark:border-neutral-800 dark:bg-neutral-900"
        >
          <div className="flex items-center gap-3">
            <Skeleton className="h-9 w-9 rounded-lg" />
            <Skeleton className="h-4 w-1/2" />
          </div>
          <Skeleton className="h-3 w-full" />
          <Skeleton className="h-3 w-4/5" />
          <div className="mt-2 flex items-center gap-2">
            <Skeleton className="h-6 w-20 rounded-full" />
            <Skeleton className="h-6 w-16 rounded-full" />
          </div>
        </div>
      ))}
    </div>
  );
}
