import ConfigProvider from '../config-provider';
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';

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 = '', prefix = 'next-' } = 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 { size, ...otherProps } = this.props;
      return (
        <NextButton
          {...otherProps}
          size={size as 'large' | 'medium' | 'small'}
          type={type}
        />
      );
    }

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

hoistNonReactStatics(Button, NextButton);

// export default Button;

export default ConfigProvider.config(Button);
