"use strict";
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const assert = require("assert");
const styles_lib_components_Accordion_AccordionItem_cjs = require("../Accordion/AccordionItem.cjs");
const styles_lib_components_Box_Box_cjs = require("../Box/Box.cjs");
const styles_lib_components_Heading_Heading_cjs = require("../Heading/Heading.cjs");
const styles_lib_components_Accordion_Accordion_cjs = require("../Accordion/Accordion.cjs");
const styles_lib_components_TextDropdown_TextDropdown_css_cjs = require("./TextDropdown.css.cjs");
const styles_lib_components_icons_IconChevron_IconChevron_cjs = require("../icons/IconChevron/IconChevron.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert);
function parseSimpleToComplexOption(option) {
  return typeof option === "object" && option !== null && "text" in option && "value" in option ? option : { value: option, text: String(option) };
}
function TextDropdown({
  id,
  value,
  onChange,
  onBlur,
  options,
  label,
  data,
  ...restProps
}) {
  assert__default.default(
    (() => {
      const inText = React.useContext(styles_lib_components_Accordion_AccordionItem_cjs.TextContext);
      const inHeading = React.useContext(styles_lib_components_Heading_Heading_cjs.HeadingContext);
      return inText || inHeading;
    })(),
    "TextDropdown components must be rendered within a Text or Heading component. See the documentation for correct usage: https://seek-oss.github.io/braid-design-system/components/TextDropdown"
  );
  const parsedOptions = options.map(parseSimpleToComplexOption);
  const [currentText] = parsedOptions.filter((o) => value === o.value);
  if (!currentText || !currentText.text) {
    throw new Error(
      `The provided value of "${value}" does not exist in the provided \`options\` list.`
    );
  }
  return /* @__PURE__ */ jsxRuntime.jsxs(
    styles_lib_components_Box_Box_cjs.Box,
    {
      display: "inlineBlock",
      position: "relative",
      ...styles_lib_components_Accordion_Accordion_cjs.buildDataAttributes({ data, validateRestProps: restProps }),
      children: [
        /* @__PURE__ */ jsxRuntime.jsxs(styles_lib_components_Box_Box_cjs.Box, { pointerEvents: "none", userSelect: "none", children: [
          currentText.text,
          " ",
          /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_icons_IconChevron_IconChevron_cjs.IconChevron, { alignY: "lowercase" })
        ] }),
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Box_Box_cjs.Box,
          {
            component: "select",
            position: "absolute",
            inset: 0,
            width: "full",
            opacity: 0,
            className: styles_lib_components_TextDropdown_TextDropdown_css_cjs.select,
            "aria-label": label,
            title: label,
            id,
            value: String(value),
            onChange: (ev) => {
              if (typeof onChange === "function") {
                const newValue = parsedOptions[ev.currentTarget.selectedIndex].value;
                onChange(newValue);
              }
            },
            onBlur,
            children: /* @__PURE__ */ jsxRuntime.jsx("optgroup", { label, children: parsedOptions.map((option) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: String(option.value), children: option.text }, String(option.value))) })
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsx(
          styles_lib_components_Accordion_AccordionItem_cjs.Overlay,
          {
            boxShadow: "outlineFocus",
            borderRadius: "standard",
            transition: "fast",
            onlyVisibleForKeyboardNavigation: true,
            className: styles_lib_components_TextDropdown_TextDropdown_css_cjs.focusOverlay
          }
        )
      ]
    }
  );
}
exports.TextDropdown = TextDropdown;
exports.parseSimpleToComplexOption = parseSimpleToComplexOption;
