/* eslint-disable jsdoc/require-description-complete-sentence */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "normalizeSettings", {
    enumerable: true,
    get: function() {
        return normalizeSettings;
    }
});
const _array = require("../../../helpers/array");
const _object = require("../../../helpers/object");
const _mixed = require("../../../helpers/mixed");
const _utils = require("./utils");
function normalizeSettings(sourceSettings, columnsLimit = Infinity) {
    const normalizedSettings = [];
    const rowspanCoverageMap = [];
    if (columnsLimit === 0) {
        return normalizedSettings;
    }
    /**
   * Checks whether the source header item is an explicit empty slot placeholder.
   * That kind of placeholders can be used by the users to indicate positions
   * covered by `rowspan`.
   *
   * @param {*} sourceHeaderSettings The source header settings.
   * @returns {boolean}
   */ const isExplicitEmptySlotPlaceholder = (sourceHeaderSettings)=>{
        if (sourceHeaderSettings === '') {
            return true;
        }
        if (!(0, _object.isObject)(sourceHeaderSettings)) {
            return false;
        }
        const { label, colspan, rowspan, headerClassName } = sourceHeaderSettings;
        return (0, _mixed.stringify)(label) === '' && (colspan === undefined || colspan === 1) && (rowspan === undefined || rowspan === 1) && headerClassName === undefined;
    };
    /**
   * Converts user-defined source settings to normalized shape.
   *
   * @param {*} sourceHeaderSettings The source header settings.
   * @returns {DefaultHeaderSettings}
   */ const normalizeHeaderSettings = (sourceHeaderSettings)=>{
        const headerSettings = (0, _utils.createDefaultHeaderSettings)();
        if ((0, _object.isObject)(sourceHeaderSettings)) {
            const { label, colspan, rowspan, headerClassName } = sourceHeaderSettings;
            headerSettings.label = (0, _mixed.stringify)(label);
            if (typeof colspan === 'number' && colspan > 1) {
                headerSettings.colspan = colspan;
                headerSettings.origColspan = colspan;
            }
            if (typeof rowspan === 'number' && rowspan > 1) {
                headerSettings.rowspan = rowspan;
                headerSettings.origRowspan = rowspan;
            }
            if (typeof headerClassName === 'string') {
                headerSettings.headerClassNames = [
                    ...headerClassName.split(' ')
                ];
            }
        } else {
            headerSettings.label = (0, _mixed.stringify)(sourceHeaderSettings);
        }
        return headerSettings;
    };
    // Normalize array items (header settings) into one shape - literal object with default props.
    (0, _array.arrayEach)(sourceSettings, (headersSettings = [])=>{
        const columns = [];
        let sourceSettingsIndex = 0;
        let visualColumnIndex = 0;
        normalizedSettings.push(columns);
        while(sourceSettingsIndex < headersSettings.length || rowspanCoverageMap[visualColumnIndex] > 0){
            if (visualColumnIndex >= columnsLimit) {
                break;
            }
            const sourceHeaderSettings = headersSettings[sourceSettingsIndex];
            const isCoveredByRowspan = rowspanCoverageMap[visualColumnIndex] > 0;
            if (isCoveredByRowspan) {
                if (isExplicitEmptySlotPlaceholder(sourceHeaderSettings)) {
                    sourceSettingsIndex += 1;
                }
                columns.push((0, _utils.createDefaultHeaderSettings)());
                rowspanCoverageMap[visualColumnIndex] -= 1;
                visualColumnIndex += 1;
                continue; // eslint-disable-line no-continue
            }
            if (sourceSettingsIndex >= headersSettings.length) {
                break;
            }
            const headerSettings = normalizeHeaderSettings(sourceHeaderSettings);
            let headerWidth = headerSettings.origColspan;
            if (visualColumnIndex + headerWidth >= columnsLimit) {
                // Adjust the colspan value to not overlap the columns limit.
                headerWidth -= visualColumnIndex + headerWidth - columnsLimit;
                headerSettings.colspan = headerWidth;
                headerSettings.origColspan = headerWidth;
            }
            if (headerSettings.origRowspan > 1) {
                for(let i = 0; i < headerWidth; i++){
                    rowspanCoverageMap[visualColumnIndex + i] = Math.max(rowspanCoverageMap[visualColumnIndex + i] ?? 0, headerSettings.origRowspan - 1);
                }
            }
            columns.push(headerSettings);
            if (headerSettings.colspan > 1) {
                for(let i = 0; i < headerSettings.colspan - 1; i++){
                    columns.push((0, _utils.createPlaceholderHeaderSettings)());
                }
            }
            visualColumnIndex += headerWidth;
            sourceSettingsIndex += 1;
        }
    });
    const columnsLength = Math.max(...(0, _array.arrayMap)(normalizedSettings, (headersSettings)=>headersSettings.length));
    // Normalize the length of each header layer to the same columns length.
    (0, _array.arrayEach)(normalizedSettings, (headersSettings)=>{
        if (headersSettings.length < columnsLength) {
            const defaultSettings = (0, _array.arrayMap)(new Array(columnsLength - headersSettings.length), ()=>(0, _utils.createDefaultHeaderSettings)());
            headersSettings.splice(headersSettings.length, 0, ...defaultSettings);
        }
    });
    return normalizedSettings;
}
