"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useAutocomplete = void 0;
var _InputBase2 = _interopRequireDefault(require("@mui/material/InputBase"));
var _react = require("react");
var _Field = require("./Field.cjs");
var _inputUtils = require("./inputUtils.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2024-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 useAutocomplete = ({
  ariaDescribedBy,
  defaultValue,
  errorMessage,
  errorMessageList,
  hasMultipleChoices,
  hint,
  HintLinkComponent,
  inputValue,
  isFullWidth,
  isOptional,
  isVirtualized: isVirtualizedProp,
  label,
  name: nameOverride,
  testId,
  value
}) => {
  const controlledStateRef = (0, _react.useRef)((0, _inputUtils.getControlState)({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const isVirtualizedRef = (0, _react.useRef)(Boolean(isVirtualizedProp));
  const defaultValueProp = (0, _react.useMemo)(() => {
    if (hasMultipleChoices) {
      if (value === undefined) {
        return defaultValue;
      }
      return [];
    }
    return value === undefined ? defaultValue : undefined;
  }, [defaultValue, hasMultipleChoices, value]);
  const valueProps = (0, _inputUtils.useInputValues)({
    controlState: controlledStateRef.current,
    defaultValue: defaultValueProp,
    value
  });
  const inputValueProp = (0, _react.useMemo)(() => {
    if (controlledStateRef.current === _inputUtils.ComponentControlledState.CONTROLLED) {
      return {
        inputValue
      };
    }
    return undefined;
  }, [inputValue]);
  const renderInput = (0, _react.useCallback)(({
    InputLabelProps,
    InputProps,
    ...params
  }) => (0, _jsxRuntime.jsx)(_Field.Field, {
    ariaDescribedBy: ariaDescribedBy,
    errorMessage: errorMessage,
    errorMessageList: errorMessageList,
    fieldType: "single",
    hasVisibleLabel: true,
    hint: hint,
    HintLinkComponent: HintLinkComponent,
    id: InputLabelProps.htmlFor,
    isFullWidth: isFullWidth,
    isOptional: isOptional,
    label: label,
    renderFieldComponent: ({
      ariaDescribedBy,
      id,
      errorMessageElementId,
      labelElementId
    }) => (0, _jsxRuntime.jsx)(_InputBase2.default, {
      ...params,
      ...InputProps,
      "aria-describedby": ariaDescribedBy,
      id: id,
      inputProps: {
        ...params.inputProps,
        "aria-errormessage": errorMessageElementId,
        "aria-labelledby": labelElementId,
        "data-se": testId
      },
      name: nameOverride ?? id,
      required: !isOptional
    })
  }), [ariaDescribedBy, errorMessage, errorMessageList, hint, HintLinkComponent, isFullWidth, isOptional, label, nameOverride, testId]);
  return {
    inputValueProp,
    isVirtualizedRef,
    renderInput,
    valueProps
  };
};
exports.useAutocomplete = useAutocomplete;
//# sourceMappingURL=useAutocomplete.cjs.map