"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 _uuid = require("uuid");

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

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

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

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 generateId = function generateId(_ref) {
  var id = _ref.id,
      prefix = _ref.prefix;
  return id === undefined ? "".concat(prefix, "-").concat((0, _uuid.v4)()) : id;
};

var noop = function noop() {};

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

  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 generateId({
      id: id,
      prefix: 'select'
    });
  }, [id]);
  (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
  }, {
    // $FlowFixMe upcast
    spacing: spacing,
    shrink: shrink,
    grow: grow,
    basis: basis
  }, {
    "data-cy": dataCy,
    "data-qa": dataQa,
    id: id
  }, props), label !== undefined && (hideLabel ? /*#__PURE__*/_react["default"].createElement(_primitives.HideAccessibly, null, /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: formSelectId,
    large: large
  }, label)) : /*#__PURE__*/_react["default"].createElement(_fieldLabel.FieldLabel, {
    htmlFor: formSelectId,
    large: large
  }, label)), /*#__PURE__*/_react["default"].createElement(_select.FormSelect, {
    large: large,
    disabled: disabled,
    error: error,
    value: value == null && placeholder != null ? 'UNDEFINED' : value,
    onBlur: onBlur,
    onChange: handleChange,
    onFocus: onFocus,
    id: formSelectId,
    name: name,
    autoFocus: focusOnMount,
    "data-qa": dataQa,
    "data-cy": dataCy,
    isPlaceholderValue: value == null && placeholder != null
  }, placeholder != null ? /*#__PURE__*/_react["default"].createElement("option", {
    value: "UNDEFINED"
  }, String(placeholder)) : null, children));
};

exports.Select = Select;
Select.displayName = 'Select';