import React, { Component } from 'react';
import cls from 'classnames';
import { Search as NextSearch } from '@alifd/next';
import { SearchProps as NextSearchProps } from '@alifd/next/types/search';


interface SearchProps extends Omit<NextSearchProps, 'size'|'type'> {
  size?: 'large' | 'medium' | 'small' | 'xs';
  type?: 'normal' | 'primary' | 'secondary' | 'dark' | 'brand';
  theme?: 'white' | 'gray';
}

interface ISearchState {
  focus?: boolean;
}

class Search extends Component<SearchProps, ISearchState> {
  constructor(props) {
    super(props);
    this.state = {
      focus: false,
    };
  }

  onFocus = e => {
    this.setState({
      focus: true,
    });
    const { onFocus } = this.props;
    onFocus && onFocus(e);
  };

  onBlur = e => {
    this.setState({
      focus: false,
    });
    const { onBlur } = this.props;
    onBlur && onBlur(e);
  };

  render() {
    const { prefix = 'next-', size = 'medium', theme = 'white', type = 'normal', ...otherProps } = this.props;
    return <NextSearch
      size={size as 'large' | 'medium'}
      {...otherProps}
      className = {cls(
        this.props.className, {
          [`${prefix}contain-focus`]: this.state.focus === true,
          [`${prefix}search-small`]: size === 'small',
          [`${prefix}search-xs`]: size === 'xs',
          [`${prefix}search-with-no-title`]: !this.props.searchText,
          [`${prefix}search-gray-theme`]: theme === 'gray',
          [`${prefix}search-white-theme`]: theme === 'white',
          [`${prefix}search-brand-type`]: type === 'brand',
        })}
      onBlur={this.onBlur}
      onFocus={this.onFocus}
    />;
  }
}
export default Search;
