"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.searchVariantValues = exports.SearchField = void 0;
var _InputBase2 = _interopRequireDefault(require("@mui/material/InputBase"));
var _InputAdornment2 = _interopRequireDefault(require("@mui/material/InputAdornment"));
var _IconButton2 = _interopRequireDefault(require("@mui/material/IconButton"));
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _react = require("react");
var _Field = require("./Field.cjs");
var _i18n = require("./i18n.generated/i18n.cjs");
var _index = require("./icons.generated/index.cjs");
var _inputUtils = require("./inputUtils.cjs");
var _OdysseyThemeProvider = require("./OdysseyThemeProvider.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * 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.
 */

const searchVariantValues = exports.searchVariantValues = ["outline", "filled"];
const StyledContainerToEnsureThemeProviderDivIsFullWidth = (0, _styled.default)("div")({
  width: "100%"
});
const SearchField = (0, _react.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
  } = (0, _i18n.useTranslation)();
  const onChange = (0, _react.useCallback)(event => {
    onChangeProp?.(event);
  }, [onChangeProp]);
  const onClear = (0, _react.useCallback)(() => {
    onClearProp?.();
  }, [onClearProp]);
  const controlledStateRef = (0, _react.useRef)((0, _inputUtils.getControlState)({
    controlledValue: value,
    uncontrolledValue: defaultValue
  }));
  const inputValues = (0, _inputUtils.useInputValues)({
    defaultValue,
    value,
    controlState: controlledStateRef.current
  });
  const renderFieldComponent = (0, _react.useCallback)(({
    ariaDescribedBy,
    id
  }) => (0, _jsxRuntime.jsx)(_InputBase2.default, {
    ...inputValues,
    "aria-describedby": ariaDescribedBy,
    autoComplete: autoCompleteType,
    autoFocus: hasInitialFocus,
    "data-ods-type": "search",
    "data-ods-variant": variant,
    "data-se": testId,
    endAdornment: (inputValues?.defaultValue || inputValues?.value) && (0, _jsxRuntime.jsx)(_InputAdornment2.default, {
      position: "end",
      children: (0, _jsxRuntime.jsx)(_IconButton2.default, {
        "aria-label": t("clear.text"),
        disabled: isDisabled,
        onClick: onClear,
        size: "small",
        children: (0, _jsxRuntime.jsx)(_index.CloseCircleFilledIcon, {})
      })
    }),
    id: id,
    inputProps: {
      tabIndex
    },
    name: nameOverride ?? id,
    onBlur: onBlur,
    onChange: onChange,
    onFocus: onFocus,
    placeholder: placeholder,
    ref: ref,
    startAdornment: (0, _jsxRuntime.jsx)(_InputAdornment2.default, {
      position: "start",
      children: (0, _jsxRuntime.jsx)(_index.SearchIcon, {})
    }),
    translate: translate,
    type: "search"
  }), [autoCompleteType, hasInitialFocus, inputValues, isDisabled, nameOverride, onBlur, onChange, onClear, onFocus, placeholder, ref, t, tabIndex, testId, translate, variant]);
  return (0, _jsxRuntime.jsx)(StyledContainerToEnsureThemeProviderDivIsFullWidth, {
    children: (0, _jsxRuntime.jsx)(_OdysseyThemeProvider.OdysseyThemeProvider, {
      children: (0, _jsxRuntime.jsx)(_Field.Field, {
        ariaDescribedBy: ariaDescribedBy,
        fieldType: "single",
        hasVisibleLabel: false,
        id: idOverride,
        isDisabled: isDisabled,
        isFullWidth: isFullWidth,
        isOptional: true,
        label: label,
        renderFieldComponent: renderFieldComponent
      })
    })
  });
});
const MemoizedSearchField = exports.SearchField = (0, _react.memo)(SearchField);
MemoizedSearchField.displayName = "SearchField";
//# sourceMappingURL=SearchField.cjs.map