import _InputBase from "@mui/material/InputBase";
import _InputAdornment from "@mui/material/InputAdornment";
import _IconButton from "@mui/material/IconButton";
/*!
 * Copyright (c) 2022-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 styled from "@emotion/styled";
import { forwardRef, memo, useCallback, useRef } from "react";
import { Field } from "./Field.js";
import { useTranslation } from "./i18n.generated/i18n.js";
import { CloseCircleFilledIcon, SearchIcon } from "./icons.generated/index.js";
import { getControlState, useInputValues } from "./inputUtils.js";
import { OdysseyThemeProvider } from "./OdysseyThemeProvider.js";
import { jsx as _jsx } from "react/jsx-runtime";
export const searchVariantValues = ["outline", "filled"];
const StyledContainerToEnsureThemeProviderDivIsFullWidth = styled("div")({
  width: "100%"
});
const SearchField = forwardRef(({
  ariaDescribedBy,
  autoCompleteType,
  defaultValue,
  hasInitialFocus,
  id: idOverride,
  isDisabled = false,
  isFullWidth = false,
  label,
  name: nameOverride,
  onChange: onChangeProp,
  onFocus,
  onBlur,
  onClear: onClearProp,
  placeholder,
  tabIndex,
  testId,
  translate,
  value,
  variant = "outline"
}, ref) => {
  const {
    t
  } = useTranslation();
  const onChange = useCallback(event => {
    onChangeProp?.(event);
  }, [onChangeProp]);
  const onClear = useCallback(() => {
    onClearProp?.();
  }, [onClearProp]);
  const controlledStateRef = useRef(getControlState({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const inputValues = useInputValues({
    defaultValue,
    value,
    controlState: controlledStateRef.current
  });
  const renderFieldComponent = useCallback(({
    ariaDescribedBy,
    id
  }) => _jsx(_InputBase, {
    ...inputValues,
    "aria-describedby": ariaDescribedBy,
    autoComplete: autoCompleteType,
    autoFocus: hasInitialFocus,
    "data-ods-type": "search",
    "data-ods-variant": variant,
    "data-se": testId,
    endAdornment: (inputValues?.defaultValue || inputValues?.value) && _jsx(_InputAdornment, {
      position: "end",
      children: _jsx(_IconButton, {
        "aria-label": t("clear.text"),
        disabled: isDisabled,
        onClick: onClear,
        size: "small",
        children: _jsx(CloseCircleFilledIcon, {})
      })
    }),
    id: id,
    inputProps: {
      tabIndex
    },
    name: nameOverride ?? id,
    onBlur: onBlur,
    onChange: onChange,
    onFocus: onFocus,
    placeholder: placeholder,
    ref: ref,
    startAdornment: _jsx(_InputAdornment, {
      position: "start",
      children: _jsx(SearchIcon, {})
    }),
    translate: translate,
    type: "search"
  }), [autoCompleteType, hasInitialFocus, inputValues, isDisabled, nameOverride, onBlur, onChange, onClear, onFocus, placeholder, ref, t, tabIndex, testId, translate, variant]);
  return _jsx(StyledContainerToEnsureThemeProviderDivIsFullWidth, {
    children: _jsx(OdysseyThemeProvider, {
      children: _jsx(Field, {
        ariaDescribedBy: ariaDescribedBy,
        fieldType: "single",
        hasVisibleLabel: false,
        id: idOverride,
        isDisabled: isDisabled,
        isFullWidth: isFullWidth,
        isOptional: true,
        label: label,
        renderFieldComponent: renderFieldComponent
      })
    })
  });
});
const MemoizedSearchField = memo(SearchField);
MemoizedSearchField.displayName = "SearchField";
export { MemoizedSearchField as SearchField };
//# sourceMappingURL=SearchField.js.map