import React, { Component } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { Button as NextButton } from '@alifd/next';
import { ButtonProps as NextButtonProps } from '@alifd/next/types/button';
import { omitProps } from '../utils/object';

interface ButtonProps extends Omit<NextButtonProps, 'size'> {
  size?: 'xl' | 'large' | 'medium' | 'small' | 'xs';
  icon?: boolean;
}

class Button extends Component<ButtonProps> {
  static Group = NextButton.Group;

  render() {
    const { size, warning, icon, className = '' } = this.props;
    // 警告按钮就取默认type
    let type = this.props.type;
    if (warning) {
      type = undefined;
    } else if (icon) {
      if (this.props.type === 'primary') {
        type = 'secondary';
      }
    }

    // xl/xs 模式
    if (size === 'xl' || size === 'xs') {
      const { prefix = 'next-', className, ...otherProps } = this.props;
      const cls = `${prefix}${size}`;
      return (
        <NextButton
          {...omitProps(otherProps, [ 'size' ])}
          type={type}
          prefix={prefix}
          className={className ? `${className} ${cls}` : cls}
        />
      );
    }

    return (
      <NextButton
        {...this.props}
        type={type}
        size={size as 'large' | 'medium' | 'small'}
        className={icon ? `${className ? className + ' ' : ''}next-btn-type-icon ` : className}
      />
    );
  }
}

hoistNonReactStatics(Button, NextButton);

export default Button;
