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;
  var _useSelect = useSelect(),
    value = _useSelect.value,
    getOptionMetadata = _useSelect.getOptionMetadata;
  var selectedOptionsLabel = value ? getSelectedOptionsLabel(getOptionMetadata, value) : null;
  return React.createElement(React.Fragment, null, React.createElement(StyledTypography, {
    type: "secondary"
  }, selectedOptionsLabel || placeholder));
}