/**
 * DevExtreme (esm/__internal/ui/form/m_form.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/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import "./m_form.layout_manager";
import "../../../ui/validation_summary";
import "../../../ui/validation_group";
import registerComponent from "../../../core/component_registrator";
import config from "../../../core/config";
import {
    getPublicElement
} from "../../../core/element";
import Guid from "../../../core/guid";
import $ from "../../../core/renderer";
import resizeObserverSingleton from "../../../core/resize_observer";
import {
    ensureDefined
} from "../../../core/utils/common";
import {
    Deferred
} from "../../../core/utils/deferred";
import {
    extend
} from "../../../core/utils/extend";
import {
    each
} from "../../../core/utils/iterator";
import {
    isDefined,
    isEmptyObject,
    isObject,
    isString
} from "../../../core/utils/type";
import {
    defaultScreenFactorFunc,
    getCurrentScreenFactor,
    hasWindow
} from "../../../core/utils/window";
import eventsEngine from "../../../events/core/events_engine";
import {
    triggerResizeEvent,
    triggerShownEvent
} from "../../../events/visibility_change";
import messageLocalization from "../../../localization/message";
import Editor from "../../../ui/editor/editor";
import Scrollable from "../../../ui/scroll_view/ui.scrollable";
import TabPanel from "../../../ui/tab_panel";
import {
    isMaterial,
    isMaterialBased
} from "../../../ui/themes";
import ValidationEngine from "../../../ui/validation_engine";
import Widget from "../../../ui/widget/ui.widget";
import {
    TOOLBAR_CLASS
} from "../../ui/toolbar/m_constants";
import {
    setLabelWidthByMaxLabelWidth
} from "./components/m_label";
import {
    FIELD_ITEM_CLASS,
    FIELD_ITEM_CONTENT_CLASS,
    FIELD_ITEM_CONTENT_HAS_GROUP_CLASS,
    FIELD_ITEM_CONTENT_HAS_TABS_CLASS,
    FIELD_ITEM_TAB_CLASS,
    FORM_CLASS,
    FORM_FIELD_ITEM_COL_CLASS,
    FORM_GROUP_CAPTION_CLASS,
    FORM_GROUP_CLASS,
    FORM_GROUP_CONTENT_CLASS,
    FORM_GROUP_WITH_CAPTION_CLASS,
    FORM_UNDERLINED_CLASS,
    FORM_VALIDATION_SUMMARY,
    GROUP_COL_COUNT_ATTR,
    GROUP_COL_COUNT_CLASS,
    ROOT_SIMPLE_ITEM_CLASS
} from "./constants";
import tryCreateItemOptionAction from "./m_form.item_options_actions";
import {
    default as FormItemsRunTimeInfo
} from "./m_form.items_runtime_info";
import {
    convertToLabelMarkOptions
} from "./m_form.layout_manager.utils";
import {
    concatPaths,
    convertToLayoutManagerOptions,
    createItemPathByIndex,
    getFullOptionName,
    getItemPath,
    getOptionNameFromFullName,
    getTextWithoutSpaces,
    isEqualToDataFieldOrNameOrTitleOrCaption,
    isFullPathContainsTabs,
    tryGetTabPath
} from "./m_form.utils";
var FOCUSED_STATE_CLASS = "dx-state-focused";
var ITEM_OPTIONS_FOR_VALIDATION_UPDATING = ["items", "isRequired", "validationRules", "visible"];
var Form = Widget.inherit({
    _init() {
        this.callBase();
        this._dirtyFields = new Set;
        this._cachedColCountOptions = [];
        this._itemsRunTimeInfo = new FormItemsRunTimeInfo;
        this._groupsColCount = [];
        this._attachSyncSubscriptions()
    },
    _getDefaultOptions() {
        return extend(this.callBase(), {
            formID: "dx-".concat(new Guid),
            formData: {},
            colCount: 1,
            screenByWidth: defaultScreenFactorFunc,
            colCountByScreen: void 0,
            labelLocation: "left",
            readOnly: false,
            onFieldDataChanged: null,
            customizeItem: null,
            onEditorEnterKey: null,
            minColWidth: 200,
            alignItemLabels: true,
            alignItemLabelsInAllGroups: true,
            alignRootItemLabels: true,
            showColonAfterLabel: true,
            showRequiredMark: true,
            showOptionalMark: false,
            requiredMark: "*",
            optionalMark: messageLocalization.format("dxForm-optionalMark"),
            requiredMessage: messageLocalization.getFormatter("dxForm-requiredMessage"),
            showValidationSummary: false,
            items: void 0,
            scrollingEnabled: false,
            validationGroup: void 0,
            stylingMode: config().editorStylingMode,
            labelMode: "outside",
            isDirty: false
        })
    },
    _defaultOptionsRules() {
        return this.callBase().concat([{
            device: () => isMaterialBased(),
            options: {
                labelLocation: "top"
            }
        }, {
            device: () => isMaterial(),
            options: {
                showColonAfterLabel: false
            }
        }])
    },
    _setOptionsByReference() {
        this.callBase();
        extend(this._optionsByReference, {
            formData: true,
            validationGroup: true
        })
    },
    _getGroupColCount: $element => parseInt($element.attr(GROUP_COL_COUNT_ATTR)),
    _applyLabelsWidthByCol($container, index) {
        var options = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {};
        var labelMarkOptions = arguments.length > 3 ? arguments[3] : void 0;
        var fieldItemClass = options.inOneColumn ? FIELD_ITEM_CLASS : FORM_FIELD_ITEM_COL_CLASS + index;
        var cssExcludeTabbedSelector = options.excludeTabbed ? ":not(.".concat(FIELD_ITEM_TAB_CLASS, ")") : "";
        setLabelWidthByMaxLabelWidth($container, ".".concat(fieldItemClass).concat(cssExcludeTabbedSelector), labelMarkOptions)
    },
    _applyLabelsWidth($container, excludeTabbed, inOneColumn, colCount, labelMarkOptions) {
        colCount = inOneColumn ? 1 : colCount || this._getGroupColCount($container);
        var applyLabelsOptions = {
            excludeTabbed: excludeTabbed,
            inOneColumn: inOneColumn
        };
        var i;
        for (i = 0; i < colCount; i++) {
            this._applyLabelsWidthByCol($container, i, applyLabelsOptions, labelMarkOptions)
        }
    },
    _getGroupElementsInColumn($container, columnIndex, colCount) {
        var cssColCountSelector = isDefined(colCount) ? ".".concat(GROUP_COL_COUNT_CLASS).concat(colCount) : "";
        var groupSelector = ".".concat(FORM_FIELD_ITEM_COL_CLASS).concat(columnIndex, " > .").concat(FIELD_ITEM_CONTENT_CLASS, " > .").concat(FORM_GROUP_CLASS).concat(cssColCountSelector);
        return $container.find(groupSelector)
    },
    _applyLabelsWidthWithGroups($container, colCount, excludeTabbed, labelMarkOptions) {
        if (true === this.option("alignRootItemLabels")) {
            var $rootSimpleItems = $container.find(".".concat(ROOT_SIMPLE_ITEM_CLASS));
            for (var colIndex = 0; colIndex < colCount; colIndex++) {
                this._applyLabelsWidthByCol($rootSimpleItems, colIndex, excludeTabbed, labelMarkOptions)
            }
        }
        var alignItemLabelsInAllGroups = this.option("alignItemLabelsInAllGroups");
        if (alignItemLabelsInAllGroups) {
            this._applyLabelsWidthWithNestedGroups($container, colCount, excludeTabbed, labelMarkOptions)
        } else {
            var $groups = this.$element().find(".".concat(FORM_GROUP_CLASS));
            var i;
            for (i = 0; i < $groups.length; i++) {
                this._applyLabelsWidth($groups.eq(i), excludeTabbed, void 0, void 0, labelMarkOptions)
            }
        }
    },
    _applyLabelsWidthWithNestedGroups($container, colCount, excludeTabbed, labelMarkOptions) {
        var applyLabelsOptions = {
            excludeTabbed: excludeTabbed
        };
        var colIndex;
        var groupsColIndex;
        var groupColIndex;
        var $groupsByCol;
        for (colIndex = 0; colIndex < colCount; colIndex++) {
            $groupsByCol = this._getGroupElementsInColumn($container, colIndex);
            this._applyLabelsWidthByCol($groupsByCol, 0, applyLabelsOptions, labelMarkOptions);
            for (groupsColIndex = 0; groupsColIndex < this._groupsColCount.length; groupsColIndex++) {
                $groupsByCol = this._getGroupElementsInColumn($container, colIndex, this._groupsColCount[groupsColIndex]);
                var groupColCount = this._getGroupColCount($groupsByCol);
                for (groupColIndex = 1; groupColIndex < groupColCount; groupColIndex++) {
                    this._applyLabelsWidthByCol($groupsByCol, groupColIndex, applyLabelsOptions, labelMarkOptions)
                }
            }
        }
    },
    _labelLocation() {
        return this.option("labelLocation")
    },
    _alignLabelsInColumn(_ref) {
        var {
            layoutManager: layoutManager,
            inOneColumn: inOneColumn,
            $container: $container,
            excludeTabbed: excludeTabbed,
            items: items
        } = _ref;
        if (!hasWindow() || "top" === this._labelLocation()) {
            return
        }
        var labelMarkOptions = convertToLabelMarkOptions(layoutManager._getMarkOptions());
        if (inOneColumn) {
            this._applyLabelsWidth($container, excludeTabbed, true, void 0, labelMarkOptions)
        } else if (this._checkGrouping(items)) {
            this._applyLabelsWidthWithGroups($container, layoutManager._getColCount(), excludeTabbed, labelMarkOptions)
        } else {
            this._applyLabelsWidth($container, excludeTabbed, false, layoutManager._getColCount(), labelMarkOptions)
        }
    },
    _prepareFormData() {
        if (!isDefined(this.option("formData"))) {
            this.option("formData", {})
        }
    },
    _setStylingModeClass() {
        if ("underlined" === this.option("stylingMode")) {
            this.$element().addClass(FORM_UNDERLINED_CLASS)
        }
    },
    _initMarkup() {
        ValidationEngine.addGroup(this._getValidationGroup(), false);
        this._clearCachedInstances();
        this._prepareFormData();
        this.$element().addClass(FORM_CLASS);
        this._setStylingModeClass();
        this.callBase();
        this.setAria("role", "form", this.$element());
        if (this.option("scrollingEnabled")) {
            this._renderScrollable()
        }
        this._renderLayout();
        this._renderValidationSummary();
        this._lastMarkupScreenFactor = this._targetScreenFactor || this._getCurrentScreenFactor();
        this._attachResizeObserverSubscription()
    },
    _attachResizeObserverSubscription() {
        if (hasWindow()) {
            var formRootElement = this.$element().get(0);
            resizeObserverSingleton.unobserve(formRootElement);
            resizeObserverSingleton.observe(formRootElement, () => {
                this._resizeHandler()
            })
        }
    },
    _resizeHandler() {
        if (this._cachedLayoutManagers.length) {
            each(this._cachedLayoutManagers, (_, layoutManager) => {
                var _a;
                null === (_a = layoutManager.option("onLayoutChanged")) || void 0 === _a ? void 0 : _a(layoutManager.isSingleColumnMode())
            })
        }
    },
    _getCurrentScreenFactor() {
        return hasWindow() ? getCurrentScreenFactor(this.option("screenByWidth")) : "lg"
    },
    _clearCachedInstances() {
        this._itemsRunTimeInfo.clear();
        this._cachedLayoutManagers = []
    },
    _alignLabels(layoutManager, inOneColumn) {
        this._alignLabelsInColumn({
            $container: this.$element(),
            layoutManager: layoutManager,
            excludeTabbed: true,
            items: this.option("items"),
            inOneColumn: inOneColumn
        });
        triggerResizeEvent(this.$element().find(".".concat(TOOLBAR_CLASS)))
    },
    _clean() {
        this._clearValidationSummary();
        this.callBase();
        this._groupsColCount = [];
        this._cachedColCountOptions = [];
        this._lastMarkupScreenFactor = void 0;
        resizeObserverSingleton.unobserve(this.$element().get(0))
    },
    _renderScrollable() {
        var useNativeScrolling = this.option("useNativeScrolling");
        this._scrollable = new Scrollable(this.$element(), {
            useNative: !!useNativeScrolling,
            useSimulatedScrollbar: !useNativeScrolling,
            useKeyboard: false,
            direction: "both",
            bounceEnabled: false
        })
    },
    _getContent() {
        return this.option("scrollingEnabled") ? $(this._scrollable.content()) : this.$element()
    },
    _clearValidationSummary() {
        var _a;
        null === (_a = this._$validationSummary) || void 0 === _a ? void 0 : _a.remove();
        this._$validationSummary = void 0;
        this._validationSummary = void 0
    },
    _renderValidationSummary() {
        this._clearValidationSummary();
        if (this.option("showValidationSummary")) {
            this._$validationSummary = $("<div>").addClass(FORM_VALIDATION_SUMMARY).appendTo(this._getContent());
            this._validationSummary = this._$validationSummary.dxValidationSummary({
                validationGroup: this._getValidationGroup()
            }).dxValidationSummary("instance")
        }
    },
    _prepareItems(items, parentIsTabbedItem, currentPath, isTabs) {
        if (items) {
            var result = [];
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                var path = concatPaths(currentPath, createItemPathByIndex(i, isTabs));
                var itemRunTimeInfo = {
                    item: item,
                    itemIndex: i,
                    path: path
                };
                var guid = this._itemsRunTimeInfo.add(itemRunTimeInfo);
                if (isString(item)) {
                    item = {
                        dataField: item
                    }
                }
                if (isObject(item)) {
                    var preparedItem = _extends({}, item);
                    itemRunTimeInfo.preparedItem = preparedItem;
                    preparedItem.guid = guid;
                    this._tryPrepareGroupItem(preparedItem);
                    this._tryPrepareTabbedItem(preparedItem, path);
                    this._tryPrepareItemTemplate(preparedItem);
                    if (parentIsTabbedItem) {
                        preparedItem.cssItemClass = FIELD_ITEM_TAB_CLASS
                    }
                    if (preparedItem.items) {
                        preparedItem.items = this._prepareItems(preparedItem.items, parentIsTabbedItem, path)
                    }
                    result.push(preparedItem)
                } else {
                    result.push(item)
                }
            }
            return result
        }
    },
    _tryPrepareGroupItem(item) {
        if ("group" === item.itemType) {
            item.alignItemLabels = ensureDefined(item.alignItemLabels, true);
            item._prepareGroupItemTemplate = itemTemplate => {
                if (item.template) {
                    item.groupContentTemplate = this._getTemplate(itemTemplate)
                }
                item.template = this._itemGroupTemplate.bind(this, item)
            };
            item._prepareGroupItemTemplate(item.template)
        }
    },
    _tryPrepareTabbedItem(item, path) {
        if ("tabbed" === item.itemType) {
            item.template = this._itemTabbedTemplate.bind(this, item);
            item.tabs = this._prepareItems(item.tabs, true, path, true)
        }
    },
    _tryPrepareItemTemplate(item) {
        if (item.template) {
            item.template = this._getTemplate(item.template)
        }
    },
    _checkGrouping(items) {
        if (items) {
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                if ("group" === item.itemType) {
                    return true
                }
            }
        }
    },
    _renderLayout() {
        var that = this;
        var items = that.option("items");
        var $content = that._getContent();
        items = that._prepareItems(items);
        that._rootLayoutManager = that._renderLayoutManager($content, this._createLayoutManagerOptions(items, {
            isRoot: true,
            colCount: that.option("colCount"),
            alignItemLabels: that.option("alignItemLabels"),
            screenByWidth: this.option("screenByWidth"),
            colCountByScreen: this.option("colCountByScreen"),
            onLayoutChanged(inOneColumn) {
                that._alignLabels.bind(that)(that._rootLayoutManager, inOneColumn)
            },
            onContentReady(e) {
                that._alignLabels(e.component, e.component.isSingleColumnMode())
            }
        }))
    },
    _tryGetItemsForTemplate: item => item.items || [],
    _itemTabbedTemplate(item, e, $container) {
        var _a;
        var $tabPanel = $("<div>").appendTo($container);
        var tabPanelOptions = extend({}, item.tabPanelOptions, {
            dataSource: item.tabs,
            onItemRendered: args => {
                var _a, _b;
                null === (_b = null === (_a = item.tabPanelOptions) || void 0 === _a ? void 0 : _a.onItemRendered) || void 0 === _b ? void 0 : _b.call(_a, args);
                triggerShownEvent(args.itemElement)
            },
            itemTemplate: (itemData, e, container) => {
                var $container = $(container);
                var alignItemLabels = ensureDefined(itemData.alignItemLabels, true);
                var layoutManager = this._renderLayoutManager($container, this._createLayoutManagerOptions(this._tryGetItemsForTemplate(itemData), {
                    colCount: itemData.colCount,
                    alignItemLabels: alignItemLabels,
                    screenByWidth: this.option("screenByWidth"),
                    colCountByScreen: itemData.colCountByScreen,
                    cssItemClass: itemData.cssItemClass,
                    onLayoutChanged: inOneColumn => {
                        this._alignLabelsInColumn({
                            $container: $container,
                            layoutManager: layoutManager,
                            items: itemData.items,
                            inOneColumn: inOneColumn
                        })
                    }
                }));
                if (this._itemsRunTimeInfo) {
                    this._itemsRunTimeInfo.extendRunTimeItemInfoByKey(itemData.guid, {
                        layoutManager: layoutManager
                    })
                }
                if (alignItemLabels) {
                    this._alignLabelsInColumn({
                        $container: $container,
                        layoutManager: layoutManager,
                        items: itemData.items,
                        inOneColumn: layoutManager.isSingleColumnMode()
                    })
                }
            }
        });
        var tryUpdateTabPanelInstance = (items, instance) => {
            if (Array.isArray(items)) {
                items.forEach(item => this._itemsRunTimeInfo.extendRunTimeItemInfoByKey(item.guid, {
                    widgetInstance: instance
                }))
            }
        };
        var tabPanel = this._createComponent($tabPanel, TabPanel, tabPanelOptions);
        $($container).parent().addClass(FIELD_ITEM_CONTENT_HAS_TABS_CLASS);
        tabPanel.on("optionChanged", e => {
            if ("dataSource" === e.fullName) {
                tryUpdateTabPanelInstance(e.value, e.component)
            }
        });
        tryUpdateTabPanelInstance([{
            guid: item.guid
        }, ...null !== (_a = item.tabs) && void 0 !== _a ? _a : []], tabPanel)
    },
    _itemGroupTemplate(item, options, $container) {
        var {
            id: id
        } = options.editorOptions.inputAttr;
        var $group = $("<div>").toggleClass(FORM_GROUP_WITH_CAPTION_CLASS, isDefined(item.caption) && item.caption.length).addClass(FORM_GROUP_CLASS).appendTo($container);
        var groupAria = {
            role: "group",
            labelledby: id
        };
        this.setAria(groupAria, $group);
        $($container).parent().addClass(FIELD_ITEM_CONTENT_HAS_GROUP_CLASS);
        if (item.caption) {
            $("<span>").addClass(FORM_GROUP_CAPTION_CLASS).text(item.caption).attr("id", id).appendTo($group)
        }
        var $groupContent = $("<div>").addClass(FORM_GROUP_CONTENT_CLASS).appendTo($group);
        if (item.groupContentTemplate) {
            item._renderGroupContentTemplate = () => {
                $groupContent.empty();
                var data = {
                    formData: this.option("formData"),
                    component: this
                };
                item.groupContentTemplate.render({
                    model: data,
                    container: getPublicElement($groupContent)
                })
            };
            item._renderGroupContentTemplate()
        } else {
            var layoutManager = this._renderLayoutManager($groupContent, this._createLayoutManagerOptions(this._tryGetItemsForTemplate(item), {
                colCount: item.colCount,
                colCountByScreen: item.colCountByScreen,
                alignItemLabels: item.alignItemLabels,
                cssItemClass: item.cssItemClass
            }));
            this._itemsRunTimeInfo && this._itemsRunTimeInfo.extendRunTimeItemInfoByKey(item.guid, {
                layoutManager: layoutManager
            });
            var colCount = layoutManager._getColCount();
            if (!this._groupsColCount.includes(colCount)) {
                this._groupsColCount.push(colCount)
            }
            $group.addClass(GROUP_COL_COUNT_CLASS + colCount);
            $group.attr(GROUP_COL_COUNT_ATTR, colCount)
        }
    },
    _createLayoutManagerOptions(items, extendedLayoutManagerOptions) {
        return convertToLayoutManagerOptions({
            form: this,
            formOptions: this.option(),
            $formElement: this.$element(),
            items: items,
            validationGroup: this._getValidationGroup(),
            extendedLayoutManagerOptions: extendedLayoutManagerOptions,
            onFieldDataChanged: args => {
                if (!this._isDataUpdating) {
                    this._triggerOnFieldDataChanged(args)
                }
            },
            onContentReady: args => {
                this._itemsRunTimeInfo.addItemsOrExtendFrom(args.component._itemsRunTimeInfo);
                extendedLayoutManagerOptions.onContentReady && extendedLayoutManagerOptions.onContentReady(args)
            },
            onDisposing: _ref2 => {
                var {
                    component: component
                } = _ref2;
                var nestedItemsRunTimeInfo = component.getItemsRunTimeInfo();
                this._itemsRunTimeInfo.removeItemsByItems(nestedItemsRunTimeInfo)
            },
            onFieldItemRendered: () => {
                var _a;
                null === (_a = this._validationSummary) || void 0 === _a ? void 0 : _a.refreshValidationGroup()
            }
        })
    },
    _renderLayoutManager($parent, layoutManagerOptions) {
        var baseColCountByScreen = {
            lg: layoutManagerOptions.colCount,
            md: layoutManagerOptions.colCount,
            sm: layoutManagerOptions.colCount,
            xs: 1
        };
        this._cachedColCountOptions.push({
            colCountByScreen: extend(baseColCountByScreen, layoutManagerOptions.colCountByScreen)
        });
        var $element = $("<div>");
        $element.appendTo($parent);
        var instance = this._createComponent($element, "dxLayoutManager", layoutManagerOptions);
        instance.on("autoColCountChanged", () => {
            this._clearAutoColCountChangedTimeout();
            this.autoColCountChangedTimeoutId = setTimeout(() => !this._disposed && this._refresh(), 0)
        });
        this._cachedLayoutManagers.push(instance);
        return instance
    },
    _getValidationGroup() {
        return this.option("validationGroup") || this
    },
    _createComponent($element, type, config) {
        config = config || {};
        this._extendConfig(config, {
            readOnly: this.option("readOnly")
        });
        return this.callBase($element, type, config)
    },
    _attachSyncSubscriptions() {
        var that = this;
        that.on("optionChanged", args => {
            var optionFullName = args.fullName;
            if ("formData" === optionFullName) {
                if (!isDefined(args.value)) {
                    that._options.silent("formData", args.value = {})
                }
                that._triggerOnFieldDataChangedByDataSet(args.value)
            }
            if (that._cachedLayoutManagers.length) {
                each(that._cachedLayoutManagers, (index, layoutManager) => {
                    if ("formData" === optionFullName) {
                        that._isDataUpdating = true;
                        layoutManager.option("layoutData", args.value);
                        that._isDataUpdating = false
                    }
                    if ("readOnly" === args.name || "disabled" === args.name) {
                        layoutManager.option(optionFullName, args.value)
                    }
                })
            }
        })
    },
    _optionChanged(args) {
        var splitFullName = args.fullName.split(".");
        if (splitFullName.length > 1 && -1 !== splitFullName[0].search("items") && this._itemsOptionChangedHandler(args)) {
            return
        }
        if (splitFullName.length > 1 && -1 !== splitFullName[0].search("formData") && this._formDataOptionChangedHandler(args)) {
            return
        }
        this._defaultOptionChangedHandler(args)
    },
    _defaultOptionChangedHandler(args) {
        switch (args.name) {
            case "formData":
                if (!this.option("items")) {
                    this._invalidate()
                } else if (isEmptyObject(args.value)) {
                    this._clear()
                }
                break;
            case "onFieldDataChanged":
                break;
            case "items":
            case "colCount":
            case "onEditorEnterKey":
            case "labelLocation":
            case "labelMode":
            case "alignItemLabels":
            case "showColonAfterLabel":
            case "customizeItem":
            case "alignItemLabelsInAllGroups":
            case "showRequiredMark":
            case "showOptionalMark":
            case "requiredMark":
            case "optionalMark":
            case "requiredMessage":
            case "scrollingEnabled":
            case "formID":
            case "colCountByScreen":
            case "screenByWidth":
            case "stylingMode":
                this._invalidate();
                break;
            case "showValidationSummary":
                this._renderValidationSummary();
                break;
            case "minColWidth":
                if ("auto" === this.option("colCount")) {
                    this._invalidate()
                }
                break;
            case "alignRootItemLabels":
            case "readOnly":
            case "isDirty":
                break;
            case "width":
                this.callBase(args);
                this._rootLayoutManager.option(args.name, args.value);
                this._alignLabels(this._rootLayoutManager, this._rootLayoutManager.isSingleColumnMode());
                break;
            case "validationGroup":
                ValidationEngine.removeGroup(args.previousValue || this);
                this._invalidate();
                break;
            default:
                this.callBase(args)
        }
    },
    _itemsOptionChangedHandler(args) {
        var nameParts = args.fullName.split(".");
        var {
            value: value
        } = args;
        var itemPath = this._getItemPath(nameParts);
        var item = this.option(itemPath);
        var optionNameWithoutPath = args.fullName.replace("".concat(itemPath, "."), "");
        var simpleOptionName = optionNameWithoutPath.split(".")[0].replace(/\[\d+]/, "");
        var itemAction = this._tryCreateItemOptionAction(simpleOptionName, item, item[simpleOptionName], args.previousValue, itemPath);
        var result = this._tryExecuteItemOptionAction(itemAction) || this._tryChangeLayoutManagerItemOption(args.fullName, value);
        if (!result && item) {
            this._changeItemOption(item, optionNameWithoutPath, value);
            var items = this._generateItemsFromData(this.option("items"));
            this.option("items", items);
            result = true
        }
        return result
    },
    _formDataOptionChangedHandler(args) {
        var nameParts = args.fullName.split(".");
        var {
            value: value
        } = args;
        var dataField = nameParts.slice(1).join(".");
        var editor = this.getEditor(dataField);
        if (editor) {
            editor.option("value", value)
        } else {
            this._triggerOnFieldDataChanged({
                dataField: dataField,
                value: value
            })
        }
        return true
    },
    _tryCreateItemOptionAction(optionName, item, value, previousValue, itemPath) {
        if ("tabs" === optionName) {
            this._itemsRunTimeInfo.removeItemsByPathStartWith("".concat(itemPath, ".tabs"));
            value = this._prepareItems(value, true, itemPath, true)
        }
        return tryCreateItemOptionAction(optionName, {
            item: item,
            value: value,
            previousValue: previousValue,
            itemsRunTimeInfo: this._itemsRunTimeInfo
        })
    },
    _tryExecuteItemOptionAction: action => action && action.tryExecute(),
    _updateValidationGroupAndSummaryIfNeeded(fullName) {
        var _a;
        var optionName = getOptionNameFromFullName(fullName);
        if (ITEM_OPTIONS_FOR_VALIDATION_UPDATING.includes(optionName)) {
            ValidationEngine.addGroup(this._getValidationGroup(), false);
            if (this.option("showValidationSummary")) {
                null === (_a = this._validationSummary) || void 0 === _a ? void 0 : _a.refreshValidationGroup()
            }
        }
    },
    _setLayoutManagerItemOption(layoutManager, optionName, value, path) {
        if (this._updateLockCount > 0) {
            !layoutManager._updateLockCount && layoutManager.beginUpdate();
            var key = this._itemsRunTimeInfo.findKeyByPath(path);
            this.postponedOperations.add(key, () => {
                !layoutManager._disposed && layoutManager.endUpdate();
                return Deferred().resolve()
            })
        }
        var contentReadyHandler = e => {
            e.component.off("contentReady", contentReadyHandler);
            if (isFullPathContainsTabs(path)) {
                var tabPath = tryGetTabPath(path);
                var tabLayoutManager = this._itemsRunTimeInfo.findGroupOrTabLayoutManagerByPath(tabPath);
                if (tabLayoutManager) {
                    this._alignLabelsInColumn({
                        items: tabLayoutManager.option("items"),
                        layoutManager: tabLayoutManager,
                        $container: tabLayoutManager.$element(),
                        inOneColumn: tabLayoutManager.isSingleColumnMode()
                    })
                }
            } else {
                this._alignLabels(this._rootLayoutManager, this._rootLayoutManager.isSingleColumnMode())
            }
        };
        layoutManager.on("contentReady", contentReadyHandler);
        layoutManager.option(optionName, value);
        this._updateValidationGroupAndSummaryIfNeeded(optionName)
    },
    _tryChangeLayoutManagerItemOption(fullName, value) {
        var nameParts = fullName.split(".");
        var optionName = getOptionNameFromFullName(fullName);
        if ("items" === optionName && nameParts.length > 1) {
            var itemPath = this._getItemPath(nameParts);
            var layoutManager = this._itemsRunTimeInfo.findGroupOrTabLayoutManagerByPath(itemPath);
            if (layoutManager) {
                this._itemsRunTimeInfo.removeItemsByItems(layoutManager.getItemsRunTimeInfo());
                var items = this._prepareItems(value, false, itemPath);
                this._setLayoutManagerItemOption(layoutManager, optionName, items, itemPath);
                return true
            }
        } else if (nameParts.length > 2) {
            var endPartIndex = nameParts.length - 2;
            var _itemPath = this._getItemPath(nameParts.slice(0, endPartIndex));
            var _layoutManager = this._itemsRunTimeInfo.findGroupOrTabLayoutManagerByPath(_itemPath);
            if (_layoutManager) {
                var fullOptionName = getFullOptionName(nameParts[endPartIndex], optionName);
                if ("editorType" === optionName) {
                    if (_layoutManager.option(fullOptionName) !== value) {
                        return false
                    }
                }
                if ("visible" === optionName) {
                    var formItems = this.option(getFullOptionName(_itemPath, "items"));
                    if (formItems && formItems.length) {
                        var layoutManagerItems = _layoutManager.option("items");
                        formItems.forEach((item, index) => {
                            var layoutItem = layoutManagerItems[index];
                            layoutItem.visibleIndex = item.visibleIndex
                        })
                    }
                }
                this._setLayoutManagerItemOption(_layoutManager, fullOptionName, value, _itemPath);
                return true
            }
        }
        return false
    },
    _tryChangeLayoutManagerItemOptions(itemPath, options) {
        var result;
        this.beginUpdate();
        each(options, (optionName, optionValue) => {
            result = this._tryChangeLayoutManagerItemOption(getFullOptionName(itemPath, optionName), optionValue);
            if (!result) {
                return false
            }
        });
        this.endUpdate();
        return result
    },
    _getItemPath(nameParts) {
        var itemPath = nameParts[0];
        var i;
        for (i = 1; i < nameParts.length; i++) {
            if (-1 !== nameParts[i].search(/items\[\d+]|tabs\[\d+]/)) {
                itemPath += ".".concat(nameParts[i])
            } else {
                break
            }
        }
        return itemPath
    },
    _triggerOnFieldDataChanged(args) {
        this._updateIsDirty(args.dataField);
        this._createActionByOption("onFieldDataChanged")(args)
    },
    _triggerOnFieldDataChangedByDataSet(data) {
        if (data && isObject(data)) {
            Object.keys(data).forEach(key => {
                this._triggerOnFieldDataChanged({
                    dataField: key,
                    value: data[key]
                })
            })
        }
    },
    _updateFieldValue(dataField, value) {
        if (isDefined(this.option("formData"))) {
            var editor = this.getEditor(dataField);
            this.option("formData.".concat(dataField), value);
            if (editor) {
                var editorValue = editor.option("value");
                if (editorValue !== value) {
                    editor.option("value", value)
                }
            }
        }
    },
    _generateItemsFromData(items) {
        var formData = this.option("formData");
        var result = [];
        if (!items && isDefined(formData)) {
            each(formData, dataField => {
                result.push({
                    dataField: dataField
                })
            })
        }
        if (items) {
            each(items, (index, item) => {
                if (isObject(item)) {
                    result.push(item)
                } else {
                    result.push({
                        dataField: item
                    })
                }
            })
        }
        return result
    },
    _getItemByField(field, items) {
        var that = this;
        var fieldParts = isObject(field) ? field : that._getFieldParts(field);
        var {
            fieldName: fieldName
        } = fieldParts;
        var {
            fieldPath: fieldPath
        } = fieldParts;
        var resultItem;
        if (items.length) {
            each(items, (index, item) => {
                var {
                    itemType: itemType
                } = item;
                if (fieldPath.length) {
                    var path = fieldPath.slice();
                    item = that._getItemByFieldPath(path, fieldName, item)
                } else if ("group" === itemType && !(item.caption || item.name) || "tabbed" === itemType && !item.name) {
                    var subItemsField = that._getSubItemField(itemType);
                    item.items = that._generateItemsFromData(item.items);
                    item = that._getItemByField({
                        fieldName: fieldName,
                        fieldPath: fieldPath
                    }, item[subItemsField])
                }
                if (isEqualToDataFieldOrNameOrTitleOrCaption(item, fieldName)) {
                    resultItem = item;
                    return false
                }
            })
        }
        return resultItem
    },
    _getFieldParts(field) {
        var fieldName = field;
        var separatorIndex = fieldName.indexOf(".");
        var resultPath = [];
        while (-1 !== separatorIndex) {
            resultPath.push(fieldName.substr(0, separatorIndex));
            fieldName = fieldName.substr(separatorIndex + 1);
            separatorIndex = fieldName.indexOf(".")
        }
        return {
            fieldName: fieldName,
            fieldPath: resultPath.reverse()
        }
    },
    _getItemByFieldPath(path, fieldName, item) {
        var {
            itemType: itemType
        } = item;
        var subItemsField = this._getSubItemField(itemType);
        var isItemWithSubItems = "group" === itemType || "tabbed" === itemType || item.title;
        var result;
        do {
            if (isItemWithSubItems) {
                var name = item.name || item.caption || item.title;
                var isGroupWithName = isDefined(name);
                var nameWithoutSpaces = getTextWithoutSpaces(name);
                var pathNode = void 0;
                item[subItemsField] = this._generateItemsFromData(item[subItemsField]);
                if (isGroupWithName) {
                    pathNode = path.pop()
                }
                if (!path.length) {
                    result = this._getItemByField(fieldName, item[subItemsField]);
                    if (result) {
                        break
                    }
                }
                if (!isGroupWithName || isGroupWithName && nameWithoutSpaces === pathNode) {
                    if (path.length) {
                        result = this._searchItemInEverySubItem(path, fieldName, item[subItemsField])
                    }
                }
            } else {
                break
            }
        } while (path.length && !isDefined(result));
        return result
    },
    _getSubItemField: itemType => "tabbed" === itemType ? "tabs" : "items",
    _searchItemInEverySubItem(path, fieldName, items) {
        var that = this;
        var result;
        each(items, (index, groupItem) => {
            result = that._getItemByFieldPath(path.slice(), fieldName, groupItem);
            if (result) {
                return false
            }
        });
        if (!result) {
            result = false
        }
        return result
    },
    _changeItemOption(item, option, value) {
        if (isObject(item)) {
            item[option] = value
        }
    },
    _dimensionChanged() {
        var currentScreenFactor = this._getCurrentScreenFactor();
        if (this._lastMarkupScreenFactor !== currentScreenFactor) {
            if (this._isColCountChanged(this._lastMarkupScreenFactor, currentScreenFactor)) {
                this._targetScreenFactor = currentScreenFactor;
                this._refresh();
                this._targetScreenFactor = void 0
            }
            this._lastMarkupScreenFactor = currentScreenFactor
        }
    },
    _isColCountChanged(oldScreenSize, newScreenSize) {
        var isChanged = false;
        each(this._cachedColCountOptions, (index, item) => {
            if (item.colCountByScreen[oldScreenSize] !== item.colCountByScreen[newScreenSize]) {
                isChanged = true;
                return false
            }
        });
        return isChanged
    },
    _refresh() {
        var editorSelector = ".".concat(FOCUSED_STATE_CLASS, " > :not(.dx-dropdowneditor-input-wrapper) input,") + " .".concat(FOCUSED_STATE_CLASS, " textarea");
        eventsEngine.trigger(this.$element().find(editorSelector), "change");
        this.callBase()
    },
    _updateIsDirty(dataField) {
        var editor = this.getEditor(dataField);
        if (!editor) {
            return
        }
        if (editor.option("isDirty")) {
            this._dirtyFields.add(dataField)
        } else {
            this._dirtyFields.delete(dataField)
        }
        this.option("isDirty", !!this._dirtyFields.size)
    },
    updateRunTimeInfoForEachEditor(editorAction) {
        this._itemsRunTimeInfo.each((_, itemRunTimeInfo) => {
            var {
                widgetInstance: widgetInstance
            } = itemRunTimeInfo;
            if (isDefined(widgetInstance) && Editor.isEditor(widgetInstance)) {
                editorAction(widgetInstance)
            }
        })
    },
    _clear() {
        this.updateRunTimeInfoForEachEditor(editor => {
            editor.clear();
            editor.option("isValid", true)
        });
        ValidationEngine.resetGroup(this._getValidationGroup())
    },
    _updateData(data, value, isComplexData) {
        var that = this;
        var _data = isComplexData ? value : data;
        if (isObject(_data)) {
            each(_data, (dataField, fieldValue) => {
                that._updateData(isComplexData ? "".concat(data, ".").concat(dataField) : dataField, fieldValue, isObject(fieldValue))
            })
        } else if (isString(data)) {
            that._updateFieldValue(data, value)
        }
    },
    registerKeyHandler(key, handler) {
        this.callBase(key, handler);
        this._itemsRunTimeInfo.each((_, itemRunTimeInfo) => {
            if (isDefined(itemRunTimeInfo.widgetInstance)) {
                itemRunTimeInfo.widgetInstance.registerKeyHandler(key, handler)
            }
        })
    },
    _focusTarget() {
        return this.$element().find(".".concat(FIELD_ITEM_CONTENT_CLASS, " [tabindex]")).first()
    },
    _visibilityChanged() {
        this._alignLabels(this._rootLayoutManager, this._rootLayoutManager.isSingleColumnMode())
    },
    _clearAutoColCountChangedTimeout() {
        if (this.autoColCountChangedTimeoutId) {
            clearTimeout(this.autoColCountChangedTimeoutId);
            this.autoColCountChangedTimeoutId = void 0
        }
    },
    _dispose() {
        this._clearAutoColCountChangedTimeout();
        ValidationEngine.removeGroup(this._getValidationGroup());
        this.callBase()
    },
    clear() {
        this._clear()
    },
    resetValues() {
        this._clear()
    },
    reset(editorsData) {
        this.updateRunTimeInfoForEachEditor(editor => {
            var editorName = editor.option("name");
            if (editorsData && editorName in editorsData) {
                editor.reset(editorsData[editorName])
            } else {
                editor.reset()
            }
        });
        this._renderValidationSummary()
    },
    updateData(data, value) {
        this._updateData(data, value)
    },
    getEditor(dataField) {
        return this._itemsRunTimeInfo.findWidgetInstanceByDataField(dataField) || this._itemsRunTimeInfo.findWidgetInstanceByName(dataField)
    },
    getButton(name) {
        return this._itemsRunTimeInfo.findWidgetInstanceByName(name)
    },
    updateDimensions() {
        var that = this;
        var deferred = Deferred();
        if (that._scrollable) {
            that._scrollable.update().done(() => {
                deferred.resolveWith(that)
            })
        } else {
            deferred.resolveWith(that)
        }
        return deferred.promise()
    },
    itemOption(id, option, value) {
        var items = this._generateItemsFromData(this.option("items"));
        var item = this._getItemByField(id, items);
        var path = getItemPath(items, item);
        if (!item) {
            return
        }
        switch (arguments.length) {
            case 1:
                return item;
            case 3:
                var itemAction = this._tryCreateItemOptionAction(option, item, value, item[option], path);
                this._changeItemOption(item, option, value);
                var fullName = getFullOptionName(path, option);
                if (!this._tryExecuteItemOptionAction(itemAction) && !this._tryChangeLayoutManagerItemOption(fullName, value)) {
                    this.option("items", items)
                }
                break;
            default:
                if (isObject(option)) {
                    if (!this._tryChangeLayoutManagerItemOptions(path, option)) {
                        var allowUpdateItems;
                        each(option, (optionName, optionValue) => {
                            var itemAction = this._tryCreateItemOptionAction(optionName, item, optionValue, item[optionName], path);
                            this._changeItemOption(item, optionName, optionValue);
                            if (!allowUpdateItems && !this._tryExecuteItemOptionAction(itemAction)) {
                                allowUpdateItems = true
                            }
                        });
                        allowUpdateItems && this.option("items", items)
                    }
                }
        }
    },
    validate() {
        return ValidationEngine.validateGroup(this._getValidationGroup())
    },
    getItemID(name) {
        return "dx_".concat(this.option("formID"), "_").concat(name || new Guid)
    },
    getTargetScreenFactor() {
        return this._targetScreenFactor
    }
});
registerComponent("dxForm", Form);
export default Form;
