"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreviewHelper = exports.GetCellUpdateRequestsFromPreview = exports.GetCellInfosFromPreview = exports.GetValidationMessage = exports.GetPreviewValidationSummary = void 0;
const StringExtensions_1 = require("../Extensions/StringExtensions");
function GetPreviewValidationSummary(previewResults) {
    let hasAnyValidation = false;
    let hasAllValidation = true;
    previewResults.forEach((previewResult) => {
        const hasValidationErrors = previewResult.validationRules.length > 0;
        if (hasValidationErrors) {
            hasAnyValidation = true;
        }
        else {
            hasAllValidation = false;
        }
    });
    return {
        validationResult: hasAnyValidation ? (hasAllValidation ? 'All' : 'Some') : 'None',
    };
}
exports.GetPreviewValidationSummary = GetPreviewValidationSummary;
function GetValidationMessage(previewInfo, newValue) {
    if (previewInfo && StringExtensions_1.StringExtensions.IsNotNullOrEmpty(newValue)) {
        let previewValidationSummary = previewInfo.previewValidationSummary;
        if (previewValidationSummary.validationResult == 'All') {
            return 'All proposed edits have failed validation (see Preview for details).';
        }
        else if (previewValidationSummary.validationResult == 'Some') {
            return 'Some proposed edits have failed validatio (see Preview for details).\nThese updates will be ignored.';
        }
    }
    return '';
}
exports.GetValidationMessage = GetValidationMessage;
function GetCellInfosFromPreview(previewInfo, bypassValidationWarnings) {
    let newValues = [];
    if (bypassValidationWarnings) {
        for (let previewResult of previewInfo.previewResults) {
            if (previewResult.validationRules.length == 0) {
                newValues.push({
                    primaryKeyValue: previewResult.id,
                    rawValue: previewResult.computedValue,
                    displayValue: previewResult.computedValue,
                    normalisedValue: previewResult.computedValue,
                    rowNode: previewResult.rowNode,
                    column: previewInfo.column,
                    isPivotCell: false,
                    isRowGroupCell: false,
                });
            }
        }
    }
    else {
        previewInfo.previewResults
            .filter((p) => p.validationRules.length == 0)
            .forEach((pr) => {
            newValues.push({
                primaryKeyValue: pr.id,
                column: previewInfo.column,
                rawValue: pr.computedValue,
                displayValue: pr.computedValue,
                normalisedValue: pr.computedValue,
                rowNode: pr.rowNode,
                isPivotCell: false,
                isRowGroupCell: false,
            });
        });
    }
    return newValues;
}
exports.GetCellInfosFromPreview = GetCellInfosFromPreview;
function GetCellUpdateRequestsFromPreview(previewInfo, bypassValidationWarnings) {
    let newValues = [];
    if (bypassValidationWarnings) {
        for (let previewResult of previewInfo.previewResults) {
            if (previewResult.validationRules.length == 0) {
                newValues.push({
                    primaryKeyValue: previewResult.id,
                    newValue: previewResult.computedValue,
                    rowNode: previewResult.rowNode,
                    columnId: previewInfo.column.columnId,
                });
            }
        }
    }
    else {
        previewInfo.previewResults
            .filter((p) => p.validationRules.length == 0)
            .forEach((pr) => {
            newValues.push({
                primaryKeyValue: pr.id,
                columnId: previewInfo.column.columnId,
                newValue: pr.computedValue,
                rowNode: pr.rowNode,
            });
        });
    }
    return newValues;
}
exports.GetCellUpdateRequestsFromPreview = GetCellUpdateRequestsFromPreview;
exports.PreviewHelper = {
    GetPreviewValidationSummary,
    GetValidationMessage,
    GetCellInfosFromPreview,
    GetCellUpdateRequestsFromPreview,
};
exports.default = exports.PreviewHelper;
