var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useCallback, useEffect, useState } from 'react';
import { Chip, TextField } from '@material-ui/core';
import Autocomplete from '@material-ui/lab/Autocomplete';
// import { FluentTextField } from '../../../../UI/inputs/textField';
export const UserRoleAutoComplete = props => {
    const { id, label, placeholder, options, textFieldProps, disabled, onChange: onChangeProp, initialValue, value, optionLabel, groupBy, userRoles, setUserRoles, isFormField = false } = props, other = __rest(props, ["id", "label", "placeholder", "options", "textFieldProps", "disabled", "onChange", "initialValue", "value", "optionLabel", "groupBy", "userRoles", "setUserRoles", "isFormField"]);
    const [acValue, setACValue] = useState(initialValue);
    // Fix onChange to something Field can understand
    const onChange = useCallback((e, value) => onChangeProp === null || onChangeProp === void 0 ? void 0 : onChangeProp(value), [
        onChangeProp,
    ]);
    const parseMultiple = useCallback(value => value, []);
    const formatMultiple = useCallback((...args) => args[0] || [], []);
    const handleChange = (e, value) => {
        onChange(e, value);
        if (userRoles && setUserRoles) {
            const userId = id.replace('_role', '');
            const updatedUserRoles = [...userRoles];
            const userRole = updatedUserRoles.find(ur => ur.id === userId);
            if (userRole) {
                userRole.roles = value;
            }
            else {
                updatedUserRoles.push({ id: userId, roles: value });
            }
            setUserRoles(updatedUserRoles);
            setACValue(value);
        }
        else {
            setACValue(value);
        }
    };
    useEffect(() => {
        if (!userRoles) {
            return;
        }
        const userId = id.replace('_role', '');
        const foundUserRole = userRoles.find(ur => ur.id === userId);
        if (foundUserRole) {
            const roleIds = foundUserRole.roles.map(role => role.roleId);
            const roleValues = options.filter(option => roleIds.includes(option.roleId));
            setACValue(roleValues);
        }
    }, [options, userRoles, id]);
    // need to NOT include onChange in dependency list, or it will re-render on every change
    useEffect(() => {
        if (isFormField && initialValue) {
            onChange(null, initialValue);
            setACValue(initialValue);
        }
    }, [initialValue, isFormField]);
    return (React.createElement(Autocomplete, { multiple: true, id: id, disabled: disabled, options: options || [], getOptionLabel: optionLabel
            ? option => option[optionLabel] || ''
            : option => option.name, onChange: (e, value) => handleChange(e, value), value: acValue, groupBy: groupBy ? option => option[groupBy] : '', parse: parseMultiple, format: formatMultiple, renderTags: (value, getTagProps) => value.map((option, index) => (React.createElement(Chip, Object.assign({ key: index, color: "primary", label: React.createElement("span", { style: { color: 'white' } }, optionLabel
                ? option[optionLabel]
                    ? option[optionLabel]
                    : ''
                : option) }, getTagProps({ index }))))), renderInput: params => (React.createElement(TextField, Object.assign({}, textFieldProps, params, other, { label: label, variant: "outlined", fullWidth: true, size: "small" }))) }));
};
//# sourceMappingURL=UserRoleAutoComplete.js.map