import React, { forwardRef, useMemo, useCallback } from 'react';
import { Autocomplete } from '@material-ui/lab';
import { TextField } from '@material-ui/core';
import { TenantTypeEnums } from '../../enums/unitySystemEnums';
const sanitizeRecordId = (tenantName) => tenantName.replace(/\s+/g, '');
export const TenantSelect = forwardRef((props, ref) => {
    const { tenants, value, onTenantChange, id = 'tenant-select', label = 'Current Tenant', className, disableClearable = true, textFieldProps, recordIdPrefix = 'udpRecord-TenantSelect' } = props;
    const userTenantsContainsMultipleTypes = tenants.some((tenant) => tenant.tenantType == TenantTypeEnums.Tenant) &&
        tenants.some((tenant) => tenant.tenantType == TenantTypeEnums.Product);
    const sortTenantsByNameAlphacetically = (tenants) => {
        const tenantProperty = "tenantName";
        tenants.sort((tenant, otherTenant) => {
            if (tenant[tenantProperty].toLowerCase() < otherTenant[tenantProperty].toLowerCase()) {
                return -1;
            }
            else if (tenant[tenantProperty].toLowerCase() > otherTenant[tenantProperty].toLowerCase()) {
                return 1;
            }
            else {
                return 0;
            }
        });
    };
    const groupTenantsByTypeAndFilter = useCallback((tenants) => {
        if (!userTenantsContainsMultipleTypes) {
            sortTenantsByNameAlphacetically(tenants);
            return tenants;
        }
        const clientTenants = tenants.filter((tenant) => tenant.tenantType == TenantTypeEnums.Tenant);
        sortTenantsByNameAlphacetically(clientTenants);
        const productTenants = tenants.filter((tenant) => tenant.tenantType == TenantTypeEnums.Product);
        sortTenantsByNameAlphacetically(productTenants);
        return clientTenants.concat(productTenants);
    }, [userTenantsContainsMultipleTypes]);
    const options = useMemo(() => tenants ? groupTenantsByTypeAndFilter(tenants) : [], [tenants, groupTenantsByTypeAndFilter]);
    const handleChange = useCallback((event, newValue, reason) => {
        if (reason !== 'select-option' || !newValue) {
            return;
        }
        onTenantChange === null || onTenantChange === void 0 ? void 0 : onTenantChange(newValue);
    }, [onTenantChange]);
    return (React.createElement(Autocomplete, { ref: ref, className: className, id: id, value: value, options: options, disableClearable: disableClearable, getOptionSelected: (option, selected) => (option === null || option === void 0 ? void 0 : option.tenantId) === (selected === null || selected === void 0 ? void 0 : selected.tenantId), getOptionLabel: (option) => {
            var _a;
            return (_a = option === null || option === void 0 ? void 0 : option.tenantName) !== null && _a !== void 0 ? _a : '';
        }, onChange: handleChange, groupBy: (option) => userTenantsContainsMultipleTypes
            ? option.tenantType === TenantTypeEnums.Product
                ? 'Product'
                : 'Client'
            : null, renderInput: (params) => (React.createElement(TextField, Object.assign({}, params, { label: label, variant: "standard" }, textFieldProps))), renderOption: (option) => {
            var _a, _b;
            return (React.createElement("li", { style: { width: '100%', height: '100%', listStyleType: 'none' }, 
                // @ts-expect-error udpRecordId is used for automation tagging
                udprecordid: `${recordIdPrefix}-${sanitizeRecordId((_a = option === null || option === void 0 ? void 0 : option.tenantName) !== null && _a !== void 0 ? _a : '')}` }, (_b = option === null || option === void 0 ? void 0 : option.tenantName) !== null && _b !== void 0 ? _b : ''));
        } }));
});
TenantSelect.displayName = 'TenantSelect';
//# sourceMappingURL=TenantSelect.js.map