/**
 * DevExtreme (esm/__internal/grids/grid_core/editing/m_editing_row_based.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    equalByValue
} from "../../../../core/utils/common";
import {
    EDIT_FORM_CLASS,
    EDIT_MODE_ROW,
    EDIT_ROW,
    EDITING_EDITROWKEY_OPTION_NAME,
    MODES_WITH_DELAYED_FOCUS,
    ROW_SELECTED_CLASS
} from "./const";
var editingControllerExtender = Base => class extends Base {
    isRowEditMode() {
        return this.getEditMode() === EDIT_MODE_ROW
    }
    _afterCancelEditData(rowIndex) {
        var dataController = this._dataController;
        if (this.isRowBasedEditMode() && rowIndex >= 0) {
            dataController.updateItems({
                changeType: "update",
                rowIndices: [rowIndex, rowIndex + 1]
            })
        } else {
            super._afterCancelEditData(rowIndex)
        }
    }
    _isDefaultButtonVisible(button, options) {
        var isRowMode = this.isRowBasedEditMode();
        var isEditRow = options.row && equalByValue(options.row.key, this.option(EDITING_EDITROWKEY_OPTION_NAME));
        if (isRowMode) {
            switch (button.name) {
                case "edit":
                    return !isEditRow && this.allowUpdating(options);
                case "delete":
                    return super._isDefaultButtonVisible(button, options) && !isEditRow;
                case "save":
                case "cancel":
                    return isEditRow;
                default:
                    return super._isDefaultButtonVisible(button, options)
            }
        }
        return super._isDefaultButtonVisible(button, options)
    }
    isEditRow(rowIndex) {
        return this.isRowBasedEditMode() && this.isEditRowByIndex(rowIndex)
    }
    _cancelSaving(result) {
        if (this.isRowBasedEditMode()) {
            if (!this.hasChanges()) {
                this._cancelEditDataCore()
            }
        }
        super._cancelSaving(result)
    }
    _refreshCore(params) {
        var {
            allowCancelEditing: allowCancelEditing
        } = null !== params && void 0 !== params ? params : {};
        if (this.isRowBasedEditMode()) {
            var hasUpdateChanges = this.getChanges().filter(it => "update" === it.type).length > 0;
            this.init();
            allowCancelEditing && hasUpdateChanges && this._cancelEditDataCore()
        }
        super._refreshCore(params)
    }
    _isEditColumnVisible() {
        var result = super._isEditColumnVisible();
        var editingOptions = this.option("editing");
        var isRowEditMode = this.isRowEditMode();
        var isVisibleInRowEditMode = editingOptions.allowUpdating || editingOptions.allowAdding;
        return result || isRowEditMode && isVisibleInRowEditMode
    }
    _focusEditorIfNeed() {
        var editMode = this.getEditMode();
        if (this._needFocusEditor) {
            if (MODES_WITH_DELAYED_FOCUS.includes(editMode)) {
                var $editingCell = this.getFocusedCellInRow(this._getVisibleEditRowIndex());
                this._delayedInputFocus($editingCell, () => {
                    $editingCell && this.component.focus($editingCell)
                })
            }
            this._needFocusEditor = false
        }
    }
};
var data = Base => class extends Base {
    _getChangedColumnIndices(oldItem, newItem, rowIndex, isLiveUpdate) {
        if (this._editingController.isRowBasedEditMode() && oldItem.isEditing !== newItem.isEditing) {
            return
        }
        return super._getChangedColumnIndices.apply(this, arguments)
    }
};
var rowsView = Base => class extends Base {
    _createRow(row) {
        var $row = super._createRow.apply(this, arguments);
        if (row) {
            var editingController = this._editingController;
            var isEditRow = editingController.isEditRow(row.rowIndex);
            if (isEditRow) {
                $row.addClass(EDIT_ROW);
                $row.removeClass(ROW_SELECTED_CLASS);
                if ("detail" === row.rowType) {
                    $row.addClass(this.addWidgetPrefix(EDIT_FORM_CLASS))
                }
            }
        }
        return $row
    }
    _update(change) {
        super._update(change);
        if ("updateSelection" === change.changeType) {
            this.getTableElements().children("tbody").children(".".concat(EDIT_ROW)).removeClass(ROW_SELECTED_CLASS)
        }
    }
};
export var editingRowBasedModule = {
    extenders: {
        controllers: {
            editing: editingControllerExtender,
            data: data
        },
        views: {
            rowsView: rowsView
        }
    }
};
