/*!
 * 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, createElement as _createElement } from "react";
import { useOdysseyDesignTokens } from "../../OdysseyDesignTokensContext.js";
import { Heading6, Paragraph } from "../../Typography.js";
import { ComposablePicker } from "./ComposablePicker.js";
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
export const StyledOption = styled.li(({
  hasAdornment
}) => ({
  ...(!hasAdornment && {
    display: "block !important"
  })
}));
export const OptionLabelContainer = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(() => ({
  p: {
    margin: 0
  }
}));
export const OptionDescription = styled("p", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  margin: `${odysseyDesignTokens.Spacing1} 0 0 !important`,
  color: odysseyDesignTokens.TypographyColorSubordinate
}));
export const OptionDetails = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  display: "flex",
  gap: odysseyDesignTokens.Spacing3,
  marginBlockStart: odysseyDesignTokens.Spacing2
}));
export const OptionDetail = styled("div", {
  shouldForwardProp: prop => prop !== "odysseyDesignTokens"
})(({
  odysseyDesignTokens
}) => ({
  display: "flex",
  alignItems: "center",
  gap: odysseyDesignTokens.Spacing1,
  svg: {
    width: odysseyDesignTokens.Spacing4,
    color: odysseyDesignTokens.HueNeutral400
  }
}));
export const Option = ({
  children,
  hasAdornment = false,
  muiProps
}) => {
  const {
    key,
    ...props
  } = muiProps;
  return _createElement(StyledOption, {
    ...props,
    hasAdornment: hasAdornment,
    key: key
  }, children);
};
export const OptionMetadataComponent = ({
  metaData,
  odysseyDesignTokens
}) => {
  return _jsx(OptionDetails, {
    odysseyDesignTokens: odysseyDesignTokens,
    children: metaData.map((meta, index) => {
      const {
        detailText,
        icon
      } = meta;
      return _jsxs(OptionDetail, {
        odysseyDesignTokens: odysseyDesignTokens,
        children: [icon, detailText]
      }, `${detailText}-${index}`);
    })
  });
};
export const OptionDescriptionComponent = ({
  description,
  odysseyDesignTokens
}) => {
  return description && _jsx(OptionDescription, {
    odysseyDesignTokens: odysseyDesignTokens,
    children: description
  });
};
export const OptionLabelOnlyComponent = ({
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    label
  } = option;
  return _jsx(Option, {
    muiProps: muiProps,
    children: _jsx(OptionLabelContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: _jsx(Paragraph, {
        children: label
      })
    })
  });
};
const OptionLabelDescription = ({
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    description,
    label
  } = option;
  return _jsx(Option, {
    muiProps: muiProps,
    children: _jsxs(OptionLabelContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [_jsx(Heading6, {
        component: "p",
        children: label
      }), _jsx(OptionDescriptionComponent, {
        description: description,
        odysseyDesignTokens: odysseyDesignTokens
      })]
    })
  });
};
const OptionLabelDescriptionMetadata = ({
  muiProps,
  odysseyDesignTokens,
  option
}) => {
  const {
    description,
    label,
    metaData
  } = option;
  return _jsxs(Option, {
    muiProps: muiProps,
    children: [_jsxs(OptionLabelContainer, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: [_jsx(Heading6, {
        component: "p",
        children: label
      }), _jsx(OptionDescriptionComponent, {
        description: description,
        odysseyDesignTokens: odysseyDesignTokens
      })]
    }), _jsx(OptionMetadataComponent, {
      metaData: metaData,
      odysseyDesignTokens: odysseyDesignTokens
    })]
  });
};
const Picker = ({
  ariaDescribedBy,
  defaultValue,
  emptyOptionsText,
  errorMessage,
  errorMessageList,
  getIsOptionEqualToValue,
  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 customOptionRender = useCallback((muiProps, option) => {
    const hasDescription = "description" in option && option.description;
    const hasMetadata = "metaData" in option && option.metaData;
    const isLabelOnly = !hasMetadata && !hasDescription;
    return _jsx(Fragment, {
      children: isLabelOnly ? _jsx(OptionLabelOnlyComponent, {
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      }) : hasMetadata ? _jsx(OptionLabelDescriptionMetadata, {
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      }) : _jsx(OptionLabelDescription, {
        muiProps: muiProps,
        odysseyDesignTokens: odysseyDesignTokens,
        option: option
      })
    }, option.value);
  }, [odysseyDesignTokens]);
  return _jsx(ComposablePicker, {
    ariaDescribedBy: ariaDescribedBy,
    defaultValue: defaultValue,
    emptyOptionsText: emptyOptionsText,
    errorMessage: errorMessage,
    errorMessageList: errorMessageList,
    getIsOptionEqualToValue: getIsOptionEqualToValue,
    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,
    testId: testId,
    translate: translate,
    value: value
  });
};
const MemoizedPicker = memo(Picker);
MemoizedPicker.displayName = "Picker";
export { MemoizedPicker as Picker };
//# sourceMappingURL=Picker.js.map