"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getListLabelWidth = exports.getListLabel = exports.countHiddenItems = void 0;
var ramda_1 = require("ramda");
var ITEMS_SEPARATOR = ', ';
var ALWAYS_VISIBLE_ITEMS_COUNT = 1;
var HIDDEN_COUNT_WIDTH = 35;
var findFirstOverflowItemIndex = function (list, labelEl) {
    var childNodes = labelEl.childNodes, clientLeft = labelEl.clientLeft, clientWidth = labelEl.clientWidth;
    if (childNodes.length > 0) {
        var textNode = Array.from(childNodes)[0];
        var range = document.createRange();
        range.setStart(textNode, 0);
        for (var index = 0; index < list.length; index++) {
            range.setEnd(textNode, range.endOffset + list[index].length);
            var rangeWidth = range.getBoundingClientRect().width;
            if (clientLeft + rangeWidth > clientWidth) {
                return index;
            }
            if (index !== list.length - 1) {
                range.setEnd(textNode, range.endOffset + ITEMS_SEPARATOR.length);
            }
        }
    }
    return -1;
};
var countOverflowItems = function (list, labelEl) {
    if (labelEl) {
        var firstOverflowItemIndex = findFirstOverflowItemIndex(list, labelEl);
        return firstOverflowItemIndex === -1 ? 0 : list.length - firstOverflowItemIndex;
    }
    else {
        return 0;
    }
};
var countHiddenItems = function (list, labelEl) {
    var itemsCount = list.length;
    var overflowItemsCount = countOverflowItems(list, labelEl);
    var visibleItemsCount = Math.max(ALWAYS_VISIBLE_ITEMS_COUNT, itemsCount - overflowItemsCount);
    return itemsCount - visibleItemsCount;
};
exports.countHiddenItems = countHiddenItems;
var getListLabel = function (list) { return list.join(ITEMS_SEPARATOR); };
exports.getListLabel = getListLabel;
var getListLabelWidth = function (maxWidth) {
    var maxCssWidth = (0, ramda_1.isNil)(maxWidth) ? '100%' : "".concat(maxWidth, "px");
    return "calc(".concat(maxCssWidth, " - ").concat(HIDDEN_COUNT_WIDTH, "px)");
};
exports.getListLabelWidth = getListLabelWidth;
