"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.tableWidthKey = exports.autoFillTableWidth = exports.FILL_COLUMN_CODE = void 0;

var _symbol = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/symbol"));

var _maxSafeInteger = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/number/max-safe-integer"));

var _find = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find"));

var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));

var _splice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/splice"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/map"));

var _findIndex = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/find-index"));

var _calculations = require("../../base/calculations");

var _utils = require("../../utils");

var _styles = require("../../base/styles");

var _columnResizeWidth = require("./columnResizeWidth");

var FILL_COLUMN_CODE = '$_fill_column_&';
exports.FILL_COLUMN_CODE = FILL_COLUMN_CODE;
var tableWidthKey = 'tableWidth';
exports.tableWidthKey = tableWidthKey;
var FLEX_COLUMN_COUNT = (0, _symbol.default)('flexCount');

var autoFillTableWidth = function autoFillTableWidth() {
  return function (pipeline) {
    var flexColumnResult = findFlexColumns(pipeline);
    var flexCount = flexColumnResult.get(FLEX_COLUMN_COUNT);

    if (flexCount) {
      // 设置了flex宽度，flex列平分剩余宽度
      var remainingWidth = getTableRemainingWidth(pipeline) || 0;

      if (remainingWidth > 0) {
        // 保存剩余的flex总和和剩余宽度总和宽度
        var residualFlexCount = flexCount;
        var residualFlexWidth = remainingWidth;
        var columnSize = pipeline.getFeatureOptions(_columnResizeWidth.COLUMN_SIZE_KEY);
        var enableColumnResizeWidthFeature = !!columnSize;
        pipeline.mapColumns((0, _utils.makeRecursiveMapper)(function (col, recursiveFlatMapInfo) {
          var isLeaf = recursiveFlatMapInfo.isLeaf;

          if (isLeaf && isValidFlexColumn(col, pipeline)) {
            var code = col.code,
                _col$features = col.features,
                features = _col$features === void 0 ? {} : _col$features;
            var flex = features.flex,
                _features$minWidth = features.minWidth,
                minWidth = _features$minWidth === void 0 ? 0 : _features$minWidth,
                _features$maxWidth = features.maxWidth,
                maxWidth = _features$maxWidth === void 0 ? _maxSafeInteger.default : _features$maxWidth;
            var usedRemainingWidth = Math.floor(remainingWidth * flex / flexCount);
            var preColWidth = col.width; // 如果当前已经是最后一个flex列，将剩余的宽度添加到该列，其他计算相应的列

            col.width = clamp(minWidth, preColWidth + (residualFlexCount === flex ? residualFlexWidth : usedRemainingWidth), maxWidth);
            residualFlexCount -= flex;
            residualFlexWidth -= col.width - preColWidth;

            if (enableColumnResizeWidthFeature) {
              columnSize[code] = col.width;
            }
          }

          return col;
        }));
        enableColumnResizeWidthFeature && pipeline.setFeatureOptions(_columnResizeWidth.COLUMN_SIZE_KEY, columnSize);
      }
    } else {
      // 未设置了flex宽度，创建占位列
      var columns = pipeline.getColumns();
      var fillColumns = (0, _find.default)(columns).call(columns, function (col) {
        return col.code === FILL_COLUMN_CODE;
      });
      var width = getTableRemainingWidth(pipeline) || 0;

      if (fillColumns) {
        fillColumns.width = width;
      } else {
        var rightNestedLockCount = (0, _calculations.getLeftNestedLockCount)((0, _slice.default)(columns).call(columns).reverse());
        var spliceIndex = columns.length - rightNestedLockCount;
        var _fillColumns = {
          name: '',
          code: FILL_COLUMN_CODE,
          width: width,
          features: {
            resizeable: false
          },
          getCellProps: function getCellProps(value, record, rowIndex) {
            return {
              className: _styles.Classes.emptyColCell
            };
          }
        };
        (0, _splice.default)(columns).call(columns, spliceIndex || columns.length, 0, _fillColumns);
      }

      pipeline.columns(columns);
    }

    return pipeline;

    function findFlexColumns(pipeline) {
      var result = new _map.default([[FLEX_COLUMN_COUNT, 0]]);
      dfs(pipeline.getColumns(), result);
      return result;

      function dfs(columns, result) {
        columns.forEach(function (col) {
          if ((0, _utils.isLeafNode)(col)) {
            if (isValidFlexColumn(col, pipeline)) {
              result.set(FLEX_COLUMN_COUNT, result.get(FLEX_COLUMN_COUNT) + col.features.flex);
            }
          } else {
            dfs(col.children, result);
          }
        });
      }
    }
  };
};

exports.autoFillTableWidth = autoFillTableWidth;

function getColumnWidthSum(pipeline) {
  return dfs(pipeline.getColumns());

  function dfs(columns) {
    return columns.reduce(function (acc, col) {
      var width = col.width,
          code = col.code;

      if ((0, _utils.isLeafNode)(col) && code !== FILL_COLUMN_CODE) {
        var resizeColumn = pipeline.getFeatureOptions(_columnResizeWidth.COLUMN_SIZE_KEY);
        return acc + (resizeColumn && resizeColumn[code] || width);
      } else {
        return acc + dfs(col.children);
      }
    }, 0);
  }
}

function getTableRemainingWidth(pipeline) {
  var _a, _b;

  var tableWidth = ((_b = (_a = pipeline.ref.current.domHelper) === null || _a === void 0 ? void 0 : _a.tableBody) === null || _b === void 0 ? void 0 : _b.clientWidth) || pipeline.getStateAtKey(tableWidthKey);
  if (!tableWidth) return;
  var remainingWidth = Math.floor(tableWidth - getColumnWidthSum(pipeline));
  return remainingWidth > 0 ? remainingWidth : 0;
}

function isAfterLastResizeCol(column, pipeline) {
  var _context, _context2;

  var lastResizedColumnCode = pipeline.getFeatureOptions(_columnResizeWidth.LAST_RESIZED_COLUMN_KEY);
  if (lastResizedColumnCode === undefined) return true;
  var lastResizedColumnIndex = (0, _findIndex.default)(_context = pipeline.getColumns()).call(_context, function (col) {
    return col.code === lastResizedColumnCode;
  });
  var colIndex = (0, _findIndex.default)(_context2 = pipeline.getColumns()).call(_context2, function (col) {
    return col.code === column.code;
  });
  return colIndex > lastResizedColumnIndex;
}

function isValidFlexColumn(col, pipeline) {
  var _a;

  var resizeColumn = pipeline.getFeatureOptions(_columnResizeWidth.RESIZED_COLUMN_KEY); // 拖拽列自动禁止flex

  if (resizeColumn === null || resizeColumn === void 0 ? void 0 : resizeColumn.has(col.code)) {
    return false;
  }

  var flex = (_a = col.features) === null || _a === void 0 ? void 0 : _a.flex;
  return typeof flex === 'number' && flex > 0 && isAfterLastResizeCol(col, pipeline);
}

function clamp(min, x, max) {
  return Math.max(min, Math.min(max, x));
}