/*!
 * 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 styled from "@emotion/styled";
import { memo, useCallback, useMemo } from "react";
import { useOdysseyDesignTokens } from "../../OdysseyDesignTokensContext.js";
import { Heading6 } from "../../Typography.js";
import { ComposablePicker } from "./ComposablePicker.js";
import { Option, OptionDescriptionComponent, OptionLabelContainer, OptionMetadataComponent } from "./Picker.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const OptionAdornmentContainer = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens" && prop !== "adornmentSize" && prop !== "isTagContainer"
})(({
  adornmentSize = "small",
  isTagContainer = false,
  odysseyDesignTokens,
  position = "left"
}) => ({
  position: "relative",
  bottom: "1px",
  alignSelf: "flex-start",
  width: odysseyDesignTokens.Spacing5,
  height: odysseyDesignTokens.Spacing5,
  overflow: "hidden",
  ...(position === "left" && {
    marginInlineEnd: odysseyDesignTokens.Spacing3
  }),
  ...(position === "right" && {
    marginInlineStart: odysseyDesignTokens.Spacing3
  }),
  svg: {
    width: "100%",
    height: "auto"
  },
  img: {
    position: "absolute",
    top: "50%",
    left: "50%",
    width: "100%",
    transform: "translate(-50%, -50%)"
  },
  ...(adornmentSize === "large" && !isTagContainer && {
    bottom: 0,
    width: odysseyDesignTokens.Spacing8,
    height: odysseyDesignTokens.Spacing8
  }),
  ...(isTagContainer && {
    bottom: 0,
    alignSelf: "center",
    width: odysseyDesignTokens.Spacing4,
    height: "auto",
    maxHeight: odysseyDesignTokens.Spacing4,
    marginInlineEnd: odysseyDesignTokens.Spacing2,
    svg: {
      display: "flex",
      width: "100%",
      height: "auto"
    }
  })
}));
const CustomOptionContainer = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(() => ({
  display: "flex",
  justifyContent: "space-between",
  width: "100%"
}));
const CustomOptionLeftContainer = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(() => ({
  display: "flex"
}));
const OptionAdornment = ({
  adornment,
  adornmentSize,
  odysseyDesignTokens
}) => {
  const isImageAdornment = typeof adornment === "string";
  if (isImageAdornment) {
    return _jsx(OptionAdornmentContainer, {
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens,
      children: _jsx("img", {
        alt: "",
        role: "presentation",
        src: adornment
      })
    });
  } else {
    return _jsx(OptionAdornmentContainer, {
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens,
      children: adornment
    });
  }
};
const Extra = ({
  content,
  size,
  odysseyDesignTokens,
  onClick
}) => {
  const handleClick = useCallback(event => {
    event.stopPropagation();
    onClick();
  }, [onClick]);
  return _jsx(OptionAdornmentContainer, {
    adornmentSize: size,
    odysseyDesignTokens: odysseyDesignTokens,
    onClick: handleClick,
    children: content
  });
};
const CustomOption = ({
  adornmentSize,
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    adornment,
    description,
    label,
    metaData,
    extra,
    onClick,
    isInteractive = true
  } = option;
  const handleOptionClick = useCallback(event => {
    if (!isInteractive) {
      event.preventDefault();
      return;
    }
    event.stopPropagation();
    onClick?.();
  }, [onClick, isInteractive]);
  const extendedMuiProps = useMemo(() => {
    return {
      ...muiProps,
      onClick: isInteractive ? handleOptionClick : undefined,
      "aria-disabled": !isInteractive ? "true" : undefined,
      role: "option"
    };
  }, [muiProps, isInteractive, handleOptionClick]);
  return _jsx(Option, {
    hasAdornment: true,
    muiProps: extendedMuiProps,
    children: _jsxs(CustomOptionContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [_jsxs(CustomOptionLeftContainer, {
        odysseyDesignTokens: odysseyDesignTokens,
        children: [_jsx(OptionAdornment, {
          adornment: adornment,
          adornmentSize: adornmentSize,
          odysseyDesignTokens: odysseyDesignTokens
        }), _jsxs("div", {
          children: [_jsxs(OptionLabelContainer, {
            odysseyDesignTokens: odysseyDesignTokens,
            children: [_jsx(Heading6, {
              component: "p",
              children: label
            }), _jsx(OptionDescriptionComponent, {
              description: description,
              odysseyDesignTokens: odysseyDesignTokens
            })]
          }), metaData && _jsx(OptionMetadataComponent, {
            metaData: metaData,
            odysseyDesignTokens: odysseyDesignTokens
          })]
        })]
      }), extra && _jsx(Extra, {
        content: extra.content,
        odysseyDesignTokens: odysseyDesignTokens,
        onClick: extra.onClick,
        size: extra.size || adornmentSize
      })]
    })
  });
};
const SearchDropdown = ({
  adornmentSize = "small",
  ariaDescribedBy,
  defaultValue,
  errorMessage,
  errorMessageList,
  getIsOptionEqualToValue,
  groupOptionsBy,
  id: idOverride,
  inputValue,
  isCustomValueAllowed,
  isDisabled,
  isFullWidth = false,
  isLoading,
  isOptional = false,
  isReadOnly,
  isVirtualized: isVirtualizedProp = false,
  hint,
  HintLinkComponent,
  label,
  name: nameOverride,
  onBlur,
  onChange: onChangeProp,
  onInputChange: onInputChangeProp,
  onFocus,
  options,
  value,
  testId,
  translate
}) => {
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const customOptionRender = useCallback((muiProps, option) => {
    return _jsx(CustomOption, {
      adornmentSize: adornmentSize,
      muiProps: muiProps,
      odysseyDesignTokens: odysseyDesignTokens,
      option: option
    }, option.value);
  }, [adornmentSize, odysseyDesignTokens]);
  return _jsx(ComposablePicker, {
    ariaDescribedBy: ariaDescribedBy,
    defaultValue: defaultValue,
    errorMessage: errorMessage,
    errorMessageList: errorMessageList,
    getIsOptionEqualToValue: getIsOptionEqualToValue,
    groupOptionsBy: groupOptionsBy,
    hint: hint,
    HintLinkComponent: HintLinkComponent,
    id: idOverride,
    inputValue: inputValue,
    isCustomValueAllowed: isCustomValueAllowed,
    isDisabled: isDisabled,
    isFullWidth: isFullWidth,
    isLoading: isLoading,
    isOptional: isOptional,
    isReadOnly: isReadOnly,
    isVirtualized: isVirtualizedProp,
    label: label,
    name: nameOverride,
    onBlur: onBlur,
    onChange: onChangeProp,
    onFocus: onFocus,
    onInputChange: onInputChangeProp,
    options: options,
    renderOption: customOptionRender,
    testId: testId,
    translate: translate,
    value: value
  });
};
const MemoizedSearchDropdown = memo(SearchDropdown);
MemoizedSearchDropdown.displayName = "MemoizedSearchDropdown";
export { MemoizedSearchDropdown as SearchDropdown };
//# sourceMappingURL=SearchDropdown.js.map