"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destructurePivotColumnId = destructurePivotColumnId;
// supports only pivotTotalColumns (aggregation total) and pivotGroupTotal
// AG Grid builds the ID as: `pivot_${pivotCols.join('-')}_${pivotKeys.join('-')}_${measureColumnId}`
// see https://github.com/ag-grid/ag-grid/blob/e0cfe533b55b75cdc148cdfb1a4e977731dc0712/packages/ag-grid-enterprise/src/pivot/pivotColDefService.ts#L454C16-L454C88
function destructurePivotColumnId(colDef, currentModel, logWarning) {
    const { colId } = colDef;
    // Basic validation
    if (!colId?.startsWith('pivot_')) {
        logWarning(`Column id must start with 'pivot_': ${colId}`);
        return '!unknown!';
    }
    const hasSpecialChars = (arr) => arr.some((str) => str.includes('-') || str.includes('_'));
    // AG Grid uses _ and - as delimiters
    // if we have these special characters in the column IDs, we need to parse them differently
    const colIdsOrKeysContainSpecialChars = hasSpecialChars(currentModel.pivotColIds) ||
        hasSpecialChars(currentModel.aggColIds) ||
        hasSpecialChars(colDef.pivotKeys || []);
    // Fast path - no special characters
    if (!colIdsOrKeysContainSpecialChars) {
        // Split by underscore to get 4 parts
        const parts = colId.split('_');
        if (parts.length !== 4) {
            logWarning(`Unsupported format of pivot total column id: ${colId}`);
            return '!unknown!';
        }
        const [_pivotPrefix, pivotColsTxt, pivotKeysTxt, aggregationColumnId] = parts;
        const pivotColumnIds = pivotColsTxt.split('-');
        const pivotKeys = pivotKeysTxt.split('-');
        const pivotColumnId = pivotColumnIds[pivotKeys.length - 1];
        return {
            pivotColumnIds,
            pivotKeys,
            pivotColumnId,
            aggregationColumnId: aggregationColumnId !== '' ? aggregationColumnId : undefined,
        };
    }
    // the complex path, where we have to handle the special characters
    // Remove 'pivot_' prefix
    const withoutPrefix = colId.slice(6);
    // Check if it's a pivot group total
    const isPivotGroupTotal = withoutPrefix.endsWith('_');
    // Get pivot keys from colDef (more reliable than string parsing)
    const pivotKeys = colDef.pivotKeys || [];
    if (isPivotGroupTotal) {
        // Remove trailing underscore for pivot group totals
        const content = withoutPrefix.slice(0, -1);
        // The remaining content should be the pivot columns
        const pivotColumnIds = currentModel.pivotColIds.filter((id) => content.includes(id));
        if (!pivotColumnIds.length) {
            logWarning(`Could not identify pivot columns in: ${content}`);
            return '!unknown!';
        }
        return {
            pivotColumnIds,
            pivotKeys,
            pivotColumnId: pivotColumnIds[pivotKeys.length],
        };
    }
    // For regular pivot columns, work backwards to find aggregation column
    const parts = withoutPrefix.split('_');
    const lastPart = parts[parts.length - 1];
    // Find the longest matching aggregation column id from the end
    const aggregationColumnId = currentModel.aggColIds.find((aggId) => lastPart.endsWith(aggId));
    if (!aggregationColumnId) {
        logWarning(`Could not identify aggregation column in: ${lastPart}`);
        return '!unknown!';
    }
    // Remove aggregation part and get pivot columns
    const withoutAgg = withoutPrefix.slice(0, -(aggregationColumnId.length + 1));
    const pivotColumnIds = currentModel.pivotColIds.filter((id) => withoutAgg.includes(id));
    if (!pivotColumnIds.length) {
        logWarning(`Could not identify pivot columns in: ${withoutAgg}`);
        return '!unknown!';
    }
    return {
        pivotColumnIds,
        pivotKeys,
        pivotColumnId: pivotColumnIds[pivotKeys.length - 1],
        aggregationColumnId,
    };
}
