"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 _randomstring = _interopRequireDefault(require("randomstring"));

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

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

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

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; }

var generateId = function generateId(_ref) {
  var id = _ref.id,
      prefix = _ref.prefix;
  var finalId = id === undefined ? "".concat(prefix, "-").concat(_randomstring["default"].generate({
    length: 7,
    charset: 'alphabetic'
  })) : id;
  return finalId;
};

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,
      onChange = _ref2.onChange;
  var handleChange = (0, _react.useCallback)(function (event) {
    var newValue = event.target.value;
    onChange(newValue === 'UNDEFINED' ? undefined : newValue);
  }, [onChange]);
  var formSelectId = generateId({
    id: id,
    prefix: 'select'
  });
  return /*#__PURE__*/_react["default"].createElement(_primitives.Box, {
    stacked: "column",
    className: className
  }, 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
  }, placeholder != null ? /*#__PURE__*/_react["default"].createElement("option", {
    value: "UNDEFINED"
  }, placeholder) : null, children));
};

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