import { PREFIX_CLS } from "./constant";

/*
 * @clsName
 * @desc  get class name
 * @param {string} cls - class
 */
export function clsName(cls) {
    return PREFIX_CLS + cls;
}

/*
 * @getRowKey
 * @desc  get row key
 * @param {Object} rowData - rowData
 * @param {string} rowKeyFieldName - row key field name
 */
export function getRowKey(rowData, rowKeyFieldName) {
    let result = null;

    if (rowData && rowKeyFieldName) {
        result = rowData[rowKeyFieldName];
    }
    return result;
}

/*
 * @getFixedTotalWidthByColumnKey
 * @desc  get fixed total width by column key
 * @param {object} colgroups - 列信息
 * @param {any} columnKey - column key
 * @param {string} fixed - left|right
 */
export function getFixedTotalWidthByColumnKey(colgroups, columnKey, fixed) {
    const currentIndex = colgroups.findIndex(x => x.key === columnKey);

    let result = 0;

    if (fixed === "left") {
        // 只计算左列固定的
        result = colgroups.reduce((total, currentVal, index) => {
            return index < currentIndex && currentVal.fixed == "left"
                ? currentVal._realTimeWidth + total
                : total;
        }, 0);
    } else if (fixed === "right") {
        // 只计算右列固定的
        result = colgroups.reduce((total, currentVal, index) => {
            return index > currentIndex && currentVal.fixed == "right"
                ? currentVal._realTimeWidth + total
                : total;
        }, 0);
    }

    return result;
}

/*
 * @getNotFixedTotalWidthByColumnKey
 * @desc  get not fixed total width by column key
 * @param {object} colgroups - 列信息
 * @param {any} columnKey - column key
 * @param {string} direction - left|right
 */
export function getNotFixedTotalWidthByColumnKey({
    colgroups,
    columnKey,
    direction
}) {
    const currentIndex = colgroups.findIndex(x => x.key === columnKey);

    let result = 0;

    if (direction === "left") {
        // 只计算左侧非固定列
        result = colgroups.reduce((total, currentVal, index) => {
            return index < currentIndex && !currentVal.fixed
                ? currentVal._realTimeWidth + total
                : total;
        }, 0);
    } else if (direction === "right") {
        // 只计算右侧非固定列
        result = colgroups.reduce((total, currentVal, index) => {
            return index > currentIndex && !currentVal.fixed
                ? currentVal._realTimeWidth + total
                : total;
        }, 0);
    }

    return result;
}

/*
 * @initGroupColumns
 * @desc  int group columns
 * @param {array} cloneColumns - clone columns
 * @return {
   isGroupHeader,
   colgroups,
   groupColumns
}
 */
export function initGroupColumns(cloneColumns) {
    let colgroups = [];
    let groupColumns = [];

    // set column level
    let maxLevel = 1;
    const setColumnLevel = (column, parent) => {
        if (parent) {
            column._level = parent._level + 1;
            if (maxLevel < column._level) {
                maxLevel = column._level;
            }
        }
        if (column.children) {
            column.children.forEach(item => {
                item.fixed = column.fixed;
                setColumnLevel(item, column);
            });
        }
    };
    cloneColumns.forEach(column => {
        column._level = 1;
        setColumnLevel(column);
    });

    // set colspan and rowspan and keys
    const setColspanAndRowspanAndKeys = column => {
        if (column.children) {
            let keys = "";
            let colspan = 0;
            column.children.forEach((item, itemIndex) => {
                setColspanAndRowspanAndKeys(item);

                colspan += item._colspan;
                keys += item._keys.endsWith("|")
                    ? item._keys
                    : item._keys + "|";
            });
            column._keys = keys;
            column._colspan = colspan;
            column._rowspan = 1;
        } else {
            column._keys = column.key;
            column._colspan = 1;
            column._rowspan = maxLevel - column._level + 1;
        }
    };

    cloneColumns.forEach((column, index) => {
        setColspanAndRowspanAndKeys(column);
    });

    // init groupColumns
    for (let i = 0; i < maxLevel; i++) {
        groupColumns.push([]);
    }
    // set colgroups and groupColumns
    const setColgroupsAndGroupColumns = column => {
        // set groupColumns
        const { children, ...groupColumn } = column;
        groupColumns[column._level - 1].push(groupColumn);

        if (column.children) {
            column.children.forEach(item => {
                setColgroupsAndGroupColumns(item);
            });
        } else {
            // set colgroups
            const { children, ...colgroup } = column;
            colgroup._realTimeWidth = colgroup.width;
            colgroups.push(colgroup);
        }
    };

    cloneColumns.forEach(column => {
        setColgroupsAndGroupColumns(column);
    });

    return {
        // set is group header
        isGroupHeader: maxLevel > 1,
        // set colgroups
        colgroups,
        // set groupColumns
        groupColumns
    };
}

@@ -488,32 +488,475 @@
 turn result;%0A%7D%0A%0A
+/*%0A * @getDomResizeObserverCompKey%0A * @desc  get dom resize observer comp key%0A * @param %7BAny%7D originalKey - original key%0A * @param %7BNumber%7D columnsOptionResetTime - columns option change time%0A */%0Aexport function getDomResizeObserverCompKey(%0A    originalKey,%0A    columnsOptionResetTime%0A) %7B%0A    let result = originalKey;%0A%0A    if (result %7C%7C result == 0) %7B%0A        result = originalKey + %22@%22 + columnsOptionResetTime;%0A    %7D%0A%0A    return result;%0A%7D%0A%0A
 /*%0A * @getFixedT

add util method:getDomResizeObserverCompKey