/*!
 * 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 { Fragment, memo, useCallback } from "react";
import { Box } from "../../Box.js";
import { useOdysseyDesignTokens } from "../../OdysseyDesignTokensContext.js";
import { Tag } from "../../Tag.js";
import { pxToRem } from "../../theme/pxToRem.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: "relative",
  bottom: "1px",
  alignSelf: "flex-start",
  width: odysseyDesignTokens.Spacing5,
  height: odysseyDesignTokens.Spacing5,
  overflow: "hidden",
  marginInlineEnd: odysseyDesignTokens.Spacing3,
  flexShrink: 0,
  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,
    "&:has(> img)": {
      height: `${pxToRem(16)}rem`
    },
    svg: {
      display: "flex",
      width: "100%",
      height: "auto"
    }
  })
}));
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 OptionWithLabelDescriptionOnly = ({
  adornmentSize,
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    adornment,
    description,
    label,
    value
  } = option;
  return _jsxs(Option, {
    hasAdornment: true,
    muiProps: muiProps,
    children: [_jsx(OptionAdornment, {
      adornment: adornment,
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens
    }), _jsxs(OptionLabelContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [_jsx(Heading6, {
        component: "p",
        children: label
      }), _jsx(OptionDescriptionComponent, {
        description: description,
        odysseyDesignTokens: odysseyDesignTokens
      })]
    })]
  }, value);
};
const OptionWithLabelDescriptionMetadata = ({
  adornmentSize,
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    adornment,
    description,
    label,
    metaData,
    value
  } = option;
  return _jsxs(Option, {
    hasAdornment: true,
    muiProps: muiProps,
    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
        })]
      }), _jsx(OptionMetadataComponent, {
        metaData: metaData,
        odysseyDesignTokens: odysseyDesignTokens
      })]
    })]
  }, value);
};
const TagAdornment = ({
  adornment
}) => {
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const isImageAdornment = typeof adornment === "string";
  if (isImageAdornment) {
    return _jsx(OptionAdornmentContainer, {
      isTagContainer: true,
      odysseyDesignTokens: odysseyDesignTokens,
      children: _jsx("img", {
        alt: "",
        role: "presentation",
        src: adornment
      })
    });
  }
  return _jsx(OptionAdornmentContainer, {
    isTagContainer: true,
    odysseyDesignTokens: odysseyDesignTokens,
    children: adornment
  });
};
const PickerWithOptionAdornment = ({
  adornmentSize = "small",
  ariaDescribedBy,
  defaultValue,
  emptyOptionsText,
  errorMessage,
  errorMessageList,
  getIsOptionEqualToValue,
  groupOptionsBy,
  hasMultipleChoices,
  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 customTagRender = useCallback((values, getTagProps) => values.map((value, index) => {
    const {
      key,
      onDelete
    } = getTagProps({
      index
    });
    const {
      adornment,
      label
    } = value;
    return _jsx(Box, {
      sx: {
        margin: odysseyDesignTokens.Spacing1,
        marginInlineEnd: 0
      },
      children: _jsx(Tag, {
        icon: _jsx(TagAdornment, {
          adornment: adornment
        }),
        label: label,
        onRemove: onDelete
      })
    }, key);
  }), [odysseyDesignTokens]);
  const customOptionRender = useCallback((muiProps, option) => {
    const hasMetadata = "metaData" in option && option.metaData;
    return _jsx(Fragment, {
      children: hasMetadata ? _jsx(OptionWithLabelDescriptionMetadata, {
        adornmentSize: adornmentSize,
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      }) : _jsx(OptionWithLabelDescriptionOnly, {
        adornmentSize: adornmentSize,
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      })
    }, option.value);
  }, [adornmentSize, odysseyDesignTokens]);
  return _jsx(ComposablePicker, {
    ariaDescribedBy: ariaDescribedBy,
    defaultValue: defaultValue,
    emptyOptionsText: emptyOptionsText,
    errorMessage: errorMessage,
    errorMessageList: errorMessageList,
    getIsOptionEqualToValue: getIsOptionEqualToValue,
    groupOptionsBy: groupOptionsBy,
    hasMultipleChoices: hasMultipleChoices,
    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,
    renderTags: customTagRender,
    testId: testId,
    translate: translate,
    value: value
  });
};
const MemoizedPickerWithOptionAdornment = memo(PickerWithOptionAdornment);
MemoizedPickerWithOptionAdornment.displayName = "PickerWithOptionAdornment";
export { MemoizedPickerWithOptionAdornment as PickerWithOptionAdornment };
//# sourceMappingURL=PickerWithOptionAdornment.js.map