'use strict';

import React from 'react';
import {FormGroup, FormGroupDefaultProps, FormGroupProps} from './FormGroup';
import PropTypes from 'prop-types';
import {CheckboxControl} from './CheckboxControl';

type Props = {
    hiddenLabel?: boolean;
    removeName?: boolean;
    labelClassName?: string;
} & FormGroupProps &
    Partial<DefaultProps> &
    React.HTMLProps<Checkbox>;

type DefaultProps = {
    testAutomationId: string;
    hiddenLabel: boolean;
    removeName: boolean;
} & FormGroupDefaultProps;

export class Checkbox extends FormGroup<Props> {
    static propTypes: any = {
        ...FormGroup.propTypes,
        hiddenLabel: PropTypes.bool,
        removeName: PropTypes.bool
    };

    static defaultProps = {
        ...FormGroup.defaultProps,
        testAutomationId: 'checkbox',
        hiddenLabel: false,
        removeName: false
    };

    controlLabelFallback(): boolean {
        return false;
    }

    renderInput(hasError: boolean): React.ReactNode {
        let {label, children, ...props} = this.props;
        let ariaDescProps = this.getAriaDescProps(hasError);
        return (
            <CheckboxControl
                {...this.getSafeProps(props)}
                ref="control"
                hiddenLabel={this.props.hiddenLabel}
                removeName={this.props.removeName}
                hasFormGroupContainer
                {...ariaDescProps}>
                {children || label}
            </CheckboxControl>
        );
    }
}
