/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var findLastIndex = require('lodash/findLastIndex');
var find = require('lodash/find');
var amisCore = require('amis-core');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var findLastIndex__default = /*#__PURE__*/_interopDefaultLegacy(findLastIndex);
var find__default = /*#__PURE__*/_interopDefaultLegacy(find);

// 当前行包含子数据
function checkChildrenRow(data, childrenColumnName) {
    return (data[childrenColumnName] &&
        Array.isArray(data[childrenColumnName]) &&
        data[childrenColumnName].length > 0);
}
// 获取当前行数据所有子行的key值
function getDataChildrenKeys(data, childrenColumnName, rowSelectionKeyField) {
    var keys = [];
    if (checkChildrenRow(data, childrenColumnName)) {
        data[childrenColumnName].forEach(function (item) {
            return (keys = tslib.__spreadArray(tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(keys), false), tslib.__read(getDataChildrenKeys(item, childrenColumnName, rowSelectionKeyField)), false), [
                item[rowSelectionKeyField]
            ], false));
        });
    }
    return keys;
}
// 获取当前表格所有可以选中的行
function getAllSelectableRows(dataSource, rowSelectionKeyField, childrenColumnName, expandable, selectedRowKeys, maxSelectedLength) {
    var allRowKeys = [];
    var allRows = [];
    dataSource.forEach(function (data) {
        allRowKeys.push(data[rowSelectionKeyField]);
        allRows.push(data);
        if (!expandable && checkChildrenRow(data, childrenColumnName)) {
            allRowKeys = tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(allRowKeys), false), tslib.__read(getDataChildrenKeys(data, childrenColumnName, rowSelectionKeyField)), false);
            data[childrenColumnName].forEach(function (item) { return allRows.push(item); });
        }
    });
    var restSelectedKeys = selectedRowKeys === null || selectedRowKeys === void 0 ? void 0 : selectedRowKeys.filter(function (key) { return !allRowKeys.includes(key); });
    if (maxSelectedLength && Number.isInteger(maxSelectedLength)) {
        if (restSelectedKeys.length + allRowKeys.length > maxSelectedLength) {
            var count = maxSelectedLength - restSelectedKeys.length;
            allRowKeys = allRowKeys.slice(0, count);
            allRows = allRows.slice(0, count);
        }
    }
    return { rows: allRows, rowKeys: allRowKeys, restSelectedKeys: restSelectedKeys };
}
function getRowsByKeys(dataSource, keys, keyField, childrenColumnName) {
    var selectedRows = [];
    var unSelectedRows = [];
    dataSource.forEach(function (data) {
        if (keys.find(function (key) { return key === data[keyField]; })) {
            selectedRows.push(data);
        }
        else {
            unSelectedRows.push(data);
        }
        if (checkChildrenRow(data, childrenColumnName)) {
            var childrenResult = getRowsByKeys(data[childrenColumnName], keys, keyField, childrenColumnName);
            selectedRows = tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(selectedRows), false), tslib.__read(childrenResult.selectedRows), false);
            unSelectedRows = tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(unSelectedRows), false), tslib.__read(childrenResult.unSelectedRows), false);
        }
    });
    return { selectedRows: selectedRows, unSelectedRows: unSelectedRows };
}
function getMaxLevelThRowSpan(columns) {
    var maxLevel = 0;
    Array.isArray(columns) &&
        columns.forEach(function (c) {
            var level = getThRowSpan(c);
            if (maxLevel < level) {
                maxLevel = level;
            }
        });
    return maxLevel;
}
function getThRowSpan(column) {
    if (!column.children || (column.children && !column.children.length)) {
        return 1;
    }
    return 1 + getMaxLevelThRowSpan(column.children);
}
function getThColSpan(column) {
    if (!column.children || (column.children && !column.children.length)) {
        return 1;
    }
    var childrenLength = 0;
    column.children.forEach(function (item) { return (childrenLength += getThColSpan(item)); });
    return childrenLength;
}
function buildColumns(columns, thColumns, tdColumns, maxLevel, depth, fixed) {
    if (columns === void 0) { columns = []; }
    if (tdColumns === void 0) { tdColumns = []; }
    if (depth === void 0) { depth = 0; }
    // 在处理表头时，如果父级column设置了fixed属性，那么所有children保持一致
    Array.isArray(columns) &&
        columns.forEach(function (column) {
            var childMaxLevel = 0;
            if (column.children) {
                childMaxLevel = getMaxLevelThRowSpan(column.children);
            }
            var newColumn = tslib.__assign(tslib.__assign({}, column), { rowSpan: childMaxLevel ? 1 : maxLevel - depth, colSpan: getThColSpan(column), depth: depth });
            var tdColumn = tslib.__assign({}, column);
            if (fixed) {
                newColumn.fixed = fixed;
                tdColumn.fixed = fixed;
            }
            if (!thColumns[depth]) {
                thColumns[depth] = [];
            }
            thColumns[depth].push(newColumn);
            if (column.children && column.children.length > 0) {
                buildColumns(column.children, thColumns, tdColumns, maxLevel, depth + 1, column.fixed);
            }
            else {
                tdColumn.children; var rest = tslib.__rest(tdColumn, ["children"]);
                tdColumns.push(rest);
            }
        });
}
function getBuildColumns(columns) {
    var filterColumns = columns.filter(function (item) { return !item.breakpoint || !amisCore.isBreakpoint(item.breakpoint); });
    var thColumns = [];
    var tdColumns = [];
    buildColumns(filterColumns, thColumns, tdColumns, getMaxLevelThRowSpan(filterColumns));
    return { thColumns: thColumns, tdColumns: tdColumns };
}
function isFixedLeftColumn(fixed) {
    return fixed === true || fixed === 'left';
}
function isFixedRightColumn(fixed) {
    return fixed === 'right';
}
function getPreviousLeftWidth(doms, index, columns) {
    var width = 0;
    for (var i = 0; i < index; i++) {
        if (columns && columns[i] && isFixedLeftColumn(columns[i].fixed)) {
            var dom = doms[i];
            width += dom.offsetWidth;
        }
    }
    return width;
}
function getAfterRightWidth(doms, index, columns) {
    var width = 0;
    for (var i = doms.length - 0; i > index; i--) {
        if (columns && columns[i] && isFixedRightColumn(columns[i].fixed)) {
            var dom = doms[i];
            if (dom) {
                width += dom.offsetWidth;
            }
        }
    }
    return width;
}
// 更新一个tr下的td的left和class
function updateFixedRow(row, columns, cx) {
    var _a, _b, _c;
    var children = (row === null || row === void 0 ? void 0 : row.children) || [];
    for (var i = 0; i < children.length; i++) {
        var dom = children[i];
        dom.style.removeProperty('left');
        dom.style.removeProperty('right');
        var fixed = columns[i] ? columns[i].fixed || '' : '';
        if (isFixedLeftColumn(fixed)) {
            dom.style.left =
                i > 0 ? getPreviousLeftWidth(children, i, columns) + 'px' : '0';
        }
        else if (isFixedRightColumn(fixed)) {
            dom.style.right =
                i < children.length - 1
                    ? getAfterRightWidth(children, i, columns) + 'px'
                    : '0';
        }
        dom.classList.remove(cx('Table-cell-fix-left-last'));
        dom.classList.remove(cx('Table-cell-fix-right-first'));
        dom.classList.remove(cx('Table-cell-fix-right-first-prev'));
    }
    // 最后一个左fixed的添加样式
    var leftIndex = findLastIndex__default["default"](columns, function (column) {
        return isFixedLeftColumn(column.fixed);
    });
    if (leftIndex > -1) {
        (_a = children[leftIndex]) === null || _a === void 0 ? void 0 : _a.classList.add(cx('Table-cell-fix-left-last'));
    }
    // 第一个右fixed的添加样式
    var rightIndex = columns.findIndex(function (column) {
        return isFixedRightColumn(column.fixed);
    });
    if (rightIndex > -1) {
        (_b = children[rightIndex]) === null || _b === void 0 ? void 0 : _b.classList.add(cx('Table-cell-fix-right-first'));
        if (rightIndex > 0) {
            (_c = children[rightIndex - 1]) === null || _c === void 0 ? void 0 : _c.classList.add(cx('Table-cell-fix-right-first-prev'));
        }
    }
}
function hasFixedColumn(columns) {
    return find__default["default"](columns, function (column) { return column.fixed; });
}
function levelsSplit(level) {
    if (!level) {
        return [];
    }
    return level.split(',').map(function (l) { return +l; });
}
function getSortData(data, columns, childrenColumnName, sort) {
    var cloneData = data.slice();
    if (!(sort === null || sort === void 0 ? void 0 : sort.orderBy)) {
        return cloneData;
    }
    var column = columns.find(function (column) { return column.name === sort.orderBy; });
    if (!column) {
        return cloneData;
    }
    if (typeof column.sorter !== 'function') {
        return cloneData;
    }
    var sortOrder = sort.orderDir;
    return cloneData
        .sort(function (record1, record2) {
        var compareResult = typeof column.sorter === 'function'
            ? column.sorter(record1, record2, sortOrder)
            : 0;
        if (compareResult !== 0) {
            return sortOrder === 'asc' ? compareResult : -compareResult;
        }
        return 0;
    })
        .map(function (record) {
        var _a;
        var subRecords = record[childrenColumnName];
        if (subRecords) {
            return tslib.__assign(tslib.__assign({}, record), (_a = {}, _a[childrenColumnName] = getSortData(data, columns, childrenColumnName, sort), _a));
        }
        return record;
    });
}

exports.buildColumns = buildColumns;
exports.checkChildrenRow = checkChildrenRow;
exports.getAllSelectableRows = getAllSelectableRows;
exports.getBuildColumns = getBuildColumns;
exports.getDataChildrenKeys = getDataChildrenKeys;
exports.getMaxLevelThRowSpan = getMaxLevelThRowSpan;
exports.getRowsByKeys = getRowsByKeys;
exports.getSortData = getSortData;
exports.getThColSpan = getThColSpan;
exports.getThRowSpan = getThRowSpan;
exports.hasFixedColumn = hasFixedColumn;
exports.levelsSplit = levelsSplit;
exports.updateFixedRow = updateFixedRow;
