/**
 * DevExtreme (cjs/__internal/ui/radio_group/m_radio_group.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = void 0;
var _component_registrator = _interopRequireDefault(require("../../../core/component_registrator"));
var _devices = _interopRequireDefault(require("../../../core/devices"));
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _deferred = require("../../../core/utils/deferred");
var _extend = require("../../../core/utils/extend");
var _type = require("../../../core/utils/type");
var _ui = _interopRequireDefault(require("../../../ui/editor/ui.data_expression"));
var _editor = _interopRequireDefault(require("../editor"));
var _m_radio_collection = _interopRequireDefault(require("./m_radio_collection"));

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

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

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const RADIO_BUTTON_CLASS = "dx-radiobutton";
const RADIO_GROUP_HORIZONTAL_CLASS = "dx-radiogroup-horizontal";
const RADIO_GROUP_VERTICAL_CLASS = "dx-radiogroup-vertical";
const RADIO_GROUP_CLASS = "dx-radiogroup";
const RADIO_FEEDBACK_HIDE_TIMEOUT = 100;
let RadioGroup = function(_Editor) {
    _inheritsLoose(RadioGroup, _Editor);

    function RadioGroup() {
        return _Editor.apply(this, arguments) || this
    }
    var _proto = RadioGroup.prototype;
    _proto._dataSourceOptions = function() {
        return {
            paginate: false
        }
    };
    _proto._defaultOptionsRules = function() {
        const defaultOptionsRules = _Editor.prototype._defaultOptionsRules.call(this);
        return defaultOptionsRules.concat([{
            device: {
                tablet: true
            },
            options: {
                layout: "horizontal"
            }
        }, {
            device: () => "desktop" === _devices.default.real().deviceType && !_devices.default.isSimulator(),
            options: {
                focusStateEnabled: true
            }
        }])
    };
    _proto._fireContentReadyAction = function(force) {
        force && _Editor.prototype._fireContentReadyAction.call(this)
    };
    _proto._focusTarget = function() {
        return this.$element()
    };
    _proto._getAriaTarget = function() {
        return this.$element()
    };
    _proto._getDefaultOptions = function() {
        const defaultOptions = _Editor.prototype._getDefaultOptions.call(this);
        return (0, _extend.extend)(defaultOptions, (0, _extend.extend)(_ui.default._dataExpressionDefaultOptions(), {
            hoverStateEnabled: true,
            activeStateEnabled: true,
            layout: "vertical"
        }))
    };
    _proto._getItemValue = function(item) {
        return this._valueGetter ? this._valueGetter(item) : item.text
    };
    _proto._getSubmitElement = function() {
        return this._$submitElement
    };
    _proto._init = function() {
        _Editor.prototype._init.call(this);
        this._activeStateUnit = ".".concat("dx-radiobutton");
        this._feedbackHideTimeout = 100;
        this._initDataExpressions()
    };
    _proto._initMarkup = function() {
        (0, _renderer.default)(this.element()).addClass("dx-radiogroup");
        this._renderSubmitElement();
        this.setAria("role", "radiogroup");
        this._renderRadios();
        this._renderLayout();
        _Editor.prototype._initMarkup.call(this)
    };
    _proto._itemClickHandler = function(_ref) {
        let {
            itemElement: itemElement,
            event: event,
            itemData: itemData
        } = _ref;
        if (this.itemElements().is(itemElement)) {
            const newValue = this._getItemValue(itemData);
            if (newValue !== this.option("value")) {
                this._saveValueChangeEvent(event);
                this.option("value", newValue)
            }
        }
    };
    _proto._getSelectedItemKeys = function() {
        let value = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this.option("value");
        const isNullSelectable = "this" !== this.option("valueExpr");
        const shouldSelectValue = isNullSelectable && null === value || (0, _type.isDefined)(value);
        return shouldSelectValue ? [value] : []
    };
    _proto._setSelection = function(currentValue) {
        const value = this._unwrappedValue(currentValue);
        this._setCollectionWidgetOption("selectedItemKeys", this._getSelectedItemKeys(value))
    };
    _proto._renderValidationState = function() {
        var _a;
        _Editor.prototype._renderValidationState.call(this);
        null === (_a = this._validationMessage) || void 0 === _a ? void 0 : _a.$content().attr("role", "alert")
    };
    _proto._optionChanged = function(args) {
        const {
            name: name,
            value: value
        } = args;
        this._dataExpressionOptionChanged(args);
        switch (name) {
            case "dataSource":
                this._invalidate();
                break;
            case "focusStateEnabled":
            case "accessKey":
            case "tabIndex":
                this._setCollectionWidgetOption(name, value);
                break;
            case "disabled":
                _Editor.prototype._optionChanged.call(this, args);
                this._setCollectionWidgetOption(name, value);
                break;
            case "valueExpr":
                this._setCollectionWidgetOption("keyExpr", this._getCollectionKeyExpr());
                break;
            case "value":
                this._setSelection(value);
                this._setSubmitValue(value);
                _Editor.prototype._optionChanged.call(this, args);
                break;
            case "items":
                this._setSelection(this.option("value"));
                break;
            case "itemTemplate":
            case "displayExpr":
                break;
            case "layout":
                this._renderLayout();
                this._updateItemsSize();
                break;
            default:
                _Editor.prototype._optionChanged.call(this, args)
        }
    };
    _proto._render = function() {
        _Editor.prototype._render.call(this);
        this._updateItemsSize()
    };
    _proto._renderLayout = function() {
        const layout = this.option("layout");
        const $element = (0, _renderer.default)(this.element());
        $element.toggleClass("dx-radiogroup-vertical", "vertical" === layout);
        $element.toggleClass("dx-radiogroup-horizontal", "horizontal" === layout)
    };
    _proto._renderRadios = function() {
        this._areRadiosCreated = (0, _deferred.Deferred)();
        const $radios = (0, _renderer.default)("<div>").appendTo(this.$element());
        const {
            displayExpr: displayExpr,
            accessKey: accessKey,
            focusStateEnabled: focusStateEnabled,
            itemTemplate: itemTemplate,
            tabIndex: tabIndex
        } = this.option();
        this._createComponent($radios, _m_radio_collection.default, {
            onInitialized: _ref2 => {
                let {
                    component: component
                } = _ref2;
                this._radios = component
            },
            onContentReady: e => {
                this._fireContentReadyAction(true)
            },
            onItemClick: this._itemClickHandler.bind(this),
            displayExpr: displayExpr,
            accessKey: accessKey,
            dataSource: this._dataSource,
            focusStateEnabled: focusStateEnabled,
            itemTemplate: itemTemplate,
            keyExpr: this._getCollectionKeyExpr(),
            noDataText: "",
            scrollingEnabled: false,
            selectByClick: false,
            selectionMode: "single",
            selectedItemKeys: this._getSelectedItemKeys(),
            tabIndex: tabIndex
        });
        this._areRadiosCreated.resolve()
    };
    _proto._renderSubmitElement = function() {
        this._$submitElement = (0, _renderer.default)("<input>").attr("type", "hidden").appendTo(this.$element());
        this._setSubmitValue()
    };
    _proto._setOptionsByReference = function() {
        _Editor.prototype._setOptionsByReference.call(this);
        (0, _extend.extend)(this._optionsByReference, {
            value: true
        })
    };
    _proto._setSubmitValue = function(value) {
        value = null !== value && void 0 !== value ? value : this.option("value");
        const submitValue = "this" === this.option("valueExpr") ? this._displayGetter(value) : value;
        this._$submitElement.val(submitValue)
    };
    _proto._setCollectionWidgetOption = function(name, value) {
        this._areRadiosCreated.done(this._setWidgetOption.bind(this, "_radios", arguments))
    };
    _proto._updateItemsSize = function() {
        var _a, _b;
        if ("horizontal" === this.option("layout")) {
            null === (_a = this.itemElements()) || void 0 === _a ? void 0 : _a.css("height", "auto")
        } else {
            const itemsCount = this.option("items").length;
            null === (_b = this.itemElements()) || void 0 === _b ? void 0 : _b.css("height", "".concat(100 / itemsCount, "%"))
        }
    };
    _proto.focus = function() {
        var _a;
        null === (_a = this._radios) || void 0 === _a ? void 0 : _a.focus()
    };
    _proto.itemElements = function() {
        var _a;
        return null === (_a = this._radios) || void 0 === _a ? void 0 : _a._itemElements()
    };
    return RadioGroup
}(_editor.default);
RadioGroup.include(_ui.default);
(0, _component_registrator.default)("dxRadioGroup", RadioGroup);
var _default = RadioGroup;
exports.default = _default;
