"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SelectedValueWrapper = void 0;
var _pathOr2 = _interopRequireDefault(require("ramda/src/pathOr"));
var _omit2 = _interopRequireDefault(require("ramda/src/omit"));
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _ = require("../../../../..");
var _context = require("../context");
var _NormalizedElements = _interopRequireDefault(require("../../../../../common/NormalizedElements"));
var _utils = require("../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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); }
var FullWidthFlexbox = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "SelectedValue__FullWidthFlexbox",
  componentId: "sc-1b6n5iu-0"
})(["height:100%;width:100%;"]);
var CleanNormalizedButton = _react.default.forwardRef(function (props, ref) {
  return _react.default.createElement(_NormalizedElements.default.Button, _extends({}, (0, _omit2.default)(['absolutePositioning'], props), {
    ref: ref
  }));
});
var StyledA11yButton = (0, _styledComponents.default)(CleanNormalizedButton).withConfig({
  displayName: "SelectedValue__StyledA11yButton",
  componentId: "sc-1b6n5iu-1"
})(["background:", ";width:100%;height:100%;cursor:", ";padding:0;display:flex;border:0;color:inherit;", ""], function (p) {
  return p.disabled ? p.theme.color.disabledBackground : 'transparent';
}, function (p) {
  return p.disabled ? 'not-allowed' : 'pointer';
}, function (p) {
  return !p.absolutePositioning ? '' : "\n      position: absolute;\n      top: 0;\n      bottom: 0;\n      left: 0;\n      right: 0;\n      outline: 0;\n      text-align: initial;\n      ";
});
var SelectedValueWrapper = exports.SelectedValueWrapper = _react.default.forwardRef(function (_ref, ref) {
  var placeholder = _ref.placeholder,
    open = _ref.open,
    Component = _ref.component,
    noFormField = _ref.noFormField,
    disabled = _ref.disabled,
    id = _ref.id,
    label = _ref.label,
    dataTestId = _ref['data-testid'];
  var _useSelectMachineFrom = (0, _context.useSelectMachineFromContext)(),
    _useSelectMachineFrom2 = _slicedToArray(_useSelectMachineFrom, 2),
    state = _useSelectMachineFrom2[0],
    send = _useSelectMachineFrom2[1];
  var value = (0, _utils.getSingleSelectValue)(state.context.selectedItems);
  var screenReaderTextSelection = value.length === 1 ? (0, _pathOr2.default)('', [0, 'label'], value) : "".concat(value.length, " items");
  var screenReaderText = value.length > 0 ? "".concat(label, ": ").concat(screenReaderTextSelection) : "".concat(label, ": ").concat(placeholder);
  var handleClick = function handleClick(e) {
    e.preventDefault();
    send({
      type: 'TOGGLE'
    });
  };
  return _react.default.createElement(_.Typography, {
    type: "secondary",
    color: function color(t) {
      return t.color.text;
    }
  }, _react.default.createElement(StyledA11yButton, {
    id: id,
    type: "button",
    form: "",
    absolutePositioning: !noFormField,
    ref: ref,
    "aria-haspopup": "listbox",
    "aria-expanded": open ? 'true' : 'false',
    onClick: handleClick,
    disabled: disabled,
    tabIndex: 0,
    "aria-label": screenReaderText
  }, _react.default.createElement(FullWidthFlexbox, {
    container: true,
    item: true,
    "aria-hidden": "true"
  }, _react.default.createElement(Component, null))));
});