"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Select = void 0;

var _react = _interopRequireWildcard(require("react"));

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _uuid = require("uuid");

var _select = require("./select.css-rules");

var _primitives = require("../../../primitives");

var _forms = require("../internals/forms.common");

var _icon = require("../../icon");

var _fieldLabel = require("../../typography/field-label");

var _hideAccessibly = require("../../../primitives/hide-accessibly");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || 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; }

var noop = function noop() {};

var Select = function Select(_ref) {
  var children = _ref.children,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === void 0 ? false : _ref$disabled,
      error = _ref.error,
      className = _ref.className,
      placeholder = _ref.placeholder,
      value = _ref.value,
      _ref$onBlur = _ref.onBlur,
      onBlur = _ref$onBlur === void 0 ? noop : _ref$onBlur,
      _ref$onFocus = _ref.onFocus,
      onFocus = _ref$onFocus === void 0 ? noop : _ref$onFocus,
      label = _ref.label,
      id = _ref.id,
      name = _ref.name,
      onChange = _ref.onChange,
      spacing = _ref.spacing,
      shrink = _ref.shrink,
      grow = _ref.grow,
      basis = _ref.basis,
      focusOnMount = _ref.focusOnMount,
      success = _ref.success,
      description = _ref.description,
      _ref$labelStyle = _ref.labelStyle,
      labelStyle = _ref$labelStyle === void 0 ? 'floating' : _ref$labelStyle,
      dataQa = _ref['data-qa'],
      dataCy = _ref['data-cy'],
      props = _objectWithoutProperties(_ref, ["children", "disabled", "error", "className", "placeholder", "value", "onBlur", "onFocus", "label", "id", "name", "onChange", "spacing", "shrink", "grow", "basis", "focusOnMount", "success", "description", "labelStyle", "data-qa", "data-cy"]);

  var errorString = error != null && typeof error !== 'boolean' ? error : null;
  var handleChange = (0, _react.useCallback)(function (event) {
    var newValue = event.target.value;
    onChange(newValue === 'UNDEFINED' ? undefined : newValue);
  }, [onChange]);
  var formSelectId = (0, _react.useMemo)(function () {
    return "select-".concat(id !== null && id !== void 0 ? id : '', "-").concat((0, _uuid.v4)());
  }, [id]);
  var descriptionId = (0, _react.useMemo)(function () {
    return "description-".concat(id !== null && id !== void 0 ? id : '', "-").concat((0, _uuid.v4)());
  }, [id]);
  var hasErrorMessage = error != null && typeof error !== 'boolean';
  var showMessage = hasErrorMessage || description != null && description !== '' || success != null && success !== '';
  (0, _react.useEffect)(function () {
    if (value == null && placeholder == null) {
      // eslint-disable-next-line no-console
      console.warn('Patchwork Selector Usage error: you must supply a placeholder prop if value is allowed to be null or undefined.\n', new Error().stack);
    }
  }, [value, placeholder]);
  return /*#__PURE__*/_react["default"].createElement(_primitives.Box, _extends({
    stacked: "column",
    className: className,
    spacing: spacing,
    shrink: shrink,
    grow: grow,
    basis: basis,
    "data-cy": dataCy,
    "data-qa": dataQa,
    id: id
  }, props), labelStyle === 'external' && /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: formSelectId
  }, label), labelStyle === 'external-large' && /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: formSelectId,
    spacing: "mb2"
  }, label), labelStyle === 'hidden' && /*#__PURE__*/_react["default"].createElement(_hideAccessibly.HideAccessibly, null, /*#__PURE__*/_react["default"].createElement("label", {
    htmlFor: formSelectId
  }, label)), /*#__PURE__*/_react["default"].createElement(_forms.FormBox, {
    stacked: "row",
    align: "center",
    spacing: "pr1",
    disabled: disabled,
    labelStyle: labelStyle
  }, labelStyle === 'floating' && /*#__PURE__*/_react["default"].createElement(_forms.FloatingLabel, {
    htmlFor: formSelectId,
    "data-has-value": value != null && value !== ''
  }, label), /*#__PURE__*/_react["default"].createElement(_select.FormSelect, {
    disabled: disabled,
    error: error,
    value: value == null && placeholder != null ? 'UNDEFINED' : value,
    onBlur: onBlur,
    onChange: handleChange,
    onFocus: onFocus,
    id: formSelectId,
    name: name,
    autoFocus: focusOnMount,
    isPlaceholderValue: value == null && placeholder != null,
    labelStyle: labelStyle,
    "aria-invalid": !!error,
    "data-has-value": value != null && value !== ''
  }, placeholder != null ? /*#__PURE__*/_react["default"].createElement("option", {
    value: "UNDEFINED"
  }, String(placeholder)) : null, children), /*#__PURE__*/_react["default"].createElement(IconContainer, {
    shrink: 0,
    stacked: "row",
    align: "center",
    "aria-hidden": true
  }, error != null && error !== false && /*#__PURE__*/_react["default"].createElement(_icon.ErrorIcon, null), /*#__PURE__*/_react["default"].createElement(_icon.ChevronDown, null))), /*#__PURE__*/_react["default"].createElement(_primitives.Box, {
    id: descriptionId,
    spacing: showMessage ? 'mb1' : 'mb0'
  }, /*#__PURE__*/_react["default"].createElement(_primitives.Box, {
    "aria-live": "polite"
  }, errorString != null && /*#__PURE__*/_react["default"].createElement(_forms.ErrorMessage, {
    message: errorString
  }), success != null && /*#__PURE__*/_react["default"].createElement(_forms.SuccessMessage, {
    message: success
  })), description != null && /*#__PURE__*/_react["default"].createElement(_forms.Description, {
    message: description
  })));
};

exports.Select = Select;
Select.displayName = 'Select'; // Absolute, no pointer events because we want clicking on
// this element to activate the Select

var IconContainer = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "IconContainer",
  componentId: "sc-1o8mn7m-0"
})(["position:absolute;right:0px;top:0px;bottom:0px;pointer-events:none;"]);