"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var React = require("react");
var Utilities_1 = require("../../Utilities");
var Label_1 = require("../../Label");
var Toggle_scss_1 = require("./Toggle.scss");
var Toggle = (function (_super) {
    __extends(Toggle, _super);
    function Toggle(props) {
        var _this = _super.call(this) || this;
        _this.state = {
            isChecked: !!(props.checked || props.defaultChecked)
        };
        _this._id = Utilities_1.getId('Toggle');
        return _this;
    }
    Object.defineProperty(Toggle.prototype, "checked", {
        /**
         * Gets the current checked state of the toggle.
         */
        get: function () {
            return this.state.isChecked;
        },
        enumerable: true,
        configurable: true
    });
    Toggle.prototype.componentWillReceiveProps = function (newProps) {
        if (newProps.checked !== undefined) {
            this.setState({
                isChecked: newProps.checked
            });
        }
    };
    Toggle.prototype.render = function () {
        var _this = this;
        var _a = this.props, label = _a.label, onText = _a.onText, offText = _a.offText, className = _a.className, disabled = _a.disabled;
        var isChecked = this.state.isChecked;
        var stateText = isChecked ? onText : offText;
        return (React.createElement("div", { className: Utilities_1.css(Toggle_scss_1.default.root, 'ms-Toggle', className, (_b = {
                    'is-checked': isChecked,
                    'is-enabled': !disabled,
                    'is-disabled': disabled
                },
                _b[Toggle_scss_1.default.isChecked] = isChecked,
                _b[Toggle_scss_1.default.isEnabled] = !disabled,
                _b[Toggle_scss_1.default.isDisabled] = disabled,
                _b)) },
            React.createElement("div", { className: Utilities_1.css(Toggle_scss_1.default.innerContainer, 'ms-Toggle-innerContainer') },
                label && (React.createElement(Label_1.Label, { className: 'ms-Toggle-label', htmlFor: this._id }, label)),
                React.createElement("div", { className: Utilities_1.css(Toggle_scss_1.default.slider, 'ms-Toggle-slider') },
                    React.createElement("button", { ref: function (c) { return _this._toggleButton = c; }, type: 'button', id: this._id, name: this._id, className: Utilities_1.css(Toggle_scss_1.default.button, 'ms-Toggle-button'), disabled: disabled, role: 'checkbox', "aria-checked": isChecked, onClick: this._onClick }),
                    React.createElement("div", { className: Utilities_1.css(Toggle_scss_1.default.background, 'ms-Toggle-background') },
                        React.createElement("div", { className: Utilities_1.css(Toggle_scss_1.default.focus, 'ms-Toggle-focus') }),
                        React.createElement("div", { className: Utilities_1.css(Toggle_scss_1.default.thumb, 'ms-Toggle-thumb') })),
                    stateText && (React.createElement(Label_1.Label, { className: Utilities_1.css(Toggle_scss_1.default.stateText, 'ms-Toggle-stateText') }, stateText))))));
        var _b;
    };
    Toggle.prototype.focus = function () {
        if (this._toggleButton) {
            this._toggleButton.focus();
        }
    };
    Toggle.prototype._onClick = function () {
        var _a = this.props, checked = _a.checked, onChanged = _a.onChanged;
        var isChecked = this.state.isChecked;
        // Only update the state if the user hasn't provided it.
        if (checked === undefined) {
            this.setState({
                isChecked: !isChecked
            });
        }
        if (onChanged) {
            onChanged(!isChecked);
        }
    };
    return Toggle;
}(React.Component));
Toggle.initialProps = {
    label: '',
    onText: 'On',
    offText: 'Off'
};
__decorate([
    Utilities_1.autobind
], Toggle.prototype, "_onClick", null);
exports.Toggle = Toggle;

//# sourceMappingURL=Toggle.js.map
