/// <reference path="../../lynx.d.ts" />
/**
 * ToggleButtonGroup Component - Lynx 版 MUI ToggleButtonGroup
 * 100% 一比一复刻 MUI ToggleButtonGroup
 * 
 * 切换按钮组容器，支持单选和多选模式
 * 在 Lynx 环境下作为布局容器，开发者手动管理 ToggleButton 状态
 * 
 * 对应 MUI: packages/mui-material/src/ToggleButtonGroup/ToggleButtonGroup.js
 */
import './ToggleButtonGroup.css'
import toggleButtonGroupClasses, { getToggleButtonGroupUtilityClass } from './toggleButtonGroupClasses'
export { toggleButtonGroupClasses, getToggleButtonGroupUtilityClass }

// =============================================
// 类型定义
// =============================================

export type ToggleButtonGroupOrientation = 'horizontal' | 'vertical'
export type ToggleButtonGroupSize = 'small' | 'medium' | 'large'

export interface ToggleButtonGroupProps {
  /** 子元素 */
  children?: any
  /** 自定义类名 */
  className?: string
  /** 样式类覆盖 */
  classes?: Partial<typeof toggleButtonGroupClasses>
  /** 是否禁用所有按钮 */
  disabled?: boolean
  /** 是否禁用涟漪 */
  disableRipple?: boolean
  /** 是否为排他模式（单选） */
  exclusive?: boolean
  /** 是否全宽 */
  fullWidth?: boolean
  /** 方向 */
  orientation?: ToggleButtonGroupOrientation
  /** 尺寸 */
  size?: ToggleButtonGroupSize
  /** 当前值（单选时为 string，多选时为 string[]） */
  value?: string | string[]
  /** 内联样式 */
  style?: Record<string, any>
  /** sx 属性 */
  sx?: Record<string, any>
  /** 变化回调 */
  onChange?: (event: any, value: string | string[]) => 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 ToggleButtonGroupProps {}

function useUtilityClasses(ownerState: OwnerState) {
  const { 
    classes, 
    orientation = 'horizontal', 
    fullWidth 
  } = ownerState

  const slots = {
    root: [
      'root',
      orientation,
      fullWidth && 'fullWidth',
    ],
  }

  return composeClasses(slots, getToggleButtonGroupUtilityClass, classes)
}

// =============================================
// ToggleButtonGroup 组件
// =============================================

export function ToggleButtonGroup(props: ToggleButtonGroupProps) {
  const {
    children,
    className,
    classes: classesProp,
    disabled = false,
    disableRipple = false,
    exclusive = true,
    fullWidth = false,
    orientation = 'horizontal',
    size = 'medium',
    value,
    style,
    sx,
    onChange,
    ...other
  } = props

  const ownerState: OwnerState = {
    ...props,
    disabled,
    exclusive,
    fullWidth,
    orientation,
    size,
  }

  const classes = useUtilityClasses(ownerState)

  // 在 Lynx 环境下，不进行子组件克隆
  // 开发者需要手动管理 ToggleButton 的 selected 状态
  const enhancedChildren = children

  // 构建类名
  const rootClasses = [
    classes.root,
    toggleButtonGroupClasses.root,
    className,
    orientation === 'vertical' && toggleButtonGroupClasses.vertical,
    fullWidth && toggleButtonGroupClasses.fullWidth,
  ].filter(Boolean).join(' ')

  return (
    <view
      className={rootClasses}
      style={sx ? { ...sx, ...style } : style}
      role="group"
      aria-orientation={orientation}
      {...other}
    >
      {enhancedChildren}
    </view>
  )
}

export default ToggleButtonGroup
