import * as tslib_1 from "tslib";
import * as React from 'react';
import { BaseComponent, classNamesFunction, createRef, getId, inputProperties, getNativeProps } from '../../Utilities';
import { Label } from '../../Label';
import { KeytipData } from '../../KeytipData';
var getClassNames = classNamesFunction();
var ToggleBase = /** @class */ (function (_super) {
    tslib_1.__extends(ToggleBase, _super);
    function ToggleBase(props) {
        var _this = _super.call(this, props) || this;
        _this._toggleButton = createRef();
        _this._onClick = function (ev) {
            var _a = _this.props, disabled = _a.disabled, checkedProp = _a.checked, onChanged = _a.onChanged, onClick = _a.onClick;
            var checked = _this.state.checked;
            if (!disabled) {
                // Only update the state if the user hasn't provided it.
                if (checkedProp === undefined) {
                    _this.setState({
                        checked: !checked
                    });
                }
                if (onChanged) {
                    onChanged(!checked);
                }
                if (onClick) {
                    onClick(ev);
                }
            }
        };
        _this._warnMutuallyExclusive({
            checked: 'defaultChecked'
        });
        _this.state = {
            checked: !!(props.checked || props.defaultChecked)
        };
        _this._id = props.id || getId('Toggle');
        return _this;
    }
    Object.defineProperty(ToggleBase.prototype, "checked", {
        /**
         * Gets the current checked state of the toggle.
         */
        get: function () {
            return this.state.checked;
        },
        enumerable: true,
        configurable: true
    });
    ToggleBase.prototype.componentWillReceiveProps = function (newProps) {
        if (newProps.checked !== undefined) {
            this.setState({
                checked: !!newProps.checked // convert null to false
            });
        }
    };
    ToggleBase.prototype.render = function () {
        var _this = this;
        var _a = this.props, _b = _a.as, RootType = _b === void 0 ? 'div' : _b, className = _a.className, theme = _a.theme, disabled = _a.disabled, keytipProps = _a.keytipProps, label = _a.label, offAriaLabel = _a.offAriaLabel, offText = _a.offText, onAriaLabel = _a.onAriaLabel, onText = _a.onText, styles = _a.styles;
        var checked = this.state.checked;
        var stateText = checked ? onText : offText;
        var ariaLabel = checked ? onAriaLabel : offAriaLabel;
        var toggleNativeProps = getNativeProps(this.props, inputProperties, ['defaultChecked']);
        var classNames = getClassNames(styles, {
            theme: theme,
            className: className,
            disabled: disabled,
            checked: checked
        });
        return (React.createElement(RootType, { className: classNames.root },
            label && (React.createElement(Label, { htmlFor: this._id, className: classNames.label }, label)),
            React.createElement("div", { className: classNames.container },
                React.createElement(KeytipData, { keytipProps: keytipProps, ariaDescribedBy: toggleNativeProps['aria-describedby'], disabled: disabled }, function (keytipAttributes) { return (React.createElement("button", tslib_1.__assign({}, toggleNativeProps, keytipAttributes, { className: classNames.pill, disabled: disabled, id: _this._id, type: "button", ref: _this._toggleButton, "aria-disabled": disabled, "aria-pressed": checked, "aria-label": ariaLabel ? ariaLabel : label, "data-is-focusable": true, onChange: _this._noop, onClick: _this._onClick }),
                    React.createElement("div", { className: classNames.thumb }))); }),
                stateText && (React.createElement(Label, { htmlFor: this._id, className: classNames.text }, stateText)))));
    };
    ToggleBase.prototype.focus = function () {
        if (this._toggleButton.current) {
            this._toggleButton.current.focus();
        }
    };
    ToggleBase.prototype._noop = function () {
        /* no-op */
    };
    return ToggleBase;
}(BaseComponent));
export { ToggleBase };
//# sourceMappingURL=Toggle.base.js.map