import React, { useCallback, useMemo } from 'react';
import { UdpAutocomplete } from '../../UI/fluent-web-components/UdpAutocomplete';
import { TenantTypeEnums } from '../../enums/unitySystemEnums';
const sortByName = (list) => [...list].sort((a, b) => a.tenantName.toLowerCase().localeCompare(b.tenantName.toLowerCase()));
export const UdpTenantSelect = ({ tenants, value, onTenantChange, label = 'Current Tenant', }) => {
    const options = useMemo(() => {
        if (!tenants)
            return [];
        const hasMultipleTypes = tenants.some((t) => t.tenantType === TenantTypeEnums.Tenant) &&
            tenants.some((t) => t.tenantType === TenantTypeEnums.Product);
        if (!hasMultipleTypes)
            return sortByName(tenants);
        return [
            ...sortByName(tenants.filter((t) => t.tenantType === TenantTypeEnums.Tenant)),
            ...sortByName(tenants.filter((t) => t.tenantType === TenantTypeEnums.Product)),
        ];
    }, [tenants]);
    const handleChange = useCallback((e) => {
        if (e.target.value)
            onTenantChange === null || onTenantChange === void 0 ? void 0 : onTenantChange(e.target.value);
    }, [onTenantChange]);
    return (React.createElement(UdpAutocomplete, { value: value !== null && value !== void 0 ? value : undefined, options: options, optionLabel: (option) => {
            var _a;
            return (_a = option === null || option === void 0 ? void 0 : option.tenantName) !== null && _a !== void 0 ? _a : '';
        }, optionValue: "tenantId", label: label, includeErrorPadding: false, onChange: handleChange }));
};
//# sourceMappingURL=UdpTenantSelect.js.map