var _excluded = ["placeholder"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from 'react';
import styled from 'styled-components';
import { Typography } from '../../..';
import { ellipsis } from '../Select.styles';
import { useSelect } from '..';
function renderValue(selectedOptions) {
  var _selectedOptions$labe;
  if (Array.isArray(selectedOptions)) {
    return React.createElement(React.Fragment, null, selectedOptions.map(function (o) {
      return o.label;
    }).join(', '));
  }
  return (_selectedOptions$labe = selectedOptions === null || selectedOptions === void 0 ? void 0 : selectedOptions.label) !== null && _selectedOptions$labe !== void 0 ? _selectedOptions$labe : '';
}
function getSelectedOptionsLabel(getOptionMetadata, value) {
  var selectedOptionsMetadata = null;
  if (value) {
    if (Array.isArray(value)) {
      selectedOptionsMetadata = value.map(function (v) {
        return getOptionMetadata(v);
      }).filter(function (o) {
        return o !== undefined;
      });
    } else {
      selectedOptionsMetadata = getOptionMetadata(value);
    }
  }
  return selectedOptionsMetadata ? renderValue(selectedOptionsMetadata) : null;
}
export var StyledTypography = styled(Typography).withConfig({
  displayName: "ValueDisplay__StyledTypography",
  componentId: "sc-1e53f4y-0"
})(["display:inline-block;margin-right:", "px;color:inherit;", ""], function (_ref) {
  var theme = _ref.theme;
  return theme.spacing.unit(2);
}, ellipsis);
export function ValueDisplay(_ref2) {
  var placeholder = _ref2.placeholder,
    rest = _objectWithoutProperties(_ref2, _excluded);
  var _useSelect = useSelect(),
    value = _useSelect.value,
    getOptionMetadata = _useSelect.getOptionMetadata;
  var selectedOptionsLabel = value ? getSelectedOptionsLabel(getOptionMetadata, value) : null;
  return React.createElement(StyledTypography, _extends({
    type: "secondary"
  }, rest), selectedOptionsLabel || placeholder);
}