import React from 'react';
import PropTypes from 'prop-types';
import {FlexBlock} from '../flexBlock/FlexBlock';

export class Switcher extends React.PureComponent<any> {
    static childContextTypes = {
        selectedId: PropTypes.string,
        onItemClick: PropTypes.func
    };

    static propTypes = {
        selectedId: PropTypes.string.isRequired,
        testAutomationId: PropTypes.string,
        onToggle: PropTypes.func
    };

    static defaultProps = {
        testAutomationId: null,
        onToggle: () => {}
    };

    getChildContext() {
        return {
            selectedId: this.props.selectedId,
            onItemClick: this.onItemClick
        };
    }

    onItemClick = id => {
        this.props.onToggle(id);
    };

    render() {
        const {children, testAutomationId} = this.props;

        return <FlexBlock {...{testAutomationId, className: 'rc-switcher'}}>{children}</FlexBlock>;
    }
}
