import _pathOr from "ramda/src/pathOr";
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; }
import React from 'react';
import styled from 'styled-components';
import { Flexbox, Typography } from '../../../../..';
import { useSelectMachineFromContext } from '../context';
import NormalizedElements from '../../../../../common/NormalizedElements';
import { getSingleSelectValue } from '../utils';
var FullWidthFlexbox = styled(Flexbox).withConfig({
  displayName: "SelectedValue__FullWidthFlexbox",
  componentId: "sc-1b6n5iu-0"
})(["height:100%;width:100%;"]);
var StyledA11yButton = styled(NormalizedElements.Button).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      ";
});
export var SelectedValueWrapper = React.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 = useSelectMachineFromContext(),
    _useSelectMachineFrom2 = _slicedToArray(_useSelectMachineFrom, 2),
    state = _useSelectMachineFrom2[0],
    send = _useSelectMachineFrom2[1];
  var value = getSingleSelectValue(state.context.selectedItems);
  var screenReaderTextSelection = value.length === 1 ? _pathOr('', [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.createElement(Typography, {
    type: "secondary",
    color: function color(t) {
      return t.color.text;
    }
  }, React.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.createElement(FullWidthFlexbox, {
    container: true,
    item: true,
    "aria-hidden": "true"
  }, React.createElement(Component, null))));
});