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 __());
};
define(["require", "exports", 'react', '../../utilities/css', '../../Label', './Toggle.scss'], function (require, exports, React, css_1, Label_1) {
    "use strict";
    var _instance = 0;
    var Toggle = (function (_super) {
        __extends(Toggle, _super);
        function Toggle(props) {
            _super.call(this);
            this.state = {
                isChecked: !!(props.checked || props.defaultChecked)
            };
            this._id = "Toggle-" + _instance++;
            this._onClick = this._onClick.bind(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: css_1.css('ms-Toggle', {
                'is-checked': isChecked,
                'is-enabled': !disabled,
                'is-disabled': disabled
            })}, React.createElement("div", {className: 'ms-Toggle-innerContainer'}, React.createElement(Label_1.Label, {className: 'ms-Toggle-label', htmlFor: this._id}, label), React.createElement("div", {className: 'ms-Toggle-slider'}, React.createElement("button", {ref: function (c) { return _this._toggleButton = c; }, id: this._id, name: this._id, className: css_1.css('ms-Toggle-button', className), disabled: disabled, role: 'button', "aria-pressed": isChecked, onClick: this._onClick}), React.createElement("div", {className: 'ms-Toggle-background'}, React.createElement("div", {className: 'ms-Toggle-focus'}), React.createElement("div", {className: 'ms-Toggle-thumb'})), React.createElement(Label_1.Label, {className: 'ms-Toggle-stateText'}, stateText)))));
        };
        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);
            }
        };
        Toggle.initialProps = {
            label: '',
            onText: 'On',
            offText: 'Off'
        };
        return Toggle;
    }(React.Component));
    exports.Toggle = Toggle;
});
