import { useTranslation } from "@enode/core/translations/hooks";

// Shared loading placeholders. Presentational; the fill token matches the
// hand-written `animate-pulse` placeholders across the portal.

const ROUNDED_CLASS = {
  sm: "rounded",
  md: "rounded-md",
  xl: "rounded-xl",
  full: "rounded-full",
} as const;

export type SkeletonRounded = keyof typeof ROUNDED_CLASS;

/**
 * Pulsing block placeholder. Size and shape come from `className`
 * (e.g. `h-4 w-24`, `size-9`).
 *
 * @param className - Tailwind size / spacing classes.
 * @param rounded - Corner radius; defaults to `"md"`. Use `"full"` for avatars.
 * @remarks Decorative (`aria-hidden`); the pulse is disabled under
 *   `prefers-reduced-motion`. Wrap groups in a `role="status"` region with a
 *   label (see `SkeletonRows`) so assistive tech announces the loading state.
 */
export function Skeleton({
  className = "",
  rounded = "md",
}: {
  className?: string;
  rounded?: SkeletonRounded;
}) {
  return (
    <div
      aria-hidden
      className={`animate-pulse bg-surface-base-muted motion-reduce:animate-none ${ROUNDED_CLASS[rounded]} ${className}`}
    />
  );
}

/**
 * Placeholder for prose or a text block: `lines` stacked lines, the last one
 * shorter. Decorative (`aria-hidden` lines); add your own status label if used
 * standalone.
 *
 * @param lines - Number of lines; defaults to 3.
 */
export function SkeletonLines({
  lines = 3,
  className = "",
}: {
  lines?: number;
  className?: string;
}) {
  return (
    <div className={`flex flex-col gap-2 ${className}`}>
      {Array.from({ length: lines }, (_, index) => (
        <Skeleton
          key={index}
          className={`h-3 ${index === lines - 1 && lines > 1 ? "w-2/3" : "w-full"}`}
        />
      ))}
    </div>
  );
}

/**
 * Loading block for a drawer or list: `rows` of avatar circle + two lines,
 * mirroring the list-row look.
 *
 * @param rows - Number of rows; defaults to 4.
 * @remarks Renders a `role="status"` container with a translated screen-reader
 *   label ("Loading…").
 */
export function SkeletonRows({
  rows = 4,
  className = "",
}: {
  rows?: number;
  className?: string;
}) {
  const { t } = useTranslation();
  return (
    <div role="status" className={`flex flex-col gap-4 ${className}`}>
      <span className="sr-only">{t("Loading…")}</span>
      {Array.from({ length: rows }, (_, index) => (
        <div key={index} className="flex items-center gap-3">
          <Skeleton rounded="full" className="size-9 shrink-0" />
          <div className="flex min-w-0 flex-1 flex-col gap-2">
            <Skeleton className="h-4 w-1/2" />
            <Skeleton className="h-3 w-1/3" />
          </div>
        </div>
      ))}
    </div>
  );
}
