import _Autocomplete from "@mui/material/Autocomplete";
import _InputBase from "@mui/material/InputBase";
import _Box from "@mui/material/Box";
import _Avatar from "@mui/material/Avatar";
/*!
 * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */
import { avatarClasses } from "@mui/material/Avatar";
import { memo, useCallback, createElement as _createElement } from "react";
import { Field } from "../Field.js";
import { GridIcon, GroupIcon, UserIcon } from "../icons.generated/index.js";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext.js";
import { Tag } from "../Tag.js";
import { Subordinate } from "../Typography.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const avatarImageSizeSmall = 16;
const avatarImageSizeMedium = 24;
const GroupPicker = ({
  hasMultipleChoices,
  isCustomValueAllowed,
  isDisabled,
  isLoading,
  isOptional = false,
  isReadOnly,
  hint,
  label,
  onChange,
  onInputChange,
  options,
  value,
  testId
}) => {
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const isOptionEqualToValue = useCallback((sourceValue, targetValue) => {
    return sourceValue.id === targetValue.id;
  }, []);
  const getOptionLabel = useCallback(option => {
    return option.name;
  }, []);
  const renderOption = useCallback((props, option) => {
    return _createElement("li", {
      ...props,
      key: option.id
    }, _jsxs(_Box, {
      sx: {
        alignItems: "top",
        display: "flex",
        flexDirection: "row"
      },
      children: [_jsx(_Box, {
        sx: {
          paddingRight: odysseyDesignTokens.Spacing2
        },
        children: _jsx(_Avatar, {
          alt: option.name,
          src: option.logo,
          sx: {
            [`.${avatarClasses.fallback}`]: {
              visibility: "hidden"
            },
            background: "transparent",
            height: avatarImageSizeMedium,
            width: avatarImageSizeMedium
          }
        })
      }), _jsxs(_Box, {
        children: [option.name, _jsx(Subordinate, {
          children: option.description
        }), _jsxs(_Box, {
          sx: {
            display: "flex",
            flexDirection: "row",
            paddingTop: odysseyDesignTokens.Spacing1
          },
          children: [typeof option.usersCount === "number" && _jsxs(_Box, {
            sx: {
              display: "flex",
              flexDirection: "row",
              paddingRight: odysseyDesignTokens.Spacing4
            },
            children: [_jsx(UserIcon, {}), option.usersCount]
          }), typeof option.appsCount === "number" && _jsxs(_Box, {
            sx: {
              display: "flex",
              flexDirection: "row",
              paddingRight: odysseyDesignTokens.Spacing4
            },
            children: [_jsx(GridIcon, {}), option.appsCount]
          }), typeof option.groupPushMappingsCount === "number" && _jsxs(_Box, {
            sx: {
              display: "flex",
              flexDirection: "row"
            },
            children: [_jsx(GroupIcon, {}), option.groupPushMappingsCount]
          })]
        })]
      })]
    }));
  }, [odysseyDesignTokens]);
  const renderTags = useCallback((values, getTagProps) => values.map((option, index) => {
    const {
      key,
      onDelete
    } = getTagProps({
      index
    });
    return _jsx(_Box, {
      sx: {
        margin: odysseyDesignTokens.Spacing1
      },
      children: _jsx(Tag, {
        icon: _jsx(_Avatar, {
          alt: option.name,
          src: option.logo,
          sx: {
            [`.${avatarClasses.fallback}`]: {
              visibility: "hidden"
            },
            background: "transparent",
            height: avatarImageSizeSmall,
            width: avatarImageSizeSmall
          }
        }),
        label: option.name,
        onRemove: onDelete
      })
    }, key);
  }), [odysseyDesignTokens]);
  const renderInput = useCallback(({
    InputLabelProps,
    InputProps,
    ...params
  }) => _jsx(Field, {
    fieldType: "single",
    hasVisibleLabel: true,
    hint: hint,
    id: InputLabelProps.htmlFor,
    isOptional: isOptional,
    label: label,
    renderFieldComponent: ({
      ariaDescribedBy,
      id
    }) => _jsx(_InputBase, {
      ...params,
      ...InputProps,
      "aria-describedby": ariaDescribedBy,
      id: id,
      required: !isOptional
    })
  }), [hint, isOptional, label]);
  return _jsx(_Autocomplete, {
    "aria-disabled": isDisabled,
    "data-se": testId,
    disabled: isDisabled,
    filterSelectedOptions: true,
    freeSolo: isCustomValueAllowed,
    getOptionLabel: getOptionLabel,
    isOptionEqualToValue: isOptionEqualToValue,
    loading: isLoading,
    multiple: hasMultipleChoices,
    onChange: onChange,
    onInputChange: onInputChange,
    options: options,
    readOnly: isReadOnly,
    renderInput: renderInput,
    renderOption: renderOption,
    renderTags: renderTags,
    value: value
  });
};
const MemoizedGroupPicker = memo(GroupPicker);
export { MemoizedGroupPicker as GroupPicker };
//# sourceMappingURL=GroupPicker.js.map