/**
 * Copyright Aquera Inc 2025
 *
 * This source code is licensed under the BSD-3-Clause license found in the
 * LICENSE file in the root directory of this source tree.
 */
import { html } from 'lit';
import { virtualize } from '@lit-labs/virtualizer/virtualize.js';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { classMap } from 'lit/directives/class-map.js';
export class VirtualSelectRenderer {
    static getVirtualizedContent(data, searchEnabled, renderItemFunction, value, multiple, getDisplayText, getItemValue, getItemDescription, getItemPrefix, getItemSuffix, showNoResults, noResultsMessage, loading, onScroll, allowHtmlLabel, enableDescription) {
        return html `
      <div part="select-options" class="select__options ${searchEnabled ? 'select__options__search-enabled' : ``} ${loading ? 'loading' : ''}">
        ${showNoResults && !loading
            ? html `
              <div part="select-no-results" class="select__no-results">
                ${noResultsMessage || 'No results found'}
              </div>
            `
            : html `
              <div
                class="virtualized"
                part="virtualized"
                @scroll=${onScroll}
              >
                ${VirtualSelectRenderer.shouldUseVirtualizer(data)
                ? html `
                      ${virtualize({
                    items: data,
                    renderItem: (item) => VirtualSelectRenderer.getItemRenderFunction(item, renderItemFunction, value, multiple, getDisplayText, getItemValue, getItemDescription, getItemPrefix, getItemSuffix, allowHtmlLabel, enableDescription),
                    scroller: true,
                })}
                    `
                : html `
                      ${data.map((item) => VirtualSelectRenderer.getItemRenderFunction(item, renderItemFunction, value, multiple, getDisplayText, getItemValue, getItemDescription, getItemPrefix, getItemSuffix, allowHtmlLabel, enableDescription))}
                    `}
              </div>
            `}
      </div>`;
    }
    static getItemRenderFunction(item, renderItemFunction, value, multiple, getDisplayText, getItemValue, getItemDescription, getItemPrefix, getItemSuffix, allowHtmlLabel, enableDescription) {
        if (!item) {
            return html ``;
        }
        const displayTextFn = getDisplayText || renderItemFunction;
        const valueFn = getItemValue || ((item) => item?.value || item);
        const optionValue = valueFn(item);
        const displayText = displayTextFn(item);
        const isDisabled = item?.disabled || false;
        const className = item?.className;
        const description = (getItemDescription ? getItemDescription(item) : item?.description) ?? '';
        const prefix = (getItemPrefix ? getItemPrefix(item) : item?.prefix) ?? '';
        const suffix = (getItemSuffix ? getItemSuffix(item) : item?.suffix) ?? '';
        let isSelected = false;
        if (multiple) {
            isSelected = Array.isArray(value) && value.some(v => String(v) === String(optionValue));
        }
        else {
            isSelected = (Array.isArray(value) ? value[0] : value) === optionValue;
        }
        return html `
      <nile-option
        value=${optionValue} 
        .selected=${isSelected} 
        .disabled=${isDisabled}
        .showCheckbox=${multiple}
        class=${classMap({
            option: enableDescription ?? false,
            [className ?? '']: !!className,
        })}
        .description=${description}
        .isDescriptionEnabled=${enableDescription}
      >
        ${unsafeHTML(prefix)}
        ${allowHtmlLabel ? unsafeHTML(displayText) : displayText}
        ${unsafeHTML(suffix)}
      </nile-option>
    `;
    }
    /**
     * Determines whether to use virtualizer based on dataset size
     * For small datasets (less than 5 items), use regular rendering for better sizing
     */
    static shouldUseVirtualizer(data) {
        return data.length >= 5;
    }
}
//# sourceMappingURL=renderer.js.map