"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.StyledTypography = void 0;
exports.ValueDisplay = ValueDisplay;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _ = require("../../..");
var _Select = require("../Select.styles");
var _2 = require("..");
var _excluded = ["placeholder"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
function renderValue(selectedOptions) {
  var _selectedOptions$labe;
  if (Array.isArray(selectedOptions)) {
    return _react.default.createElement(_react.default.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;
}
var StyledTypography = exports.StyledTypography = (0, _styledComponents.default)(_.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);
}, _Select.ellipsis);
function ValueDisplay(_ref2) {
  var placeholder = _ref2.placeholder,
    rest = _objectWithoutProperties(_ref2, _excluded);
  var _useSelect = (0, _2.useSelect)(),
    value = _useSelect.value,
    getOptionMetadata = _useSelect.getOptionMetadata;
  var selectedOptionsLabel = value ? getSelectedOptionsLabel(getOptionMetadata, value) : null;
  return _react.default.createElement(StyledTypography, _extends({
    type: "secondary"
  }, rest), selectedOptionsLabel || placeholder);
}