"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* @flow */
const React = __importStar(require("react"));
{
    ToggleButtonProps;
}
from;
'./types';
const classnames_1 = __importDefault(require("classnames"));
const prop_types_1 = __importDefault(require("prop-types"));
const withStyles_1 = __importDefault(require("components/hoc/withStyles"));
const ErrorBoundary_1 = __importDefault(require("components/hoc/ErrorBoundary"));
const styles_1 = __importDefault(require("./styles"));
class ToggleButton extends React.Component {
    constructor(props) {
        super(props);
        this._handleChange = (index) => {
            if (index === this.state.selectedIndex) {
                return;
            }
            const { labels } = this.props;
            this.setState({ selectedIndex: index });
            if (this.props.onChange) {
                this.props.onChange(labels[index], index);
            }
        };
        this._renderButtons = () => {
            const { classes, labels } = this.props;
            const { selectedIndex } = this.state;
            return (React.createElement("div", { className: classnames_1.default(classes.root) },
                React.createElement("div", { className: classnames_1.default(classes.buttons) }, labels.map((label, index) => (React.createElement("div", { "data-test": `button-${index}`, onClick: () => {
                        this._handleChange(index);
                    }, className: classnames_1.default(classes.button, {
                        active: selectedIndex === index,
                    }), key: index }, label)))),
                React.createElement("div", { className: classnames_1.default(classes.highlighter, {
                        'is-at-first': selectedIndex === 0,
                        'is-at-last': selectedIndex === labels.length - 1,
                    }), style: {
                        left: `${(100 / labels.length) * selectedIndex}%`,
                    } })));
        };
        this.state = {
            selectedIndex: props.selectedIndex || 0,
        };
    }
    componentDidUpdate(prevProps) {
        if (this.props.selectedIndex !== prevProps.selectedIndex) {
            this.setState({ selectedIndex: this.props.selectedIndex });
        }
    }
    render() {
        let { classes, title, id } = this.props;
        return (React.createElement(ErrorBoundary_1.default, null,
            React.createElement("div", { className: classnames_1.default(classes.component), id: id },
                React.createElement("div", { className: classes.labelWrapper }, Boolean(title) && React.createElement("div", { className: classes.label }, title)),
                this._renderButtons())));
    }
}
ToggleButton.propTypes = {
    id: prop_types_1.default.string,
    title: prop_types_1.default.string,
    labels: prop_types_1.default.arrayOf(prop_types_1.default.string).isRequired,
    activeColor: prop_types_1.default.string,
    onChange: prop_types_1.default.func,
    selectedIndex: prop_types_1.default.number,
    classes: prop_types_1.default.object,
};
ToggleButton.defaultProps = {
    id: '',
    onChange: () => { },
};
exports.default = withStyles_1.default(styles_1.default)(ToggleButton);
//# sourceMappingURL=index.js.map