"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SelectControl = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _compose = require("@wordpress/compose");
var _element = require("@wordpress/element");
var _baseControl = _interopRequireDefault(require("../base-control"));
var _selectControlStyles = require("./styles/select-control-styles");
var _chevronDown = _interopRequireDefault(require("./chevron-down"));
var _useDeprecatedProps = require("../utils/use-deprecated-props");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function useUniqueId(idProp) {
  const instanceId = (0, _compose.useInstanceId)(SelectControl);
  const id = `inspector-select-control-${instanceId}`;
  return idProp || id;
}
function UnforwardedSelectControl(props, ref) {
  const {
    className,
    disabled = false,
    help,
    hideLabelFromVision,
    id: idProp,
    label,
    multiple = false,
    onChange,
    options = [],
    size = 'default',
    value: valueProp,
    labelPosition = 'top',
    children,
    prefix,
    suffix,
    __next40pxDefaultSize = false,
    __nextHasNoMarginBottom = false,
    ...restProps
  } = (0, _useDeprecatedProps.useDeprecated36pxDefaultSizeProp)(props);
  const id = useUniqueId(idProp);
  const helpId = help ? `${id}__help` : undefined;

  // Disable reason: A select with an onchange throws a warning.
  if (!options?.length && !children) {
    return null;
  }
  const handleOnChange = event => {
    if (props.multiple) {
      const selectedOptions = Array.from(event.target.options).filter(({
        selected
      }) => selected);
      const newValues = selectedOptions.map(({
        value
      }) => value);
      props.onChange?.(newValues, {
        event
      });
      return;
    }
    props.onChange?.(event.target.value, {
      event
    });
  };
  const classes = (0, _clsx.default)('components-select-control', className);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_baseControl.default, {
    help: help,
    id: id,
    __nextHasNoMarginBottom: __nextHasNoMarginBottom,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_selectControlStyles.StyledInputBase, {
      className: classes,
      disabled: disabled,
      hideLabelFromVision: hideLabelFromVision,
      id: id,
      label: label,
      size: size,
      suffix: suffix || !multiple && /*#__PURE__*/(0, _jsxRuntime.jsx)(_chevronDown.default, {}),
      prefix: prefix,
      labelPosition: labelPosition,
      __next40pxDefaultSize: __next40pxDefaultSize,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_selectControlStyles.Select, {
        ...restProps,
        __next40pxDefaultSize: __next40pxDefaultSize,
        "aria-describedby": helpId,
        className: "components-select-control__input",
        disabled: disabled,
        id: id,
        multiple: multiple,
        onChange: handleOnChange,
        ref: ref,
        selectSize: size,
        value: valueProp,
        children: children || options.map((option, index) => {
          const key = option.id || `${option.label}-${option.value}-${index}`;
          return /*#__PURE__*/(0, _jsxRuntime.jsx)("option", {
            value: option.value,
            disabled: option.disabled,
            hidden: option.hidden,
            children: option.label
          }, key);
        })
      })
    })
  });
}

/**
 * `SelectControl` allows users to select from a single or multiple option menu.
 * It functions as a wrapper around the browser's native `<select>` element.
 *
 * ```jsx
 * import { SelectControl } from '@wordpress/components';
 * import { useState } from '@wordpress/element';
 *
 * const MySelectControl = () => {
 *   const [ size, setSize ] = useState( '50%' );
 *
 *   return (
 *     <SelectControl
 *       label="Size"
 *       value={ size }
 *       options={ [
 *         { label: 'Big', value: '100%' },
 *         { label: 'Medium', value: '50%' },
 *         { label: 'Small', value: '25%' },
 *       ] }
 *       onChange={ setSize }
 *     />
 *   );
 * };
 * ```
 */
const SelectControl = exports.SelectControl = (0, _element.forwardRef)(UnforwardedSelectControl);
var _default = exports.default = SelectControl;
//# sourceMappingURL=index.js.map