"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var Ariakit = _interopRequireWildcard(require("@ariakit/react"));
var _clsx = _interopRequireDefault(require("clsx"));
var _customSelect = _interopRequireDefault(require("../custom-select"));
var _item = _interopRequireDefault(require("../item"));
var Styled = _interopRequireWildcard(require("../styles"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * External dependencies
 */

/**
 * Internal dependencies
 */

function CustomSelectControl(props) {
  const {
    __experimentalShowSelectedHint = false,
    __next40pxDefaultSize = false,
    describedBy,
    options,
    onChange,
    size = 'default',
    value,
    className: classNameProp,
    ...restProps
  } = props;

  // Forward props + store from v2 implementation
  const store = Ariakit.useSelectStore({
    async setValue(nextValue) {
      const nextOption = options.find(item => item.name === nextValue);
      if (!onChange || !nextOption) {
        return;
      }

      // Executes the logic in a microtask after the popup is closed.
      // This is simply to ensure the isOpen state matches that in Downshift.
      await Promise.resolve();
      const state = store.getState();
      const changeObject = {
        highlightedIndex: state.renderedItems.findIndex(item => item.value === nextValue),
        inputValue: '',
        isOpen: state.open,
        selectedItem: nextOption,
        type: ''
      };
      onChange(changeObject);
    },
    value: value?.name,
    // Setting the first option as a default value when no value is provided
    // is already done natively by the underlying Ariakit component,
    // but doing this explicitly avoids the `onChange` callback from firing
    // on initial render, thus making this implementation closer to the v1.
    defaultValue: options[0]?.name
  });
  const children = options.map(({
    name,
    key,
    __experimentalHint,
    style,
    className
  }) => {
    const withHint = /*#__PURE__*/(0, _jsxRuntime.jsxs)(Styled.WithHintItemWrapper, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: name
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Styled.WithHintItemHint, {
        children: __experimentalHint
      })]
    });
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_item.default, {
      value: name,
      children: __experimentalHint ? withHint : name,
      style: style,
      className: (0, _clsx.default)(
      // TODO: Legacy classname. Add V1 styles are removed from the codebase
      // 'components-custom-select-control__item',
      className
      // TODO: Legacy classname. Add V1 styles are removed from the codebase
      // {
      // 	'has-hint': __experimentalHint,
      // }
      )
    }, key);
  });
  const renderSelectedValueHint = () => {
    const {
      value: currentValue
    } = store.getState();
    const currentHint = options?.find(({
      name
    }) => currentValue === name);
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Styled.SelectedExperimentalHintWrapper, {
      children: [currentValue, /*#__PURE__*/(0, _jsxRuntime.jsx)(Styled.SelectedExperimentalHintItem, {
        children: currentHint?.__experimentalHint
      })]
    });
  };
  const translatedSize = (() => {
    if (__next40pxDefaultSize && size === 'default' || size === '__unstable-large') {
      return 'default';
    }
    if (!__next40pxDefaultSize && size === 'default') {
      return 'compact';
    }
    return size;
  })();
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_customSelect.default, {
    "aria-describedby": describedBy,
    renderSelectedValue: __experimentalShowSelectedHint ? renderSelectedValueHint : undefined,
    size: translatedSize,
    store: store,
    className: (0, _clsx.default)(
    // TODO: Legacy classname. Add V1 styles are removed from the codebase
    // 'components-custom-select-control',
    classNameProp),
    isLegacy: true,
    ...restProps,
    children: children
  });
}
var _default = exports.default = CustomSelectControl;
//# sourceMappingURL=index.js.map