import { MOBILE_TABLE_MIN_COLUMN_WIDTH, TABLE_EDIT_COL_WIDTH_MIN_COLUMNS } from "../../../../../Constants/mobile";
import { getReadonlyTableColWidths } from "./getTableColWidths";
var TABLE_EDIT_DESKTOP_MIN_COLUMN_WIDTH = 60;
var clamp = function clamp(value, min, max) {
    return Math.min(Math.max(value, min), max);
};
var toPixelWidth = function toPixelWidth(width, containerWidth, fallbackWidth) {
    if (typeof width === 'number') return width;
    if (!width) return fallbackWidth;
    if (width.endsWith('%')) {
        var ratio = Number.parseFloat(width);
        if (Number.isFinite(ratio)) return containerWidth * ratio / 100;
    }
    var parsed = Number.parseFloat(width);
    return Number.isFinite(parsed) ? parsed : fallbackWidth;
};
/** 统一编辑态列宽策略：显式列宽优先，其他场景复用只读算法并归一化为像素值 */ export var getEditableTableColWidths = function getEditableTableColWidths(param) {
    var readonly = param.readonly, columnCount = param.columnCount, availableTableWidth = param.availableTableWidth, mobileBreakpointValue = param.mobileBreakpointValue, element = param.element;
    var _element_children, _element_otherProps;
    if (readonly) return [];
    if (!(element === null || element === void 0 ? void 0 : (_element_children = element.children) === null || _element_children === void 0 ? void 0 : _element_children.length)) return [];
    if (columnCount < TABLE_EDIT_COL_WIDTH_MIN_COLUMNS) return [];
    var explicitColWidths = (_element_otherProps = element.otherProps) === null || _element_otherProps === void 0 ? void 0 : _element_otherProps.colWidths;
    if (explicitColWidths === null || explicitColWidths === void 0 ? void 0 : explicitColWidths.length) {
        return explicitColWidths.slice(0, columnCount).map(function(width) {
            return Math.max(1, Math.round(toPixelWidth(width, availableTableWidth, TABLE_EDIT_DESKTOP_MIN_COLUMN_WIDTH)));
        });
    }
    var isMobileLayout = availableTableWidth <= mobileBreakpointValue;
    var minColumnWidth = isMobileLayout ? MOBILE_TABLE_MIN_COLUMN_WIDTH : TABLE_EDIT_DESKTOP_MIN_COLUMN_WIDTH;
    var maxColumnWidth = isMobileLayout ? availableTableWidth : Math.max(TABLE_EDIT_DESKTOP_MIN_COLUMN_WIDTH, availableTableWidth / 4);
    var fallbackWidth = Math.max(minColumnWidth, Math.floor(availableTableWidth / Math.max(columnCount, 1)));
    var sourceColWidths = getReadonlyTableColWidths({
        columnCount: columnCount,
        element: element,
        containerWidth: availableTableWidth
    });
    var normalizedWidths = Array.from({
        length: columnCount
    }, function(_, index) {
        return clamp(toPixelWidth(sourceColWidths[index], availableTableWidth, fallbackWidth), minColumnWidth, maxColumnWidth);
    });
    var totalWidth = normalizedWidths.reduce(function(sum, width) {
        return sum + width;
    }, 0);
    if (totalWidth > availableTableWidth) {
        return Array(columnCount).fill(fallbackWidth);
    }
    return normalizedWidths;
};
export var getEditableTableMinWidth = function getEditableTableMinWidth(param) {
    var columnCount = param.columnCount, colWidths = param.colWidths, availableTableWidth = param.availableTableWidth, mobileBreakpointValue = param.mobileBreakpointValue, resolvedContentWidth = param.resolvedContentWidth, minContainerWidth = param.minContainerWidth, rowIndexColWidth = param.rowIndexColWidth;
    var isMobileLayout = availableTableWidth <= mobileBreakpointValue;
    var minColumnWidth = isMobileLayout ? MOBILE_TABLE_MIN_COLUMN_WIDTH : TABLE_EDIT_DESKTOP_MIN_COLUMN_WIDTH;
    var colWidthsTotal = colWidths.reduce(function(total, width) {
        return total + width;
    }, 0);
    var fallbackMinWidth = Number((Math.max(resolvedContentWidth, minContainerWidth) * 0.95).toFixed(0));
    return Math.max(columnCount * minColumnWidth, colWidthsTotal + rowIndexColWidth, fallbackMinWidth, minContainerWidth);
};
