import { j as hasValue } from './utils-3aef1637.js';

const ALL_ROWS_KEY = 'all-rows';
var EditControl;
(function (EditControl) {
    EditControl["save"] = "save";
    EditControl["cancel"] = "cancel";
})(EditControl || (EditControl = {}));
const EditControls = Object.values(EditControl);
var RowControl;
(function (RowControl) {
    RowControl["update"] = "update";
    RowControl["delete"] = "delete";
})(RowControl || (RowControl = {}));
const RowControls = Object.values(RowControl);
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 = {}));
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;
};
const columnOffset = (cols) => {
    if (cols.length === 0) {
        return 0;
    }
    return cols.map(col => parseInt(col.width, 10)).reduce((total = 0, width) => total + width);
};
const calculatePinBoundaries = (leftCount, rightCount, scrollCount) => {
    return {
        lastLeftPinned: leftCount > 0 ? leftCount - 1 : undefined,
        firstRightPinned: rightCount > 0 ? leftCount + scrollCount : undefined
    };
};
const keyFromData = (key) => {
    return typeof key === 'string' && !isNaN(key) ? parseInt(key, 10) : key;
};
const colCellsFromCell = (el) => {
    const colClass = el.classList.value.split(' ').find(c => c.includes('anj-col-'));
    return Array.from(el.closest('table').querySelectorAll(`.${colClass}`));
};
const columnFromCell = (columns, cell) => {
    const index = cell.dataset.columnIndex;
    return columns[index];
};
const implicitColumnFactory = (fields, data) => {
    return fields.map(field => {
        const firstWithValue = data.find(d => hasValue(d[field]));
        return {
            field,
            dataType: inferDataType(firstWithValue[field])
        };
    });
};
const inferDataType = (value) => {
    if (typeof value === 'boolean') {
        return 'boolean';
    }
    if (typeof value === 'number') {
        return 'number';
    }
    if (new Date(value).valueOf()) {
        return 'date';
    }
    return 'string';
};
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;
};

export { ALL_ROWS_KEY as A, EditControl as E, RowControl as R, TableClasses as T, calculatePinBoundaries as a, calculateOffsets as b, columnFromCell as c, colCellsFromCell as d, EditControls as e, RowControls as f, implicitColumnFactory as i, keyFromData as k, persistColumn as p };
