"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GroupPicker = void 0;
var _Autocomplete2 = _interopRequireDefault(require("@mui/material/Autocomplete"));
var _InputBase2 = _interopRequireDefault(require("@mui/material/InputBase"));
var _Box2 = _interopRequireDefault(require("@mui/material/Box"));
var _Avatar2 = _interopRequireWildcard(require("@mui/material/Avatar"));
var _react = require("react");
var _Field = require("../Field.cjs");
var _index = require("../icons.generated/index.cjs");
var _OdysseyDesignTokensContext = require("../OdysseyDesignTokensContext.cjs");
var _Tag = require("../Tag.cjs");
var _Typography = require("../Typography.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * 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.
 */

const avatarImageSizeSmall = 16;
const avatarImageSizeMedium = 24;
const GroupPicker = ({
  hasMultipleChoices,
  isCustomValueAllowed,
  isDisabled,
  isLoading,
  isOptional = false,
  isReadOnly,
  hint,
  label,
  onChange,
  onInputChange,
  options,
  value,
  testId
}) => {
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const isOptionEqualToValue = (0, _react.useCallback)((sourceValue, targetValue) => {
    return sourceValue.id === targetValue.id;
  }, []);
  const getOptionLabel = (0, _react.useCallback)(option => {
    return option.name;
  }, []);
  const renderOption = (0, _react.useCallback)((props, option) => {
    return (0, _react.createElement)("li", {
      ...props,
      key: option.id
    }, (0, _jsxRuntime.jsxs)(_Box2.default, {
      sx: {
        alignItems: "top",
        display: "flex",
        flexDirection: "row"
      },
      children: [(0, _jsxRuntime.jsx)(_Box2.default, {
        sx: {
          paddingRight: odysseyDesignTokens.Spacing2
        },
        children: (0, _jsxRuntime.jsx)(_Avatar2.default, {
          alt: option.name,
          src: option.logo,
          sx: {
            [`.${_Avatar2.avatarClasses.fallback}`]: {
              visibility: "hidden"
            },
            background: "transparent",
            height: avatarImageSizeMedium,
            width: avatarImageSizeMedium
          }
        })
      }), (0, _jsxRuntime.jsxs)(_Box2.default, {
        children: [option.name, (0, _jsxRuntime.jsx)(_Typography.Subordinate, {
          children: option.description
        }), (0, _jsxRuntime.jsxs)(_Box2.default, {
          sx: {
            display: "flex",
            flexDirection: "row",
            paddingTop: odysseyDesignTokens.Spacing1
          },
          children: [typeof option.usersCount === "number" && (0, _jsxRuntime.jsxs)(_Box2.default, {
            sx: {
              display: "flex",
              flexDirection: "row",
              paddingRight: odysseyDesignTokens.Spacing4
            },
            children: [(0, _jsxRuntime.jsx)(_index.UserIcon, {}), option.usersCount]
          }), typeof option.appsCount === "number" && (0, _jsxRuntime.jsxs)(_Box2.default, {
            sx: {
              display: "flex",
              flexDirection: "row",
              paddingRight: odysseyDesignTokens.Spacing4
            },
            children: [(0, _jsxRuntime.jsx)(_index.GridIcon, {}), option.appsCount]
          }), typeof option.groupPushMappingsCount === "number" && (0, _jsxRuntime.jsxs)(_Box2.default, {
            sx: {
              display: "flex",
              flexDirection: "row"
            },
            children: [(0, _jsxRuntime.jsx)(_index.GroupIcon, {}), option.groupPushMappingsCount]
          })]
        })]
      })]
    }));
  }, [odysseyDesignTokens]);
  const renderTags = (0, _react.useCallback)((values, getTagProps) => values.map((option, index) => {
    const {
      key,
      onDelete
    } = getTagProps({
      index
    });
    return (0, _jsxRuntime.jsx)(_Box2.default, {
      sx: {
        margin: odysseyDesignTokens.Spacing1
      },
      children: (0, _jsxRuntime.jsx)(_Tag.Tag, {
        icon: (0, _jsxRuntime.jsx)(_Avatar2.default, {
          alt: option.name,
          src: option.logo,
          sx: {
            [`.${_Avatar2.avatarClasses.fallback}`]: {
              visibility: "hidden"
            },
            background: "transparent",
            height: avatarImageSizeSmall,
            width: avatarImageSizeSmall
          }
        }),
        label: option.name,
        onRemove: onDelete
      })
    }, key);
  }), [odysseyDesignTokens]);
  const renderInput = (0, _react.useCallback)(({
    InputLabelProps,
    InputProps,
    ...params
  }) => (0, _jsxRuntime.jsx)(_Field.Field, {
    fieldType: "single",
    hasVisibleLabel: true,
    hint: hint,
    id: InputLabelProps.htmlFor,
    isOptional: isOptional,
    label: label,
    renderFieldComponent: ({
      ariaDescribedBy,
      id
    }) => (0, _jsxRuntime.jsx)(_InputBase2.default, {
      ...params,
      ...InputProps,
      "aria-describedby": ariaDescribedBy,
      id: id,
      required: !isOptional
    })
  }), [hint, isOptional, label]);
  return (0, _jsxRuntime.jsx)(_Autocomplete2.default, {
    "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 = exports.GroupPicker = (0, _react.memo)(GroupPicker);
//# sourceMappingURL=GroupPicker.cjs.map