/**
 * DevExtreme (renovation/ui/editors/check_box.js)
 * Version: 21.2.0 (build 21200-0321)
 * Build date: Mon Jul 19 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.defaultOptions = defaultOptions;
exports.CheckBox = exports.defaultOptionRules = exports.CheckBoxProps = exports.viewFunction = void 0;
var _inferno = require("inferno");
var _vdom = require("@devextreme/vdom");
var _utils = require("../../../core/options/utils");
var _themes = require("../../../ui/themes");
var _devices = _interopRequireDefault(require("../../../core/devices"));
var _guid = _interopRequireDefault(require("../../../core/guid"));
var _widget = require("../common/widget");
var _style = require("../../../core/utils/style");
var _base_props = require("../common/base_props");
var _combine_classes = require("../../utils/combine_classes");
var _validation_message = require("../overlays/validation_message");
var _excluded = ["accessKey", "activeStateEnabled", "className", "defaultValue", "disabled", "focusStateEnabled", "height", "hint", "hoverStateEnabled", "iconHeight", "iconWidth", "isValid", "name", "onClick", "onFocusIn", "onKeyDown", "readOnly", "rtlEnabled", "saveValueChangeEvent", "tabIndex", "text", "validationError", "validationErrors", "validationMessageMode", "validationStatus", "value", "valueChange", "visible", "width"];

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _objectWithoutProperties(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = _objectWithoutPropertiesLoose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
        for (i = 0; i < sourceSymbolKeys.length; i++) {
            key = sourceSymbolKeys[i];
            if (excluded.indexOf(key) >= 0) {
                continue
            }
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) {
                continue
            }
            target[key] = source[key]
        }
    }
    return target
}

function _objectWithoutPropertiesLoose(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) {
            continue
        }
        target[key] = source[key]
    }
    return target
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, descriptor.key, descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    return Constructor
}

function _assertThisInitialized(self) {
    if (void 0 === self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return self
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf || function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}

function _extends() {
    _extends = Object.assign || function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}
var getCssClasses = function(model) {
    var isValid = model.isValid,
        readOnly = model.readOnly,
        text = model.text,
        value = model.value;
    var checked = value;
    var indeterminate = null === checked;
    var classesMap = {
        "dx-checkbox": true,
        "dx-state-readonly": !!readOnly,
        "dx-checkbox-checked": true === checked,
        "dx-checkbox-has-text": !!text,
        "dx-invalid": !isValid,
        "dx-checkbox-indeterminate": indeterminate
    };
    return (0, _combine_classes.combineClasses)(classesMap)
};
var viewFunction = function(viewModel) {
    var _viewModel$props = viewModel.props,
        name = _viewModel$props.name,
        text = _viewModel$props.text;
    return (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, _widget.Widget, _extends({
        rootElementRef: viewModel.target,
        accessKey: viewModel.props.accessKey,
        activeStateEnabled: viewModel.props.activeStateEnabled,
        className: viewModel.props.className,
        classes: viewModel.cssClasses,
        disabled: viewModel.props.disabled,
        focusStateEnabled: viewModel.props.focusStateEnabled,
        height: viewModel.props.height,
        hint: viewModel.props.hint,
        hoverStateEnabled: viewModel.props.hoverStateEnabled,
        onFocusIn: viewModel.onFocusIn,
        aria: viewModel.aria,
        onClick: viewModel.onWidgetClick,
        onKeyDown: viewModel.onWidgetKeyDown,
        rtlEnabled: viewModel.props.rtlEnabled,
        tabIndex: viewModel.props.tabIndex,
        visible: viewModel.props.visible,
        width: viewModel.props.width
    }, viewModel.restAttributes, {
        children: [(0, _inferno.normalizeProps)((0, _inferno.createVNode)(64, "input", null, null, 1, _extends({
            type: "hidden",
            value: "".concat(viewModel.props.value)
        }, name && {
            name: name
        }), null, viewModel.inputRef)), (0, _inferno.createVNode)(1, "div", "dx-checkbox-container", [(0, _inferno.createVNode)(1, "span", "dx-checkbox-icon", null, 1, {
            style: (0, _vdom.normalizeStyles)(viewModel.iconStyles)
        }, null, viewModel.iconRef), text && (0, _inferno.createVNode)(1, "span", "dx-checkbox-text", text, 0)], 0), viewModel.showValidationMessage && (0, _inferno.createComponentVNode)(2, _validation_message.ValidationMessage, {
            validationErrors: viewModel.validationErrors,
            mode: viewModel.props.validationMessageMode,
            positionRequest: "below",
            rtlEnabled: viewModel.props.rtlEnabled,
            target: viewModel.targetCurrent,
            boundary: viewModel.targetCurrent,
            container: viewModel.targetCurrent
        })]
    }), null, viewModel.widgetRef))
};
exports.viewFunction = viewFunction;
var CheckBoxProps = _extends({}, _base_props.BaseWidgetProps, {
    activeStateEnabled: true,
    hoverStateEnabled: true,
    validationError: null,
    validationErrors: null,
    text: "",
    validationMessageMode: "auto",
    validationStatus: "valid",
    name: "",
    readOnly: false,
    isValid: true,
    defaultValue: false,
    valueChange: function() {}
});
exports.CheckBoxProps = CheckBoxProps;
var defaultOptionRules = (0, _utils.createDefaultOptionRules)([{
    device: function() {
        return "desktop" === _devices.default.real().deviceType && !_devices.default.isSimulator()
    },
    options: {
        focusStateEnabled: true
    }
}]);
exports.defaultOptionRules = defaultOptionRules;
var CheckBox = function(_InfernoWrapperCompon) {
    _inheritsLoose(CheckBox, _InfernoWrapperCompon);

    function CheckBox(props) {
        var _this;
        _this = _InfernoWrapperCompon.call(this, props) || this;
        _this.iconRef = (0, _inferno.createRef)();
        _this.inputRef = (0, _inferno.createRef)();
        _this.widgetRef = (0, _inferno.createRef)();
        _this.target = (0, _inferno.createRef)();
        _this.state = {
            showValidationMessage: false,
            value: void 0 !== _this.props.value ? _this.props.value : _this.props.defaultValue
        };
        _this.updateValidationMessageVisibility = _this.updateValidationMessageVisibility.bind(_assertThisInitialized(_this));
        _this.updateIconFontSize = _this.updateIconFontSize.bind(_assertThisInitialized(_this));
        _this.focus = _this.focus.bind(_assertThisInitialized(_this));
        _this.onFocusIn = _this.onFocusIn.bind(_assertThisInitialized(_this));
        _this.onWidgetClick = _this.onWidgetClick.bind(_assertThisInitialized(_this));
        _this.onWidgetKeyDown = _this.onWidgetKeyDown.bind(_assertThisInitialized(_this));
        return _this
    }
    var _proto = CheckBox.prototype;
    _proto.createEffects = function() {
        return [new _vdom.InfernoEffect(this.updateValidationMessageVisibility, [this.props.isValid, this.props.validationStatus, this.props.validationError, this.props.validationErrors]), new _vdom.InfernoEffect(this.updateIconFontSize, [this.props.iconHeight, this.props.iconWidth]), (0, _vdom.createReRenderEffect)()]
    };
    _proto.updateEffects = function() {
        var _this$_effects$, _this$_effects$2;
        null === (_this$_effects$ = this._effects[0]) || void 0 === _this$_effects$ ? void 0 : _this$_effects$.update([this.props.isValid, this.props.validationStatus, this.props.validationError, this.props.validationErrors]);
        null === (_this$_effects$2 = this._effects[1]) || void 0 === _this$_effects$2 ? void 0 : _this$_effects$2.update([this.props.iconHeight, this.props.iconWidth])
    };
    _proto.updateValidationMessageVisibility = function() {
        var _this2 = this;
        this.setState((function(state) {
            return _extends({}, state, {
                showValidationMessage: _this2.shouldShowValidationMessage
            })
        }));
        return
    };
    _proto.updateIconFontSize = function() {
        var _this$iconRef;
        var iconElement = null === (_this$iconRef = this.iconRef) || void 0 === _this$iconRef ? void 0 : _this$iconRef.current;
        var _this$props = this.props,
            iconHeight = _this$props.iconHeight,
            iconWidth = _this$props.iconWidth;
        if (null !== iconElement && void 0 !== iconElement) {
            var width = "number" === typeof iconWidth ? iconWidth : iconElement.offsetWidth;
            var height = "number" === typeof iconHeight ? iconHeight : iconElement.offsetHeight;
            var iconFontSizeRatio = (0, _themes.isMaterial)((0, _themes.current)()) ? 16 / 18 : 16 / 22;
            var calculatedFontSize = "".concat(Math.ceil(Math.min(width, height) * iconFontSizeRatio), "px");
            iconElement.style.fontSize = calculatedFontSize
        }
        return
    };
    _proto.onFocusIn = function(event) {
        var onFocusIn = this.props.onFocusIn;
        null === onFocusIn || void 0 === onFocusIn ? void 0 : onFocusIn(event)
    };
    _proto.onWidgetClick = function(event) {
        var _ref;
        var _this$props2 = this.props,
            readOnly = _this$props2.readOnly,
            saveValueChangeEvent = _this$props2.saveValueChangeEvent;
        var value = null !== (_ref = void 0 !== this.props.value ? this.props.value : this.state.value) && void 0 !== _ref ? _ref : false;
        if (!readOnly) {
            null === saveValueChangeEvent || void 0 === saveValueChangeEvent ? void 0 : saveValueChangeEvent(event);
            var __newValue;
            this.setState((function(state) {
                __newValue = !value;
                return {
                    value: __newValue
                }
            }));
            this.props.valueChange(__newValue)
        }
    };
    _proto.onWidgetKeyDown = function(e) {
        var onKeyDown = this.props.onKeyDown;
        var keyName = e.keyName,
            originalEvent = e.originalEvent,
            which = e.which;
        var result = null === onKeyDown || void 0 === onKeyDown ? void 0 : onKeyDown(e);
        if (null !== result && void 0 !== result && result.cancel) {
            return result
        }
        if ("space" === keyName || "space" === which) {
            originalEvent.preventDefault();
            this.onWidgetClick(originalEvent)
        }
        return
    };
    _proto.focus = function() {
        this.widgetRef.current.focus()
    };
    _proto.render = function() {
        var props = this.props;
        return viewFunction({
            props: _extends({}, props, {
                value: void 0 !== this.props.value ? this.props.value : this.state.value
            }),
            showValidationMessage: this.state.showValidationMessage,
            iconRef: this.iconRef,
            inputRef: this.inputRef,
            target: this.target,
            widgetRef: this.widgetRef,
            onFocusIn: this.onFocusIn,
            onWidgetClick: this.onWidgetClick,
            onWidgetKeyDown: this.onWidgetKeyDown,
            iconStyles: this.iconStyles,
            cssClasses: this.cssClasses,
            shouldShowValidationMessage: this.shouldShowValidationMessage,
            aria: this.aria,
            validationErrors: this.validationErrors,
            targetCurrent: this.targetCurrent,
            restAttributes: this.restAttributes
        })
    };
    _createClass(CheckBox, [{
        key: "iconStyles",
        get: function() {
            var _this$props3 = this.props,
                iconHeight = _this$props3.iconHeight,
                iconWidth = _this$props3.iconWidth;
            var width = (0, _style.normalizeStyleProp)("width", iconWidth);
            var height = (0, _style.normalizeStyleProp)("height", iconHeight);
            return {
                height: height,
                width: width
            }
        }
    }, {
        key: "cssClasses",
        get: function() {
            return getCssClasses(_extends({}, this.props, {
                value: void 0 !== this.props.value ? this.props.value : this.state.value
            }))
        }
    }, {
        key: "shouldShowValidationMessage",
        get: function() {
            var _this$validationError;
            var _this$props4 = this.props,
                isValid = _this$props4.isValid,
                validationStatus = _this$props4.validationStatus;
            var validationErrors = null !== (_this$validationError = this.validationErrors) && void 0 !== _this$validationError ? _this$validationError : [];
            return !isValid && "invalid" === validationStatus && validationErrors.length > 0
        }
    }, {
        key: "aria",
        get: function() {
            var _this$props5 = this.props,
                isValid = _this$props5.isValid,
                readOnly = _this$props5.readOnly;
            var checked = true === (void 0 !== this.props.value ? this.props.value : this.state.value);
            var indeterminate = null === (void 0 !== this.props.value ? this.props.value : this.state.value);
            var result = {
                role: "checkbox",
                checked: indeterminate ? "mixed" : "".concat(checked),
                readonly: readOnly ? "true" : "false",
                invalid: !isValid ? "true" : "false"
            };
            if (this.shouldShowValidationMessage) {
                result.describedby = "dx-".concat(new _guid.default)
            }
            return result
        }
    }, {
        key: "validationErrors",
        get: function() {
            var _this$props6 = this.props,
                validationError = _this$props6.validationError,
                validationErrors = _this$props6.validationErrors;
            var allValidationErrors = validationErrors;
            if (!allValidationErrors && validationError) {
                allValidationErrors = [validationError]
            }
            return allValidationErrors
        }
    }, {
        key: "targetCurrent",
        get: function() {
            var _this$target;
            return null === (_this$target = this.target) || void 0 === _this$target ? void 0 : _this$target.current
        }
    }, {
        key: "restAttributes",
        get: function() {
            var _this$props$value = _extends({}, this.props, {
                    value: void 0 !== this.props.value ? this.props.value : this.state.value
                }),
                restProps = (_this$props$value.accessKey, _this$props$value.activeStateEnabled, _this$props$value.className, _this$props$value.defaultValue, _this$props$value.disabled, _this$props$value.focusStateEnabled, _this$props$value.height, _this$props$value.hint, _this$props$value.hoverStateEnabled, _this$props$value.iconHeight, _this$props$value.iconWidth, _this$props$value.isValid, _this$props$value.name, _this$props$value.onClick, _this$props$value.onFocusIn, _this$props$value.onKeyDown, _this$props$value.readOnly, _this$props$value.rtlEnabled, _this$props$value.saveValueChangeEvent, _this$props$value.tabIndex, _this$props$value.text, _this$props$value.validationError, _this$props$value.validationErrors, _this$props$value.validationMessageMode, _this$props$value.validationStatus, _this$props$value.value, _this$props$value.valueChange, _this$props$value.visible, _this$props$value.width, _objectWithoutProperties(_this$props$value, _excluded));
            return restProps
        }
    }]);
    return CheckBox
}(_vdom.InfernoWrapperComponent);
exports.CheckBox = CheckBox;

function __processTwoWayProps(defaultProps) {
    var twoWayProps = ["value"];
    return Object.keys(defaultProps).reduce((function(props, propName) {
        var propValue = defaultProps[propName];
        var defaultPropName = twoWayProps.some((function(p) {
            return p === propName
        })) ? "default" + propName.charAt(0).toUpperCase() + propName.slice(1) : propName;
        props[defaultPropName] = propValue;
        return props
    }), {})
}

function __createDefaultProps() {
    return _extends({}, CheckBoxProps, __processTwoWayProps((0, _utils.convertRulesToOptions)(defaultOptionRules)))
}
CheckBox.defaultProps = __createDefaultProps();
var __defaultOptionRules = [];

function defaultOptions(rule) {
    __defaultOptionRules.push(rule);
    CheckBox.defaultProps = _extends({}, __createDefaultProps(), __processTwoWayProps((0, _utils.convertRulesToOptions)(__defaultOptionRules)))
}
