import type { CSSProperties, HTMLAttributes } from "react";

/**
 * Shared loading-skeleton primitives.
 *
 * Both render `.skeleton-box` (public/assets/css/custom-fixes.css), which is
 * globally linked, carries the shimmer, and stops animating under
 * prefers-reduced-motion. That class is deliberately self-contained — unlike
 * Bootstrap's `.placeholder`, it needs no `.placeholder-glow` parent — so a
 * skeleton box can be dropped anywhere without a wrapper.
 *
 * Purely presentational and hook-free, so this stays usable from server
 * components as well as client ones.
 */

/** A CSS length: a number is treated as px, a string is passed through ("60%", "3rem"). */
type Size = number | string;

function toCssLength(value: Size | undefined): string | undefined {
  if (value == null) return undefined;
  return typeof value === "number" ? `${value}px` : value;
}

export interface SkeletonProps extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
  /** Width. Defaults to filling the parent. */
  w?: Size;
  /** Height. Defaults to 14px — roughly one line of body text. */
  h?: Size;
  /** Corner radius. Numbers are px; omit for the class default (4px). */
  rounded?: Size;
  /** Render a circle. With only `w` set, height matches width. */
  circle?: boolean;
}

/**
 * One grey block standing in for a piece of content.
 *
 * `aria-hidden` by default: a screen reader gains nothing from the shapes, and
 * the loading state should be announced once by a live region on the container
 * rather than N times by its boxes. Pass `aria-hidden={false}` to opt out.
 */
export function Skeleton({
  w,
  h = 14,
  rounded,
  circle = false,
  className = "",
  style,
  ...rest
}: SkeletonProps) {
  const width = toCssLength(w);
  // A circle with only a width given is still a circle — mirror it onto height
  // so callers can write <Skeleton circle w={56} /> rather than repeating it.
  const height = toCssLength(circle && h === 14 && w != null ? w : h);

  const computed: CSSProperties = {
    width: width ?? "100%",
    height,
    borderRadius: circle ? "50%" : toCssLength(rounded),
    // Grey blocks inside a text flow would otherwise pick up line-height and
    // sit unevenly; this keeps a box exactly the height it was asked for.
    display: "block",
    flexShrink: 0,
  };

  return (
    <div
      aria-hidden="true"
      {...rest}
      className={`skeleton-box ${className}`.trim()}
      // Caller styles win, so a one-off tweak needs no new prop.
      style={{ ...computed, ...style }}
    />
  );
}

export interface SkeletonTextProps {
  /** Number of lines. */
  lines?: number;
  /** Height of each line, px. */
  lineHeight?: number;
  /** Vertical gap between lines, px. */
  gap?: number;
  /** Width of the final line — the ragged edge that makes a block read as prose. Ignored when `lines` is 1. */
  lastWidth?: Size;
  className?: string;
}

/** A paragraph-shaped stack of lines, the last one short. */
export function SkeletonText({
  lines = 3,
  lineHeight = 12,
  gap = 8,
  lastWidth = "60%",
  className = "",
}: SkeletonTextProps) {
  return (
    <div className={`d-flex flex-column ${className}`.trim()} style={{ gap }}>
      {Array.from({ length: Math.max(1, lines) }).map((_, i) => (
        <Skeleton
          key={i}
          h={lineHeight}
          w={lines > 1 && i === lines - 1 ? lastWidth : "100%"}
        />
      ))}
    </div>
  );
}

export default Skeleton;
