/// <reference path="../../lynx.d.ts" />
/**
 * IconButton Component - Lynx 版 MUI IconButton
 * 100% 一比一复刻 MUI IconButton
 * 
 * 对应 MUI: packages/mui-material/src/IconButton/IconButton.js
 */
import './IconButton.css'
import iconButtonClasses, { getIconButtonUtilityClass } from './iconButtonClasses'
export { iconButtonClasses, getIconButtonUtilityClass }

// =============================================
// 类型定义 - 对应 MUI IconButton.d.ts
// =============================================

export type IconButtonColor = 'inherit' | 'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning'
export type IconButtonSize = 'small' | 'medium' | 'large'

export interface IconButtonProps {
  /** 子元素 (图标) */
  children?: any
  /** 自定义类名 */
  className?: string
  /** 样式类覆盖 */
  classes?: Partial<typeof iconButtonClasses>
  /** 颜色 */
  color?: IconButtonColor
  /** 是否禁用 */
  disabled?: boolean
  /** 是否禁用焦点涟漪 */
  disableFocusRipple?: boolean
  /** 是否禁用涟漪 */
  disableRipple?: boolean
  /** 边缘位置 */
  edge?: 'start' | 'end' | false
  /** 尺寸 */
  size?: IconButtonSize
  /** 内联样式 */
  style?: Record<string, any>
  /** sx 属性 */
  sx?: Record<string, any>
  /** 点击事件 */
  bindtap?: (event?: any) => void
  onClick?: (event?: any) => void
}

// =============================================
// 辅助函数
// =============================================

function capitalize(str: string): string {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

function composeClasses(
  slots: Record<string, (string | boolean | null | undefined)[]>,
  getUtilityClass: (slot: string) => string,
  classes?: Record<string, string>
): Record<string, string> {
  const output: Record<string, string> = {}
  
  Object.keys(slots).forEach((slot) => {
    output[slot] = slots[slot]
      .filter(Boolean)
      .map((key) => {
        if (classes && classes[key as string]) {
          return `${getUtilityClass(key as string)} ${classes[key as string]}`
        }
        return getUtilityClass(key as string)
      })
      .join(' ')
  })
  
  return output
}

// =============================================
// useUtilityClasses
// =============================================

interface OwnerState extends IconButtonProps {}

function useUtilityClasses(ownerState: OwnerState) {
  const { 
    color = 'default', 
    disabled, 
    edge, 
    size = 'medium', 
    classes 
  } = ownerState

  const slots = {
    root: [
      'root',
      disabled && 'disabled',
      color !== 'default' && `color${capitalize(color)}`,
      edge && `edge${capitalize(edge as string)}`,
      `size${capitalize(size)}`,
    ],
  }

  return composeClasses(slots, getIconButtonUtilityClass, classes)
}

// =============================================
// IconButton 组件 - 完整实现
// =============================================

export function IconButton(props: IconButtonProps) {
  const {
    children,
    className,
    classes: classesProp,
    color = 'default',
    disabled = false,
    disableFocusRipple = false,
    disableRipple = false,
    edge = false,
    size = 'medium',
    style,
    sx,
    bindtap,
    onClick,
    ...other
  } = props

  const ownerState: OwnerState = {
    ...props,
    color,
    disabled,
    disableFocusRipple,
    edge,
    size,
  }

  const classes = useUtilityClasses(ownerState)

  // 构建最终类名
  const rootClasses = [
    classes.root,
    className,
    disabled && iconButtonClasses.disabled,
  ].filter(Boolean).join(' ')

  // 处理点击
  const handleTap = (event?: any) => {
    if (disabled) return
    if (bindtap) bindtap(event)
    if (onClick) onClick(event)
  }

  return (
    <view
      className={rootClasses}
      style={sx ? { ...sx, ...style } : style}
      bindtap={handleTap}
      accessibility-element={true}
      accessibility-role="button"
      {...other}
    >
      {children}
    </view>
  )
}

export default IconButton
