/**
 * DevExtreme (esm/__internal/grids/grid_core/error_handling/m_error_handling.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 $ from "../../../../core/renderer";
import {
    each
} from "../../../../core/utils/iterator";
import {
    name as clickEventName
} from "../../../../events/click";
import eventsEngine from "../../../../events/core/events_engine";
import messageLocalization from "../../../../localization/message";
import modules from "../m_modules";
var ERROR_ROW_CLASS = "dx-error-row";
var ERROR_MESSAGE_CLASS = "dx-error-message";
var ERROR_CLOSEBUTTON_CLASS = "dx-closebutton";
var ACTION_CLASS = "action";
export class ErrorHandlingController extends modules.ViewController {
    init() {
        this._resizingController = this.getController("resizing");
        this._columnsController = this.getController("columns");
        this._columnHeadersView = this.getView("columnHeadersView");
        this._rowsView = this.getView("rowsView")
    }
    _createErrorRow(error, $tableElements) {
        var $errorRow;
        var $closeButton;
        var $errorMessage = this._renderErrorMessage(error);
        if ($tableElements) {
            $errorRow = $("<tr>").attr("role", "row").addClass(ERROR_ROW_CLASS);
            $closeButton = $("<div>").addClass(ERROR_CLOSEBUTTON_CLASS).addClass(this.addWidgetPrefix(ACTION_CLASS));
            eventsEngine.on($closeButton, clickEventName, this.createAction(args => {
                var _a, _b;
                var e = args.event;
                var $errorRow;
                var errorRowIndex = $(e.currentTarget).closest(".".concat(ERROR_ROW_CLASS)).index();
                e.stopPropagation();
                each($tableElements, (_, tableElement) => {
                    $errorRow = $(tableElement).children("tbody").children("tr").eq(errorRowIndex);
                    this.removeErrorRow($errorRow)
                });
                null === (_b = null === (_a = this._resizingController) || void 0 === _a ? void 0 : _a.fireContentReadyAction) || void 0 === _b ? void 0 : _b.call(_a)
            }));
            $("<td>").attr({
                colSpan: this._columnsController.getVisibleColumns().length,
                role: "gridcell"
            }).prepend($closeButton).append($errorMessage).appendTo($errorRow);
            return $errorRow
        }
        return $errorMessage
    }
    _renderErrorMessage(error) {
        var message = error.url ? error.message.replace(error.url, "") : error.message || error;
        var $message = $("<div>").attr("role", "alert").attr("aria-roledescription", messageLocalization.format("dxDataGrid-ariaError")).addClass(ERROR_MESSAGE_CLASS).text(message);
        if (error.url) {
            $("<a>").attr("href", error.url).text(error.url).appendTo($message)
        }
        return $message
    }
    renderErrorRow(error, rowIndex, $popupContent) {
        var _a, _b;
        var that = this;
        var $errorMessageElement;
        var $firstErrorRow;
        if ($popupContent) {
            $popupContent.find(".".concat(ERROR_MESSAGE_CLASS)).remove();
            $errorMessageElement = that._createErrorRow(error);
            $popupContent.prepend($errorMessageElement);
            return $errorMessageElement
        }
        var viewElement = rowIndex >= 0 || !that._columnHeadersView.isVisible() ? that._rowsView : that._columnHeadersView;
        var $tableElements = viewElement.getTableElements();
        each($tableElements, (_, tableElement) => {
            $errorMessageElement = that._createErrorRow(error, $tableElements);
            $firstErrorRow = $firstErrorRow || $errorMessageElement;
            if (rowIndex >= 0) {
                var $row = viewElement._getRowElements($(tableElement)).eq(rowIndex);
                that.removeErrorRow($row.next());
                $errorMessageElement.insertAfter($row)
            } else {
                var $tbody = $(tableElement).children("tbody");
                var rowElements = $tbody.children("tr");
                if (that._columnHeadersView.isVisible()) {
                    that.removeErrorRow(rowElements.last());
                    $(tableElement).append($errorMessageElement)
                } else {
                    that.removeErrorRow(rowElements.first());
                    $tbody.first().prepend($errorMessageElement)
                }
            }
        });
        null === (_b = null === (_a = this._resizingController) || void 0 === _a ? void 0 : _a.fireContentReadyAction) || void 0 === _b ? void 0 : _b.call(_a);
        return $firstErrorRow
    }
    removeErrorRow($row) {
        if (!$row) {
            var $columnHeaders = this._columnHeadersView && this._columnHeadersView.element();
            $row = $columnHeaders && $columnHeaders.find(".".concat(ERROR_ROW_CLASS));
            if (!$row || !$row.length) {
                var $rowsViewElement = this._rowsView.element();
                $row = $rowsViewElement && $rowsViewElement.find(".".concat(ERROR_ROW_CLASS))
            }
        }
        $row && $row.hasClass(ERROR_ROW_CLASS) && $row.remove()
    }
    optionChanged(args) {
        switch (args.name) {
            case "errorRowEnabled":
                args.handled = true;
                break;
            default:
                super.optionChanged(args)
        }
    }
}
var data = Base => class extends Base {
    init() {
        super.init();
        this.dataErrorOccurred.add((error, $popupContent) => {
            if (this.option("errorRowEnabled")) {
                this._errorHandlingController.renderErrorRow(error, void 0, $popupContent)
            }
        });
        this.changed.add(e => {
            var _a, _b;
            if (e && "loadError" === e.changeType) {
                return
            }
            if (this._editingController && !this._editingController.hasChanges()) {
                null === (_b = null === (_a = this._errorHandlingController) || void 0 === _a ? void 0 : _a.removeErrorRow) || void 0 === _b ? void 0 : _b.call(_a)
            }
        })
    }
};
export var errorHandlingModule = {
    defaultOptions: () => ({
        errorRowEnabled: true
    }),
    controllers: {
        errorHandling: ErrorHandlingController
    },
    extenders: {
        controllers: {
            data: data
        }
    }
};
