import { hasValue } from '../../utils/utils';
export const ALL_ROWS_KEY = 'all-rows';
export var EditControl;
(function (EditControl) {
    EditControl["save"] = "save";
    EditControl["cancel"] = "cancel";
})(EditControl || (EditControl = {}));
export const EditControls = Object.values(EditControl);
export var RowControl;
(function (RowControl) {
    RowControl["update"] = "update";
    RowControl["delete"] = "delete";
})(RowControl || (RowControl = {}));
export const RowControls = Object.values(RowControl);
export var TableClasses;
(function (TableClasses) {
    TableClasses["body"] = "anj-table-body";
    TableClasses["caption"] = "anj-column-caption";
    TableClasses["detail"] = "anj-row-detail";
    TableClasses["detailCell"] = "anj-row-detail-cell";
    TableClasses["detailRow"] = "anj-detail-row";
    TableClasses["detailWrapper"] = "anj-row-detail-wrapper";
    TableClasses["editCell"] = "anj-form-cell";
    TableClasses["header"] = "anj-table-head";
    TableClasses["row"] = "anj-row";
    TableClasses["rowGroupHeader"] = "anj-row-group-header";
    TableClasses["table"] = "anj-table";
})(TableClasses || (TableClasses = {}));
export const calculateOffsets = (cols) => {
    const offsets = {};
    cols.forEach((col, index) => {
        let offset;
        switch (col.pinned) {
            case 'left':
                offset = columnOffset(cols.slice(0, index));
                break;
            case 'right':
                offset = columnOffset(cols.slice(index + 1, cols.length).reverse());
                break;
            default:
                offset = undefined;
        }
        offsets[index] = !isNaN(offset) ? `${offset}px` : '';
    });
    return offsets;
};
export const columnOffset = (cols) => {
    if (cols.length === 0) {
        return 0;
    }
    return cols.map(col => parseInt(col.width, 10)).reduce((total = 0, width) => total + width);
};
export const calculatePinBoundaries = (leftCount, rightCount, scrollCount) => {
    return {
        lastLeftPinned: leftCount > 0 ? leftCount - 1 : undefined,
        firstRightPinned: rightCount > 0 ? leftCount + scrollCount : undefined
    };
};
export const keyFromData = (key) => {
    return typeof key === 'string' && !isNaN(key) ? parseInt(key, 10) : key;
};
export const colCellsFromCell = (el) => {
    const colClass = el.classList.value.split(' ').find(c => c.includes('anj-col-'));
    return Array.from(el.closest('table').querySelectorAll(`.${colClass}`));
};
export const columnFromCell = (columns, cell) => {
    const index = cell.dataset.columnIndex;
    return columns[index];
};
export const implicitColumnFactory = (fields, data) => {
    return fields.map(field => {
        const firstWithValue = data.find(d => hasValue(d[field]));
        return {
            field,
            dataType: inferDataType(firstWithValue[field])
        };
    });
};
export const inferDataType = (value) => {
    if (typeof value === 'boolean') {
        return 'boolean';
    }
    if (typeof value === 'number') {
        return 'number';
    }
    if (new Date(value).valueOf()) {
        return 'date';
    }
    return 'string';
};
export const persistColumn = (column) => {
    const whitelist = ['filter', 'groupIndex', 'groupKeys', 'index', 'name', 'pinned', 'sortIndex', 'sortOrder', 'visible', 'width'];
    const persistent = {};
    whitelist.forEach(k => {
        persistent[k] = column[k];
    });
    return persistent;
};
