"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.constructPopoverOrigin = exports.isSelectedFacet = exports.filterItemsInGroups = exports.adjustVerticalPopupLayout = void 0;
var ramda_1 = require("ramda");
var adjustVerticalPopupLayout = function (anchorEl, popupHeight) {
    var verticalOrigin = 'top';
    var marginThreshold = 16;
    if (anchorEl) {
        var anchorRect = anchorEl.getBoundingClientRect();
        var highestPoint = marginThreshold;
        var lowestPoint = window.innerHeight - marginThreshold;
        if (anchorRect.top + popupHeight > lowestPoint) {
            if (anchorRect.bottom - popupHeight < highestPoint) {
                var maxHeightToBottom = anchorRect.bottom - highestPoint;
                var maxHeightToTop = lowestPoint - anchorRect.top;
                popupHeight = Math.max(maxHeightToBottom, maxHeightToTop);
                verticalOrigin = maxHeightToBottom > maxHeightToTop ? 'bottom' : 'top';
            }
            else {
                verticalOrigin = 'bottom';
            }
        }
    }
    return { verticalOrigin: verticalOrigin, popupHeight: popupHeight };
};
exports.adjustVerticalPopupLayout = adjustVerticalPopupLayout;
var filterItem = function (item, filterText) { var _a, _b; return (_b = (_a = item.title) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(filterText.toLowerCase())) !== null && _b !== void 0 ? _b : false; };
var filterItemsTree = function (item, filterText) {
    return filterItem(item, filterText) || (item.children || []).some(function (child) { return filterItemsTree(child, filterText); });
};
var getFilteredFacets = function (facets, filterText) {
    return (facets || [])
        .map(function (facet) { return (__assign(__assign({}, facet), { hide: !filterItemsTree(facet, filterText), filterText: filterText })); })
        .map(function (facet) {
        var children = getFilteredFacets(facet.children, filterText);
        return (0, ramda_1.isEmpty)(children)
            ? facet
            : __assign(__assign({}, facet), { children: children });
    });
};
var filterItemsInGroups = function (groups, filterText) {
    return groups.map(function (group) { return (__assign(__assign({}, group), { facets: getFilteredFacets(group.facets, filterText) })); });
};
exports.filterItemsInGroups = filterItemsInGroups;
var isSelectedFacet = function (_a) {
    var facet = _a.facet, selectedFacetsFieldNames = _a.selectedFacetsFieldNames;
    return !!selectedFacetsFieldNames.find(function (fieldName) { return fieldName === facet.fieldName || fieldName.includes("".concat(facet.fieldName, ".")); });
};
exports.isSelectedFacet = isSelectedFacet;
var constructPopoverOrigin = function (verticalOrigin, horizontalOrigin) {
    if (horizontalOrigin === void 0) { horizontalOrigin = 'right'; }
    return {
        vertical: verticalOrigin,
        horizontal: horizontalOrigin
    };
};
exports.constructPopoverOrigin = constructPopoverOrigin;
