/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { getColWidthAttr } from './utils.mjs';
import '../../../_chunks/dep-5717e263.mjs';
import '../../../_chunks/dep-eba0ea6e.mjs';
import '../../../_chunks/dep-469b4ae0.mjs';
import '../../../_chunks/dep-255303ba.mjs';
import '../../../_chunks/dep-a8bcacea.mjs';
import '../../../_chunks/dep-3f21269a.mjs';
import '../../../_chunks/dep-3da99438.mjs';

var setMissingColumnWidth = function setMissingColumnWidth(columns, missingWidthCols, thWidthList, tableLayout, actualWidth, tableWidth) {
  var thMap = thWidthList;
  if (actualWidth < tableWidth) {
    var widthDiff = tableWidth - actualWidth;
    var remainCols = [];
    missingWidthCols.forEach(function (col) {
      var minWidth = getColWidthAttr(col, "minWidth");
      if (minWidth) {
        thMap[col.colKey] = minWidth;
        widthDiff -= minWidth;
      } else {
        remainCols.push(col);
      }
    });
    if (widthDiff > 0) {
      if (remainCols.length) {
        var avgWidth = widthDiff / remainCols.length;
        remainCols.forEach(function (col) {
          thMap[col.colKey] = avgWidth;
        });
      } else {
        var _avgWidth = widthDiff / missingWidthCols.length;
        missingWidthCols.forEach(function (col) {
          thMap[col.colKey] += _avgWidth;
        });
      }
    } else {
      remainCols.forEach(function (col) {
        thMap[col.colKey] = 100;
      });
    }
  } else if (tableLayout === "fixed") {
    missingWidthCols.forEach(function (col) {
      thMap[col.colKey] = getColWidthAttr(col, "minWidth") || 100;
    });
  } else {
    var extraWidth = 0;
    missingWidthCols.forEach(function (col) {
      extraWidth += getColWidthAttr(col, "minWidth") || 100;
    });
    var totalWidth = extraWidth + actualWidth;
    columns.forEach(function (col) {
      if (!thMap[col.colKey]) {
        var colWidth = getColWidthAttr(col, "minWidth") || 100;
        thMap[col.colKey] = colWidth / totalWidth * tableWidth;
      } else {
        thMap[col.colKey] = thMap[col.colKey] / totalWidth * tableWidth;
      }
    });
  }
};
var setNormalColumnWidth = function setNormalColumnWidth(columns, thWidthList, actualWidth, tableWidth, notCalculateWidthCols) {
  var thMap = thWidthList;
  columns.forEach(function (col) {
    if (notCalculateWidthCols.includes(col.colKey)) return;
    thMap[col.colKey] = thMap[col.colKey] / actualWidth * tableWidth;
  });
};
var setInitialColumnWidth = function setInitialColumnWidth(missingWidthCols, thWidthList) {
  var thMap = thWidthList;
  missingWidthCols.forEach(function (col) {
    thMap[col.colKey] = getColWidthAttr(col, "minWidth") || 100;
  });
};
function recalculateColumnWidth(columns, thWidthList, tableLayout, tableElmWidth, notCalculateWidthCols, callback) {
  var actualWidth = 0;
  var missingWidthCols = [];
  var thMap = {};
  columns.forEach(function (col) {
    if (!thWidthList[col.colKey]) {
      thMap[col.colKey] = getColWidthAttr(col, "width");
    } else {
      thMap[col.colKey] = thWidthList[col.colKey];
    }
    var originWidth = thMap[col.colKey];
    if (originWidth) {
      actualWidth += originWidth;
    } else {
      missingWidthCols.push(col);
    }
  });
  var tableWidth = tableElmWidth;
  var needUpdate = false;
  if (tableWidth > 0) {
    if (missingWidthCols.length) {
      setMissingColumnWidth(columns, missingWidthCols, thMap, tableLayout, actualWidth, tableWidth);
      needUpdate = true;
    } else {
      if (notCalculateWidthCols.length) {
        var sum = 0;
        notCalculateWidthCols.forEach(function (colKey) {
          sum += thMap[colKey];
        });
        actualWidth -= sum;
        tableWidth -= sum;
      }
      if (actualWidth !== tableWidth || notCalculateWidthCols.length) {
        setNormalColumnWidth(columns, thMap, actualWidth, tableWidth, notCalculateWidthCols);
        needUpdate = true;
      }
    }
  } else {
    setInitialColumnWidth(missingWidthCols, thMap);
    needUpdate = true;
  }
  if (needUpdate) {
    var addon = 0;
    Object.keys(thMap).forEach(function (key) {
      var width = thMap[key];
      addon += width - Math.floor(width);
      thMap[key] = Math.floor(width) + (addon > 1 ? 1 : 0);
      if (addon > 1) {
        addon -= 1;
      }
    });
    if (addon > 0.5) {
      thMap[columns[0].colKey] += 1;
    }
  }
  callback(thMap);
}

export { recalculateColumnWidth as default };
//# sourceMappingURL=recalculate-column-width.mjs.map
