/**
 * DevExtreme (cjs/__internal/grids/tree_list/editing/m_editing.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";
require("../module_not_extended/editor_factory");
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 _message = _interopRequireDefault(require("../../../../localization/message"));
var _ui = _interopRequireDefault(require("../../../../ui/widget/ui.errors"));
var _m_editing = require("../../../grids/grid_core/editing/m_editing");
var _m_utils = _interopRequireDefault(require("../../../grids/grid_core/m_utils"));
var _m_core = _interopRequireDefault(require("../m_core"));

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 TREELIST_EXPAND_ICON_CONTAINER_CLASS = "dx-treelist-icon-container";
const SELECT_CHECKBOX_CLASS = "dx-select-checkbox";
const DATA_EDIT_DATA_INSERT_TYPE = "insert";
let EditingController = function(_editingModule$contro) {
    _inheritsLoose(EditingController, _editingModule$contro);

    function EditingController() {
        return _editingModule$contro.apply(this, arguments) || this
    }
    var _proto = EditingController.prototype;
    _proto._generateNewItem = function(key) {
        const item = _editingModule$contro.prototype._generateNewItem.call(this, key);
        item.data = {
            key: key
        };
        item.children = [];
        item.level = 0;
        item.parentKey = this.option("rootValue");
        return item
    };
    _proto._isProcessedItem = function() {
        return true
    };
    _proto._setInsertAfterOrBeforeKey = function(change, parentKey) {
        if (void 0 !== parentKey && parentKey !== this.option("rootValue")) {
            change.insertAfterKey = parentKey
        } else {
            _editingModule$contro.prototype._setInsertAfterOrBeforeKey.apply(this, arguments)
        }
    };
    _proto._getLoadedRowIndex = function(items, change) {
        const dataSourceAdapter = this._dataController.dataSource();
        const parentKey = null === dataSourceAdapter || void 0 === dataSourceAdapter ? void 0 : dataSourceAdapter.parentKeyOf(change.data);
        if (void 0 !== parentKey && parentKey !== this.option("rootValue")) {
            const rowIndex = _m_utils.default.getIndexByKey(parentKey, items);
            if (rowIndex >= 0 && this._dataController.isRowExpanded(parentKey)) {
                return rowIndex + 1
            }
            return -1
        }
        return _editingModule$contro.prototype._getLoadedRowIndex.apply(this, arguments)
    };
    _proto._isEditColumnVisible = function() {
        const result = _editingModule$contro.prototype._isEditColumnVisible.apply(this, arguments);
        const editingOptions = this.option("editing");
        return result || editingOptions.allowAdding
    };
    _proto._isDefaultButtonVisible = function(button, options) {
        const result = _editingModule$contro.prototype._isDefaultButtonVisible.apply(this, arguments);
        const {
            row: row
        } = options;
        if ("add" === button.name) {
            return this.allowAdding(options) && row.rowIndex !== this._getVisibleEditRowIndex() && !(row.removed || row.isNewRow)
        }
        return result
    };
    _proto._getEditingButtons = function(options) {
        const buttons = _editingModule$contro.prototype._getEditingButtons.apply(this, arguments);
        if (!options.column.buttons) {
            buttons.unshift(this._getButtonConfig("add", options))
        }
        return buttons
    };
    _proto._beforeSaveEditData = function(change) {
        var _a;
        const result = _editingModule$contro.prototype._beforeSaveEditData.apply(this, arguments);
        if (change && "insert" !== change.type) {
            const store = null === (_a = this._dataController) || void 0 === _a ? void 0 : _a.store();
            const key = null === store || void 0 === store ? void 0 : store.key();
            if (!(0, _type.isDefined)(key)) {
                throw _ui.default.Error("E1045")
            }
        }
        return result
    };
    _proto.addRowByRowIndex = function(rowIndex) {
        const row = this._dataController.getVisibleRows()[rowIndex];
        return this.addRow(row ? row.key : void 0)
    };
    _proto.addRow = function(key) {
        if (void 0 === key) {
            key = this.option("rootValue")
        }
        return _editingModule$contro.prototype.addRow.call(this, key)
    };
    _proto._addRowCore = function(data, parentKey, oldEditRowIndex) {
        const rootValue = this.option("rootValue");
        const dataSourceAdapter = this._dataController.dataSource();
        const parentKeyGetter = dataSourceAdapter.createParentIdGetter();
        parentKey = parentKeyGetter(data);
        if (void 0 !== parentKey && parentKey !== rootValue && !this._dataController.isRowExpanded(parentKey)) {
            const deferred = new _deferred.Deferred;
            this._dataController.expandRow(parentKey).done(() => {
                setTimeout(() => {
                    _editingModule$contro.prototype._addRowCore.call(this, data, parentKey, oldEditRowIndex).done(deferred.resolve).fail(deferred.reject)
                })
            }).fail(deferred.reject);
            return deferred.promise()
        }
        return _editingModule$contro.prototype._addRowCore.call(this, data, parentKey, oldEditRowIndex)
    };
    _proto._initNewRow = function(options, parentKey) {
        const dataSourceAdapter = this._dataController.dataSource();
        const parentIdSetter = dataSourceAdapter.createParentIdSetter();
        parentIdSetter(options.data, parentKey);
        return _editingModule$contro.prototype._initNewRow.apply(this, arguments)
    };
    _proto.allowAdding = function(options) {
        return this._allowEditAction("allowAdding", options)
    };
    _proto._needToCloseEditableCell = function($targetElement) {
        return _editingModule$contro.prototype._needToCloseEditableCell.apply(this, arguments) || $targetElement.closest(".".concat("dx-treelist-icon-container")).length && this.isEditing()
    };
    _proto.getButtonLocalizationNames = function() {
        const names = _editingModule$contro.prototype.getButtonLocalizationNames.apply(this);
        names.add = "dxTreeList-editingAddRowToNode";
        return names
    };
    return EditingController
}(_m_editing.editingModule.controllers.editing);
const rowsView = Base => function(_editingModule$extend) {
    _inheritsLoose(TreeListEditingRowsViewExtender, _editingModule$extend);

    function TreeListEditingRowsViewExtender() {
        return _editingModule$extend.apply(this, arguments) || this
    }
    var _proto2 = TreeListEditingRowsViewExtender.prototype;
    _proto2._renderCellCommandContent = function($container, options) {
        const editingController = this._editingController;
        const isEditRow = options.row && editingController.isEditRow(options.row.rowIndex);
        const isEditing = options.isEditing || isEditRow;
        if (!isEditing) {
            return _editingModule$extend.prototype._renderCellCommandContent.apply(this, arguments)
        }
        return false
    };
    _proto2.validateClick = function(e) {
        const $targetElement = (0, _renderer.default)(e.event.target);
        const originalClickHandler = "dxdblclick" === e.event.type ? _editingModule$extend.prototype._rowDblClick : _editingModule$extend.prototype._rowClick;
        if ($targetElement.closest(".".concat("dx-select-checkbox")).length) {
            return false
        }
        return !this.needToCallOriginalClickHandler(e, originalClickHandler)
    };
    _proto2.needToCallOriginalClickHandler = function(e, originalClickHandler) {
        const $targetElement = (0, _renderer.default)(e.event.target);
        if (!$targetElement.closest(".".concat("dx-treelist-icon-container")).length) {
            originalClickHandler.call(this, e);
            return true
        }
        return false
    };
    _proto2._rowClick = function(e) {
        if (this.validateClick(e)) {
            _editingModule$extend.prototype._rowClickTreeListHack.apply(this, arguments)
        }
    };
    _proto2._rowDblClick = function(e) {
        if (this.validateClick(e)) {
            _editingModule$extend.prototype._rowDblClickTreeListHack.apply(this, arguments)
        }
    };
    return TreeListEditingRowsViewExtender
}(_m_editing.editingModule.extenders.views.rowsView(Base));
const data = Base => function(_dataControllerEditin) {
    _inheritsLoose(DataControllerTreeListEditingExtender, _dataControllerEditin);

    function DataControllerTreeListEditingExtender() {
        return _dataControllerEditin.apply(this, arguments) || this
    }
    var _proto3 = DataControllerTreeListEditingExtender.prototype;
    _proto3.changeRowExpand = function() {
        this._editingController.refresh();
        return _dataControllerEditin.prototype.changeRowExpand.apply(this, arguments)
    };
    return DataControllerTreeListEditingExtender
}((0, _m_editing.dataControllerEditingExtenderMixin)(Base));
_m_core.default.registerModule("editing", {
    defaultOptions: () => (0, _extend.extend)(true, _m_editing.editingModule.defaultOptions(), {
        editing: {
            texts: {
                addRowToNode: _message.default.format("dxTreeList-editingAddRowToNode")
            }
        }
    }),
    controllers: {
        editing: EditingController
    },
    extenders: {
        controllers: {
            data: data
        },
        views: {
            rowsView: rowsView,
            headerPanel: _m_editing.editingModule.extenders.views.headerPanel
        }
    }
});
