"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"));
{
    ToggleSwitchProps;
}
from;
'./types';
const classnames_1 = __importDefault(require("classnames"));
const prop_types_1 = __importDefault(require("prop-types"));
const Switch_1 = __importDefault(require("@material-ui/core/Switch"));
const withStyles_1 = __importDefault(require("components/hoc/withStyles"));
const ErrorBoundary_1 = __importDefault(require("components/hoc/ErrorBoundary"));
const styles_1 = __importDefault(require("./styles"));
class ToggleSwitch extends React.Component {
    constructor(props) {
        super(props);
        this.changeValue = value => {
            this.setState({ value });
            if (this.props.onChange) {
                this.props.onChange(value);
            }
        };
        this._handleChange = (event, checked) => {
            this.setState({ value: checked });
            if (this.props.onChange) {
                this.props.onChange(checked);
            }
        };
        this._renderSwitch = () => {
            const { classes, offLabel, onLabel } = this.props;
            return (React.createElement("div", { className: classes.toggleSwitchWrapper, "data-test": `toggle-switch${this.state.value ? '-checked' : ''}` },
                React.createElement(Switch_1.default, { checked: this.state.value, classes: {
                        root: classes.root,
                        icon: classes.icon,
                        bar: classes.bar,
                        checked: classes.checked,
                    }, disabled: this.props.disabled, onChange: this._handleChange }),
                !this.state.value && (React.createElement("div", { className: classes.offLabel }, offLabel)),
                this.state.value && React.createElement("div", { className: classes.onLabel }, onLabel)));
        };
        this.state = {
            value: props.value,
        };
    }
    componentDidUpdate(prevProps) {
        const props = this.props;
        if (props.value !== prevProps.value) {
            this.setState({
                value: props.value,
            });
        }
    }
    _renderLabel() {
        const { classes, label } = this.props;
        return React.createElement("div", { className: classes.label }, label);
    }
    render() {
        let { classes, label } = this.props;
        let componentClass = classnames_1.default(classes.switchComponent);
        return (React.createElement(ErrorBoundary_1.default, null,
            React.createElement("div", { className: componentClass },
                Boolean(label) && this._renderLabel(),
                this._renderSwitch())));
    }
}
ToggleSwitch.propTypes = {
    offLabel: prop_types_1.default.string,
    offColor: prop_types_1.default.string,
    onLabel: prop_types_1.default.string,
    onColor: prop_types_1.default.string,
    onChange: prop_types_1.default.func,
    disabled: prop_types_1.default.bool,
    value: prop_types_1.default.bool,
    label: prop_types_1.default.string,
    compact: prop_types_1.default.bool,
    classes: prop_types_1.default.object,
};
ToggleSwitch.defaultProps = {
    offLabel: 'OFF',
    onLabel: 'ON',
    onChange: () => { },
    value: false,
};
exports.default = withStyles_1.default(styles_1.default)(ToggleSwitch);
//# sourceMappingURL=index.js.map