/**
 * DevExtreme (cjs/__internal/ui/form/m_form.item_options_actions.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 _element_data = require("../../../core/element_data");
var _extend = require("../../../core/utils/extend");
var _m_form = _interopRequireDefault(require("./m_form.item_option_action"));
var _m_form2 = require("./m_form.utils");

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)
}
let WidgetOptionItemOptionAction = function(_ItemOptionAction) {
    _inheritsLoose(WidgetOptionItemOptionAction, _ItemOptionAction);

    function WidgetOptionItemOptionAction() {
        return _ItemOptionAction.apply(this, arguments) || this
    }
    var _proto = WidgetOptionItemOptionAction.prototype;
    _proto.tryExecute = function() {
        const {
            value: value
        } = this._options;
        const instance = this.findInstance();
        if (instance) {
            instance.option(value);
            return true
        }
        return false
    };
    return WidgetOptionItemOptionAction
}(_m_form.default);
let TabOptionItemOptionAction = function(_ItemOptionAction2) {
    _inheritsLoose(TabOptionItemOptionAction, _ItemOptionAction2);

    function TabOptionItemOptionAction() {
        return _ItemOptionAction2.apply(this, arguments) || this
    }
    var _proto2 = TabOptionItemOptionAction.prototype;
    _proto2.tryExecute = function() {
        const tabPanel = this.findInstance();
        if (tabPanel) {
            const {
                optionName: optionName,
                item: item,
                value: value
            } = this._options;
            const itemIndex = this._itemsRunTimeInfo.findItemIndexByItem(item);
            if (itemIndex >= 0) {
                tabPanel.option((0, _m_form2.getFullOptionName)("items[".concat(itemIndex, "]"), optionName), value);
                return true
            }
        }
        return false
    };
    return TabOptionItemOptionAction
}(_m_form.default);
let SimpleItemTemplateChangedAction = function(_ItemOptionAction3) {
    _inheritsLoose(SimpleItemTemplateChangedAction, _ItemOptionAction3);

    function SimpleItemTemplateChangedAction() {
        return _ItemOptionAction3.apply(this, arguments) || this
    }
    var _proto3 = SimpleItemTemplateChangedAction.prototype;
    _proto3.tryExecute = function() {
        return false
    };
    return SimpleItemTemplateChangedAction
}(_m_form.default);
let GroupItemTemplateChangedAction = function(_ItemOptionAction4) {
    _inheritsLoose(GroupItemTemplateChangedAction, _ItemOptionAction4);

    function GroupItemTemplateChangedAction() {
        return _ItemOptionAction4.apply(this, arguments) || this
    }
    var _proto4 = GroupItemTemplateChangedAction.prototype;
    _proto4.tryExecute = function() {
        const preparedItem = this.findPreparedItem();
        if (null != preparedItem && preparedItem._prepareGroupItemTemplate && preparedItem._renderGroupContentTemplate) {
            preparedItem._prepareGroupItemTemplate(this._options.item.template);
            preparedItem._renderGroupContentTemplate();
            return true
        }
        return false
    };
    return GroupItemTemplateChangedAction
}(_m_form.default);
let TabsOptionItemOptionAction = function(_ItemOptionAction5) {
    _inheritsLoose(TabsOptionItemOptionAction, _ItemOptionAction5);

    function TabsOptionItemOptionAction() {
        return _ItemOptionAction5.apply(this, arguments) || this
    }
    var _proto5 = TabsOptionItemOptionAction.prototype;
    _proto5.tryExecute = function() {
        const tabPanel = this.findInstance();
        if (tabPanel) {
            const {
                value: value
            } = this._options;
            tabPanel.option("dataSource", value);
            return true
        }
        return false
    };
    return TabsOptionItemOptionAction
}(_m_form.default);
let ValidationRulesItemOptionAction = function(_ItemOptionAction6) {
    _inheritsLoose(ValidationRulesItemOptionAction, _ItemOptionAction6);

    function ValidationRulesItemOptionAction() {
        return _ItemOptionAction6.apply(this, arguments) || this
    }
    var _proto6 = ValidationRulesItemOptionAction.prototype;
    _proto6.tryExecute = function() {
        const {
            item: item
        } = this._options;
        const instance = this.findInstance();
        const validator = instance && (0, _element_data.data)(instance.$element()[0], "dxValidator");
        if (validator && item) {
            const filterRequired = item => "required" === item.type;
            const oldContainsRequired = (validator.option("validationRules") || []).some(filterRequired);
            const newContainsRequired = (item.validationRules || []).some(filterRequired);
            if (!oldContainsRequired && !newContainsRequired || oldContainsRequired && newContainsRequired) {
                validator.option("validationRules", item.validationRules);
                return true
            }
        }
        return false
    };
    return ValidationRulesItemOptionAction
}(_m_form.default);
let CssClassItemOptionAction = function(_ItemOptionAction7) {
    _inheritsLoose(CssClassItemOptionAction, _ItemOptionAction7);

    function CssClassItemOptionAction() {
        return _ItemOptionAction7.apply(this, arguments) || this
    }
    var _proto7 = CssClassItemOptionAction.prototype;
    _proto7.tryExecute = function() {
        const $itemContainer = this.findItemContainer();
        const {
            previousValue: previousValue,
            value: value
        } = this._options;
        if ($itemContainer) {
            $itemContainer.removeClass(previousValue).addClass(value);
            return true
        }
        return false
    };
    return CssClassItemOptionAction
}(_m_form.default);
const tryCreateItemOptionAction = (optionName, itemActionOptions) => {
    var _a, _b, _c;
    switch (optionName) {
        case "editorOptions":
        case "buttonOptions":
            return new WidgetOptionItemOptionAction(itemActionOptions);
        case "validationRules":
            return new ValidationRulesItemOptionAction(itemActionOptions);
        case "cssClass":
            return new CssClassItemOptionAction(itemActionOptions);
        case "badge":
        case "disabled":
        case "icon":
        case "tabTemplate":
        case "title":
            return new TabOptionItemOptionAction((0, _extend.extend)(itemActionOptions, {
                optionName: optionName
            }));
        case "tabs":
            return new TabsOptionItemOptionAction(itemActionOptions);
        case "template": {
            const itemType = null !== (_b = null === (_a = null === itemActionOptions || void 0 === itemActionOptions ? void 0 : itemActionOptions.item) || void 0 === _a ? void 0 : _a.itemType) && void 0 !== _b ? _b : null === (_c = itemActionOptions.itemsRunTimeInfo.findPreparedItemByItem(null === itemActionOptions || void 0 === itemActionOptions ? void 0 : itemActionOptions.item)) || void 0 === _c ? void 0 : _c.itemType;
            if ("simple" === itemType) {
                return new SimpleItemTemplateChangedAction(itemActionOptions)
            }
            if ("group" === itemType) {
                return new GroupItemTemplateChangedAction(itemActionOptions)
            }
            return new TabOptionItemOptionAction((0, _extend.extend)(itemActionOptions, {
                optionName: optionName
            }))
        }
        default:
            return null
    }
};
var _default = tryCreateItemOptionAction;
exports.default = _default;
