/**
 * empty comment line Denis Makarov
 *
 * @author: Denis Makarov
 * @date: 2019-06-03
 */

import * as React from 'react';
import {Spin} from 'antd';
import {SpinSize} from '../../constants';

import * as styles from './spinner.m.scss';
import {joinClassNames} from '../../utils/joinClassNames';

interface ISpinnerProps {
    tip?: string; // text after spinner
    size?: SpinSize;
    isShow?: boolean;
    centered?: boolean;
}

export class Spinner extends React.Component<ISpinnerProps> {
    static defaultProps = {
        isShow: true
    };

    renderSpinnerWrapper () {
        const {isShow, centered, ...spinnerProps} = this.props;

        if (isShow) {
            return (
                <Spin className={styles.spinnerWrapper} {...spinnerProps}>
                    {this.props.children}
                </Spin>
            );
        }

        return this.props.children;
    }

    renderSpinner () {
        const {isShow, centered, ...spinnerProps} = this.props;

        if (!isShow) {
            return null;
        }

        const className = joinClassNames(
            styles.spinner,
            [styles.spinnerCentered, Boolean(centered)]
        );

        return (
            <div className={className}>
                <Spin {...spinnerProps}/>
            </div>
        );
    }

    override render () {
        if (this.props.children) {
            return this.renderSpinnerWrapper();
        } else {
            return this.renderSpinner();
        }
    }
}
