"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const utils_external_1 = require("../../../utils-external");
const DismissableLayer_1 = require("../../../utils/components/dismissablelayer/DismissableLayer");
const Floating_1 = require("../../../utils/components/floating/Floating");
const helpers_1 = require("../../../utils/helpers");
const Input_context_1 = require("../Input/Input.context");
const selectedOptionsContext_1 = require("../SelectedOptions/selectedOptionsContext");
const AddNewOption_1 = __importDefault(require("./AddNewOption"));
const FilteredOptionsItem_1 = __importDefault(require("./FilteredOptionsItem"));
const LoadingMessage_1 = __importDefault(require("./LoadingMessage"));
const MaxSelectedMessage_1 = __importDefault(require("./MaxSelectedMessage"));
const NoSearchHitsMessage_1 = __importDefault(require("./NoSearchHitsMessage"));
const filtered_options_util_1 = __importDefault(require("./filtered-options-util"));
const filteredOptionsContext_1 = require("./filteredOptionsContext");
const FilteredOptions = () => {
    const { inputProps: { id }, anchorRef, } = (0, Input_context_1.useInputContext)();
    const { allowNewValues, isLoading, isListOpen, filteredOptions, setFilteredOptionsRef, isMouseLastUsedInputDevice, isValueNew, toggleIsListOpen, } = (0, filteredOptionsContext_1.useFilteredOptionsContext)();
    const [localOpen, setLocalOpen] = (0, react_1.useState)(isListOpen);
    const floatingRef = (0, react_1.useRef)(null);
    /**
     * This is a dirty hack to make the positioning-logic in Floating base the "flip" on the static 290px max-height,
     * instead of the dynamic one based on available space. Without this, the list won't flip to top when there's
     * not enough space below the input.
     */
    (0, utils_external_1.useClientLayoutEffect)(() => {
        queueMicrotask(() => setLocalOpen(isListOpen));
    }, [isListOpen]);
    const { maxSelected, isMultiSelect, selectedOptions } = (0, selectedOptionsContext_1.useSelectedOptionsContext)();
    const shouldRenderNonSelectables = (isMultiSelect && maxSelected.limit) || // Render maxSelected message
        isLoading || // Render loading message
        (!isLoading && filteredOptions.length === 0 && !allowNewValues); // Render no hits message
    const singleSelectedOption = (0, react_1.useMemo)(() => {
        if (isMultiSelect ||
            selectedOptions.length === 0 ||
            selectedOptions.length > 1) {
            return null;
        }
        return selectedOptions[0];
    }, [isMultiSelect, selectedOptions]);
    const shouldRenderFilteredOptionsList = (allowNewValues && isValueNew && !maxSelected.isLimitReached) || // Render add new option
        filteredOptions.length > 0 || // Render filtered options
        !!singleSelectedOption;
    return (react_1.default.createElement(DismissableLayer_1.DismissableLayer, { asChild: true, safeZone: {
            anchor: anchorRef,
        }, onDismiss: () => localOpen && toggleIsListOpen(false), onEscapeKeyDown: (event) => {
            /* We handle this manually in Input */
            event.preventDefault();
        }, enabled: localOpen },
        react_1.default.createElement(Floating_1.Floating.Content, { ref: floatingRef, className: (0, helpers_1.cl)("aksel-combobox__list", {
                "aksel-combobox__list--closed": !isListOpen,
                "aksel-combobox__list--with-hover": isMouseLastUsedInputDevice,
            }), id: filtered_options_util_1.default.getFilteredOptionsId(id), tabIndex: -1, sideOffset: 8, side: "bottom", fallbackPlacements: ["top"], enabled: isListOpen, style: {
                maxHeight: localOpen
                    ? `min(316px, var(--__axc-floating-available-height))`
                    : `316px`,
            }, autoUpdateWhileMounted: false },
            shouldRenderNonSelectables && (react_1.default.createElement("div", { className: "aksel-combobox__list_non-selectables", role: "status" },
                isMultiSelect && maxSelected.limit && react_1.default.createElement(MaxSelectedMessage_1.default, null),
                isLoading && react_1.default.createElement(LoadingMessage_1.default, null),
                !isLoading && filteredOptions.length === 0 && !allowNewValues && (react_1.default.createElement(NoSearchHitsMessage_1.default, null)))),
            shouldRenderFilteredOptionsList && (react_1.default.createElement("ul", { ref: setFilteredOptionsRef, role: "listbox", className: "aksel-combobox__list-options" },
                isValueNew && !maxSelected.isLimitReached && allowNewValues && (react_1.default.createElement(AddNewOption_1.default, null)),
                singleSelectedOption && (react_1.default.createElement(FilteredOptionsItem_1.default, { key: singleSelectedOption.value, option: singleSelectedOption })),
                filteredOptions.map((option) => {
                    if (singleSelectedOption &&
                        option.value === singleSelectedOption.value) {
                        return null;
                    }
                    return react_1.default.createElement(FilteredOptionsItem_1.default, { key: option.value, option: option });
                }))))));
};
exports.default = FilteredOptions;
//# sourceMappingURL=FilteredOptions.js.map