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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"use client";

// @ts-nocheck - pre-existing legacy design token (bare 'colors'/'spacing' etc) issues in shared components.

import Link from "next/link";
import { forwardRef, ButtonHTMLAttributes, AnchorHTMLAttributes } from "react";
import { clsx } from "clsx";
import { designSystem, colors, typography, spacing } from "@/lib/design-system";
import { Loader2 } from "lucide-react";

const { semanticColors } = designSystem; // colors/radius etc reserved for variants

// =============================================================================
// Button Types and Interfaces
// =============================================================================

export type ButtonVariant = 
  | "primary"
  | "secondary"
  | "ghost"
  | "outline"
  | "danger"
  | "success"
  | "emerald"
  | "violet"
  | "amber"
  | "blue";

export type ButtonSize = "sm" | "md" | "lg" | "xl" | "icon";

export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
  variant?: ButtonVariant;
  size?: ButtonSize;
  loading?: boolean;
  fullWidth?: boolean;
  leftIcon?: React.ReactNode;
  rightIcon?: React.ReactNode;
  asLink?: boolean;
  href?: string;
}

// =============================================================================
// Style Mappings
// =============================================================================

const variantStyles = {
  primary: {
    bg: colors.accentPrimary,
    text: colors.white,
    hoverBg: colors.accentPrimaryHover,
    border: "none",
    activeBg: colors.accentPrimary,
  },
  secondary: {
    bg: colors.bgSurface,
    text: colors.textPrimary,
    hoverBg: colors.bgElevated,
    border: colors.borderMedium,
    hoverBorder: colors.borderMedium,
    activeBg: colors.bgElevated,
  },
  ghost: {
    bg: "transparent",
    text: colors.textSecondary,
    hoverBg: colors.bgGlass,
    border: "none",
    hoverBorder: "none",
    hoverText: colors.textPrimary,
    activeBg: colors.bgGlass,
  },
  outline: {
    bg: "transparent",
    text: colors.textPrimary,
    hoverBg: colors.bgGlass,
    border: colors.borderSubtle,
    hoverBorder: colors.borderMedium,
    activeBg: colors.bgGlass,
  },
  danger: {
    bg: colors.accentRed,
    text: colors.white,
    hoverBg: "#F87171", // lighter red
    border: "none",
    activeBg: colors.accentRed,
  },
  success: {
    bg: colors.accentEmerald,
    text: colors.white,
    hoverBg: "#10B981", // same or lighter
    border: "none",
    activeBg: colors.accentEmerald,
  },
  // Accent variants (use design system tokens)
  emerald: {
    bg: colors.accentEmerald,
    text: colors.white,
    hoverBg: "#34D399", // lighter emerald
    border: "none",
    activeBg: colors.accentEmerald,
  },
  violet: {
    bg: semanticColors.info,
    text: colors.white,
    hoverBg: colors.accentViolet,
    border: "none",
    activeBg: semanticColors.info,
  },
  amber: {
    bg: colors.accentPrimary,
    text: colors.white,
    hoverBg: colors.accentPrimaryHover,
    border: "none",
    activeBg: colors.accentPrimary,
  },
  blue: {
    bg: colors.accentBlue,
    text: colors.white,
    hoverBg: "#93C5FD", // lighter blue
    border: "none",
    activeBg: colors.accentBlue,
  },
} as const;

const sizeStyles = {
  sm: {
    height: "32px",
    padding: "0 12px",
    fontSize: typography.captionSmall.size,
    fontWeight: typography.captionSmall.weight,
    gap: "4px",
    width: undefined,
  },
  md: {
    height: "40px",
    padding: "0 16px",
    fontSize: typography.button.size,
    fontWeight: typography.button.weight,
    gap: spacing.button.gap,
    width: undefined,
  },
  lg: {
    height: "44px",
    padding: "0 24px",
    fontSize: typography.buttonPrimary.size,
    fontWeight: typography.buttonPrimary.weight,
    gap: spacing.button.gap,
    width: undefined,
  },
  xl: {
    height: "48px",
    padding: "0 32px",
    fontSize: "15px",
    fontWeight: "600" as const,
    gap: "10px",
    width: undefined,
  },
  icon: {
    height: "40px",
    width: "40px",
    padding: "0",
    fontSize: "20px",
    gap: "0",
  },
} as const;

// Helper to extract Link-compatible props safely
function extractLinkProps(props: ButtonProps): Partial<AnchorHTMLAttributes<HTMLAnchorElement>> {
  const {
    type: _type,
    form: _form,
    formAction: _formAction,
    formEncType: _formEncType,
    formMethod: _formMethod,
    formNoValidate: _formNoValidate,
    formTarget: _formTarget,
    name: _name,
    value: _value,
    autoFocus: _autoFocus,
    tabIndex: _tabIndex,
    ...safeProps
  } = props;
  return safeProps as Partial<AnchorHTMLAttributes<HTMLAnchorElement>>;
}

// =============================================================================
// SharedButton Component
// =============================================================================

