"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get runSourceDataValidator () {
        return runSourceDataValidator;
    },
    get runSourceDataValidators () {
        return runSourceDataValidators;
    }
});
const _console = require("../helpers/console");
const _function = require("../helpers/function");
const _mixed = require("../helpers/mixed");
function runSourceDataValidator(value, cellMeta, source) {
    const validator = cellMeta.sourceDataValidator;
    if (!(0, _function.isFunction)(validator)) {
        return true;
    }
    const isValid = validator(value, cellMeta, source);
    if (isValid === true) {
        return true;
    }
    const { row, col, sourceDataWarningMessage, allowInvalid } = cellMeta;
    if (sourceDataWarningMessage) {
        logSourceDataWarning(sourceDataWarningMessage, [
            {
                row,
                col,
                value
            }
        ]);
    }
    return allowInvalid === true;
}
function runSourceDataValidators(hotInstance, source) {
    const rowSourceCount = hotInstance.countSourceRows();
    const colSourceCount = hotInstance.countSourceCols();
    if (rowSourceCount === 0 || colSourceCount === 0) {
        return;
    }
    const invalidByMessageType = new Map();
    for(let row = 0; row < rowSourceCount; row += 1){
        for(let col = 0; col < colSourceCount; col += 1){
            const visualRow = hotInstance.toVisualRow(row);
            const visualColumn = hotInstance.toVisualColumn(col);
            if (visualRow === null || visualColumn === null) {
                continue;
            }
            const cellMeta = hotInstance.getCellMeta(visualRow, visualColumn);
            const { sourceDataWarningMessage, sourceDataValidator, allowInvalid } = cellMeta;
            if (!(0, _function.isFunction)(sourceDataValidator)) {
                continue;
            }
            const dataSource = hotInstance._getDataSource();
            const value = dataSource.getAtCell(row, col, null);
            const isValid = sourceDataValidator(value, cellMeta, source);
            if (isValid === true) {
                continue;
            }
            if (allowInvalid === false) {
                dataSource.setAtCell(row, col, null);
            }
            if (sourceDataWarningMessage) {
                const message = sourceDataWarningMessage;
                const list = invalidByMessageType.get(message) ?? [];
                list.push({
                    row,
                    col,
                    value,
                    message
                });
                invalidByMessageType.set(message, list);
            }
        }
    }
    invalidByMessageType.forEach((items, message)=>{
        logSourceDataWarning(message, items);
    });
}
/**
 * Logs a source data warning.
 *
 * @param {string} message The message to log.
 * @param {Array} items The list of affected cells.
 */ function logSourceDataWarning(message, items) {
    (0, _console.logAggregatedItems)({
        logFunction: _console.warn,
        message,
        items,
        itemFormatter: ({ row, col, value })=>{
            const rawValue = (0, _mixed.stringify)(value);
            const shortValue = rawValue.length > 64 ? `${rawValue.slice(0, 64)}...` : rawValue;
            return `row ${row}, col ${col}, value: "${shortValue}"`;
        }
    });
}
