'use strict';

const utils = require('./utils-2a7c8f6e.js');

const ALL_ROWS_KEY = 'all-rows';
(function (EditControl) {
    EditControl["save"] = "save";
    EditControl["cancel"] = "cancel";
})(exports.EditControl || (exports.EditControl = {}));
const EditControls = Object.values(exports.EditControl);
(function (RowControl) {
    RowControl["update"] = "update";
    RowControl["delete"] = "delete";
})(exports.RowControl || (exports.RowControl = {}));
const RowControls = Object.values(exports.RowControl);
(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";
})(exports.TableClasses || (exports.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 => utils.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;
};

exports.ALL_ROWS_KEY = ALL_ROWS_KEY;
exports.EditControls = EditControls;
exports.RowControls = RowControls;
exports.calculateOffsets = calculateOffsets;
exports.calculatePinBoundaries = calculatePinBoundaries;
exports.colCellsFromCell = colCellsFromCell;
exports.columnFromCell = columnFromCell;
exports.implicitColumnFactory = implicitColumnFactory;
exports.keyFromData = keyFromData;
exports.persistColumn = persistColumn;