export const SharedButton = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
  (
    {
      variant = "primary",
      size = "md",
      loading = false,
      fullWidth = false,
      leftIcon,
      rightIcon,
      asLink = false,
      href,
      disabled,
      className,
      children,
      ...props
    },
    ref,
  ) => {
    const styles = variantStyles[variant];
    const sizes = sizeStyles[size];

    const baseClasses = clsx(
      "relative z-10 pointer-events-auto",
      "inline-flex items-center justify-center",
      "border",
      "font-sans",
      "transition-all",
      "duration-150",
      "focus:outline-none",
      "focus-visible:ring-2",
      "focus-visible:ring-[var(--accent-primary)]",
      "focus-visible:ring-offset-2",
      "focus-visible:ring-offset-[var(--bg-root)]",
      fullWidth && "w-full",
      className,
    );

    const sizeClasses = clsx(
      `h-[${sizes.height}]`,
      sizes.width && `w-[${sizes.width}]`,
      sizes.padding && `px-[${sizes.padding.split(" ")[0].replace("px", "")}px]`,
      sizes.fontSize && `text-[${sizes.fontSize}]`,
      "fontWeight" in sizes && (sizes as { fontWeight?: string }).fontWeight && `font-[${(sizes as { fontWeight?: string }).fontWeight}]`,
      sizes.gap && `gap-[${sizes.gap}]`,
    );

    const variantClasses = clsx(
      !styles.bg || styles.bg === "transparent" ? "" : `bg-[${styles.bg}]`,
      styles.text && `text-[${styles.text}]`,
      !styles.border || styles.border === "none" ? "border-transparent" : `border-[${styles.border}]`,
      // Hover states
      styles.hoverBg && `hover:bg-[${styles.hoverBg}]`,
      ("hoverBorder" in styles && (styles as { hoverBorder?: string }).hoverBorder) && `hover:border-[${(styles as { hoverBorder?: string }).hoverBorder}]`,
      ("hoverText" in styles && (styles as { hoverText?: string }).hoverText) && `hover:text-[${(styles as { hoverText?: string }).hoverText}]`,
      // Active states
      styles.activeBg && `active:bg-[${styles.activeBg}]`,
      // Disabled states
      "disabled:cursor-not-allowed",
      "disabled:opacity-40",
      "disabled:transform-none",
    );

    const allClasses = clsx(baseClasses, sizeClasses, variantClasses);

    // Loading state
    if (loading) {
      return (
        <button
          ref={ref as React.RefObject<HTMLButtonElement>}
          type="button"
          disabled={true}
          aria-busy="true"
          className={clsx(allClasses, "cursor-not-allowed")}
          {...props}
        >
          <Loader2 className="size-4 animate-spin" aria-hidden="true" />
          <span className="sr-only">Loading...</span>
        </button>
      );
    }

    // Render as Link if asLink or href is provided
    if (asLink || href) {
      const linkProps = asLink ? { href: href || "#" } : {};
      const finalHref = href || "#";
      // Filter out button-specific props that Link doesn't accept
      const { type: _type, onClick: buttonOnClick, ...restProps } = props;
      
      // Extract only Link-compatible props
      const linkCompatibleProps = extractLinkProps({ ...restProps } as ButtonProps);
      
      // Safe onClick handler with proper typing
      const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
        if (disabled) {
          e.preventDefault();
          return;
        }
        buttonOnClick?.(e as unknown as React.MouseEvent<HTMLButtonElement>);
      };

      return (
        <Link
          ref={ref as React.RefObject<HTMLAnchorElement>}
          href={finalHref}
          className={clsx(allClasses, "cursor-pointer", disabled && "pointer-events-none opacity-40")}
          {...linkCompatibleProps}
          {...linkProps}
          onClick={handleClick}
        >
          {leftIcon && <span aria-hidden="true">{leftIcon}</span>}
          {children}
          {rightIcon && <span aria-hidden="true">{rightIcon}</span>}
        </Link>
      );
    }

    // Render as button
    return (
      <button
        ref={ref as React.RefObject<HTMLButtonElement>}
        type="button"
        disabled={disabled}
        className={clsx(allClasses, !disabled && "cursor-pointer")}
        {...props}
      >
        {leftIcon && <span aria-hidden="true">{leftIcon}</span>}
        {children}
        {rightIcon && <span aria-hidden="true">{rightIcon}</span>}
      </button>
    );
  },
);

SharedButton.displayName = "SharedButton";

// =============================================================================
// Convenience Button Components (matching existing patterns)
// =============================================================================

// Button with consistent primary style
export function BtnPrimary({
  children,
  onClick,
  loading = false,
  className,
  ...props
}: ButtonProps) {
  return (
    <SharedButton
      variant="primary"
      onClick={onClick}
      loading={loading}
      className={clsx("rounded-lg", className)}
      {...props}
    >
      {children}
    </SharedButton>
  );
}

// Button with secondary style
export function BtnSecondary({
  children,
  onClick,
  loading = false,
  className,
  ...props
}: ButtonProps) {
  return (
    <SharedButton
      variant="secondary"
      onClick={onClick}
      loading={loading}
      className={clsx("rounded-lg", className)}
      {...props}
    >
      {children}
    </SharedButton>
  );
}

// Ghost button
export function BtnGhost({
  children,
  onClick,
  className,
  ...props
}: ButtonProps) {
  return (
    <SharedButton
      variant="ghost"
      onClick={onClick}
      className={clsx("rounded-lg", className)}
      {...props}
    >
      {children}
    </SharedButton>
  );
}

// Action button (for teach page pattern)
export function ActionButton({
  children,
  primary = false,
  onClick,
  className,
  ...props
}: ButtonProps & { primary?: boolean }) {
  return (
    <SharedButton
      variant={primary ? "primary" : "secondary"}
      size="sm"
      onClick={onClick}
      className={clsx(
        "rounded-md",
        primary ? "text-white" : "text-[var(--text-secondary)] hover:text-[var(--text-primary)]",
        className,
      )}
      {...props}
    >
      {children}
    </SharedButton>
  );
}