/**
 * DevExtreme (cjs/__internal/grids/grid_core/editing/m_editing_utils.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/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.getButtonIndex = exports.generateNewRowTempKey = exports.forEachFormItems = exports.createFailureHandler = void 0;
exports.getButtonName = getButtonName;
exports.getEditorType = exports.getEditingTexts = void 0;
exports.isEditable = isEditable;
exports.isNewRowTempKey = exports.isEditingOrShowEditorAlwaysDataCell = exports.isEditingCell = void 0;
var _guid = _interopRequireDefault(require("../../../../core/guid"));
var _type = require("../../../../core/utils/type");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const NEW_ROW_TEMP_KEY_PREFIX = "_DX_KEY_";
const GUID_LENGTH = 36;
const createFailureHandler = function(deferred) {
    return function(arg) {
        const error = arg instanceof Error ? arg : new Error(arg && String(arg) || "Unknown error");
        deferred.reject(error)
    }
};
exports.createFailureHandler = createFailureHandler;
const isEditingCell = function(isEditRow, cellOptions) {
    return cellOptions.isEditing || isEditRow && cellOptions.column.allowEditing
};
exports.isEditingCell = isEditingCell;
const isEditingOrShowEditorAlwaysDataCell = function(isEditRow, cellOptions) {
    const isCommandCell = !!cellOptions.column.command;
    const isEditing = isEditingCell(isEditRow, cellOptions);
    const isEditorCell = !isCommandCell && (isEditing || cellOptions.column.showEditorAlways);
    return "data" === cellOptions.rowType && isEditorCell
};
exports.isEditingOrShowEditorAlwaysDataCell = isEditingOrShowEditorAlwaysDataCell;
const getEditingTexts = options => {
    const editingTexts = options.component.option("editing.texts") || {};
    return {
        save: editingTexts.saveRowChanges,
        cancel: editingTexts.cancelRowChanges,
        edit: editingTexts.editRow,
        undelete: editingTexts.undeleteRow,
        delete: editingTexts.deleteRow,
        add: editingTexts.addRowToNode
    }
};
exports.getEditingTexts = getEditingTexts;
const generateNewRowTempKey = () => "".concat("_DX_KEY_").concat(new _guid.default);
exports.generateNewRowTempKey = generateNewRowTempKey;
const isNewRowTempKey = key => "string" === typeof key && key.startsWith("_DX_KEY_") && key.length === "_DX_KEY_".length + 36;
exports.isNewRowTempKey = isNewRowTempKey;
const getButtonIndex = (buttons, name) => {
    let result = -1;
    buttons.some((button, index) => {
        if (getButtonName(button) === name) {
            result = index;
            return true
        }
    });
    return result
};
exports.getButtonIndex = getButtonIndex;

function getButtonName(button) {
    return (0, _type.isObject)(button) ? button.name : button
}

function isEditable($element) {
    return $element && ($element.is("input") || $element.is("textarea"))
}
const getEditorType = item => {
    var _a;
    const {
        column: column
    } = item;
    return item.isCustomEditorType ? item.editorType : null === (_a = column.formItem) || void 0 === _a ? void 0 : _a.editorType
};
exports.getEditorType = getEditorType;
const forEachFormItems = (items, callBack) => {
    items.forEach(item => {
        if (item.items || item.tabs) {
            forEachFormItems(item.items || item.tabs, callBack)
        } else {
            callBack(item)
        }
    })
};
exports.forEachFormItems = forEachFormItems;
