"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PickerWithOptionAdornment = void 0;
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _react = require("react");
var _Box = require("../../Box.cjs");
var _OdysseyDesignTokensContext = require("../../OdysseyDesignTokensContext.cjs");
var _Tag = require("../../Tag.cjs");
var _pxToRem = require("../../theme/pxToRem.cjs");
var _Typography = require("../../Typography.cjs");
var _ComposablePicker = require("./ComposablePicker.cjs");
var _Picker = require("./Picker.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * 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.
 */

const OptionAdornmentContainer = (0, _styled.default)("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: `${(0, _pxToRem.pxToRem)(16)}rem`
    },
    svg: {
      display: "flex",
      width: "100%",
      height: "auto"
    }
  })
}));
const OptionAdornment = ({
  adornment,
  adornmentSize,
  odysseyDesignTokens
}) => {
  const isImageAdornment = typeof adornment === "string";
  if (isImageAdornment) {
    return (0, _jsxRuntime.jsx)(OptionAdornmentContainer, {
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens,
      children: (0, _jsxRuntime.jsx)("img", {
        alt: "",
        role: "presentation",
        src: adornment
      })
    });
  } else {
    return (0, _jsxRuntime.jsx)(OptionAdornmentContainer, {
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens,
      children: adornment
    });
  }
};
const OptionWithLabelDescriptionOnly = ({
  adornmentSize,
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    adornment,
    description,
    label,
    value
  } = option;
  return (0, _jsxRuntime.jsxs)(_Picker.Option, {
    hasAdornment: true,
    muiProps: muiProps,
    children: [(0, _jsxRuntime.jsx)(OptionAdornment, {
      adornment: adornment,
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens
    }), (0, _jsxRuntime.jsxs)(_Picker.OptionLabelContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [(0, _jsxRuntime.jsx)(_Typography.Heading6, {
        component: "p",
        children: label
      }), (0, _jsxRuntime.jsx)(_Picker.OptionDescriptionComponent, {
        description: description,
        odysseyDesignTokens: odysseyDesignTokens
      })]
    })]
  }, value);
};
const OptionWithLabelDescriptionMetadata = ({
  adornmentSize,
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    adornment,
    description,
    label,
    metaData,
    value
  } = option;
  return (0, _jsxRuntime.jsxs)(_Picker.Option, {
    hasAdornment: true,
    muiProps: muiProps,
    children: [(0, _jsxRuntime.jsx)(OptionAdornment, {
      adornment: adornment,
      adornmentSize: adornmentSize,
      odysseyDesignTokens: odysseyDesignTokens
    }), (0, _jsxRuntime.jsxs)("div", {
      children: [(0, _jsxRuntime.jsxs)(_Picker.OptionLabelContainer, {
        odysseyDesignTokens: odysseyDesignTokens,
        children: [(0, _jsxRuntime.jsx)(_Typography.Heading6, {
          component: "p",
          children: label
        }), (0, _jsxRuntime.jsx)(_Picker.OptionDescriptionComponent, {
          description: description,
          odysseyDesignTokens: odysseyDesignTokens
        })]
      }), (0, _jsxRuntime.jsx)(_Picker.OptionMetadataComponent, {
        metaData: metaData,
        odysseyDesignTokens: odysseyDesignTokens
      })]
    })]
  }, value);
};
const TagAdornment = ({
  adornment
}) => {
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const isImageAdornment = typeof adornment === "string";
  if (isImageAdornment) {
    return (0, _jsxRuntime.jsx)(OptionAdornmentContainer, {
      isTagContainer: true,
      odysseyDesignTokens: odysseyDesignTokens,
      children: (0, _jsxRuntime.jsx)("img", {
        alt: "",
        role: "presentation",
        src: adornment
      })
    });
  }
  return (0, _jsxRuntime.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 = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const customTagRender = (0, _react.useCallback)((values, getTagProps) => values.map((value, index) => {
    const {
      key,
      onDelete
    } = getTagProps({
      index
    });
    const {
      adornment,
      label
    } = value;
    return (0, _jsxRuntime.jsx)(_Box.Box, {
      sx: {
        margin: odysseyDesignTokens.Spacing1,
        marginInlineEnd: 0
      },
      children: (0, _jsxRuntime.jsx)(_Tag.Tag, {
        icon: (0, _jsxRuntime.jsx)(TagAdornment, {
          adornment: adornment
        }),
        label: label,
        onRemove: onDelete
      })
    }, key);
  }), [odysseyDesignTokens]);
  const customOptionRender = (0, _react.useCallback)((muiProps, option) => {
    const hasMetadata = "metaData" in option && option.metaData;
    return (0, _jsxRuntime.jsx)(_react.Fragment, {
      children: hasMetadata ? (0, _jsxRuntime.jsx)(OptionWithLabelDescriptionMetadata, {
        adornmentSize: adornmentSize,
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      }) : (0, _jsxRuntime.jsx)(OptionWithLabelDescriptionOnly, {
        adornmentSize: adornmentSize,
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      })
    }, option.value);
  }, [adornmentSize, odysseyDesignTokens]);
  return (0, _jsxRuntime.jsx)(_ComposablePicker.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 = exports.PickerWithOptionAdornment = (0, _react.memo)(PickerWithOptionAdornment);
MemoizedPickerWithOptionAdornment.displayName = "PickerWithOptionAdornment";
//# sourceMappingURL=PickerWithOptionAdornment.cjs.map