"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormFieldOrFragment = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _ = require("../../../../..");
var _excluded = ["children", "noFormField", "open", "onFocus", "onBlur", "fullWidth", "id", "size", "labelToolTip", "labelTooltipPosition", "labelTooltipInModal", "innerRef"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
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 Chevron = (0, _styledComponents.default)(_.OldIcon.ChevronDown).withConfig({
  displayName: "FormFieldOrFragment__Chevron",
  componentId: "sc-1y3h7ub-0"
})(["transform:translateY(-50%) ", ";transform-origin:center center;transition:transform 0.16s ease-out;position:absolute;height:", "px;top:50%;right:", "px;pointer-events:none;"], function (p) {
  return p.open ? 'rotate(180deg)' : 'rotate(0)';
}, function (p) {
  return p.theme.spacing.unit(2);
}, function (p) {
  return p.theme.spacing.unit(1);
});
var StyledRelativeDiv = _styledComponents.default.div.withConfig({
  displayName: "FormFieldOrFragment__StyledRelativeDiv",
  componentId: "sc-1y3h7ub-1"
})(["position:relative;display:block;width:", ";"], function (p) {
  return p.fullWidth ? '100%' : 'auto';
});
var determineHeight = function determineHeight(p) {
  if (p.height) {
    return p.theme.spacing.unit(p.height);
  }
  return p.size === 's' ? p.theme.spacing.unit(8) : p.theme.spacing.unit(10);
};
var height = (0, _styledComponents.css)(["height:", "px;"], determineHeight);
var hoverBorderStyles = (0, _styledComponents.css)(["", ""], function (p) {
  return p.disabled ? '' : "\n    &:hover {\n      border-color: ".concat(p.theme.color.inputBorderHover, ";\n    }\n");
});
var focusBorderStyles = (0, _styledComponents.css)(["&:focus-within{border-color:", ";}&.focus-within{border-color:", ";}"], function (p) {
  return p.theme.color.borderActive;
}, function (p) {
  return p.theme.color.borderActive;
});
var hasError = function hasError(error) {
  return error && error !== '';
};
var borderStyles = (0, _styledComponents.css)(["border:1px solid ", ";position:relative;", " ", ""], function (p) {
  if (hasError(p.error)) return p.theme.color.inputBorderError;
  if (p.success) return p.theme.color.inputBorderSuccess;
  return p.theme.color.inputBorder;
}, hoverBorderStyles, focusBorderStyles);
var SelectWrapper = _styledComponents.default.div.withConfig({
  displayName: "FormFieldOrFragment__SelectWrapper",
  componentId: "sc-1y3h7ub-2"
})(["", " ", " background:", ";box-sizing:border-box;position:relative;border-radius:", ";"], height, borderStyles, function (p) {
  return p.theme.color.inputBackground;
}, function (_ref) {
  var theme = _ref.theme;
  return theme.borderRadius4;
});
var FormFieldOrFragment = exports.FormFieldOrFragment = _react.default.forwardRef(function (_ref2, ref) {
  var children = _ref2.children,
    noFormField = _ref2.noFormField,
    open = _ref2.open,
    onFocus = _ref2.onFocus,
    onBlur = _ref2.onBlur,
    fullWidth = _ref2.fullWidth,
    id = _ref2.id,
    size = _ref2.size,
    labelToolTip = _ref2.labelToolTip,
    labelTooltipPosition = _ref2.labelTooltipPosition,
    labelTooltipInModal = _ref2.labelTooltipInModal,
    innerRef = _ref2.innerRef,
    props = _objectWithoutProperties(_ref2, _excluded);
  if (noFormField) {
    return _react.default.createElement(StyledRelativeDiv, {
      tabIndex: 0,
      ref: ref,
      fullWidth: fullWidth,
      width: props.width,
      onBlur: onBlur,
      onFocus: onFocus
    }, _react.default.createElement(_.Flexbox, _extends({
      ref: innerRef,
      container: true,
      alignItems: "center"
    }, fullWidth ? {
      width: '100%'
    } : {}), children));
  }
  return _react.default.createElement(StyledRelativeDiv, {
    fullWidth: fullWidth,
    width: props.width
  }, _react.default.createElement(_.Flexbox, _extends({
    container: true,
    alignItems: "center"
  }, fullWidth ? {
    width: '100%'
  } : {}), _react.default.createElement(_.FormField, _extends({
    fieldId: id,
    labelTooltip: labelToolTip,
    labelTooltipPosition: labelTooltipPosition,
    labelTooltipInModal: labelTooltipInModal
  }, props, fullWidth ? {
    width: '100%'
  } : {}, {
    ref: ref
  }), _react.default.createElement(SelectWrapper, _extends({
    ref: innerRef,
    onBlur: onBlur,
    onFocus: onFocus,
    size: size
  }, props, {
    tabIndex: 0
  }), children, _react.default.createElement(Chevron, {
    open: open
  })))));
});
FormFieldOrFragment.displayName = 'FormFieldOrFragment';