"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getIconDataUri = exports.getPlaceholderColor = exports.getTextColor = exports.getInputIconPosition = exports.getInputLeftPadding = exports.getInputBorder = exports.getBackgroundColor = exports.getInputFontSize = exports.getInputVerticalPadding = void 0;

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

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

var _server = require("react-dom/server");

var _branding = require("../../../../mixins/branding");

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

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

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

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

var hasError = function hasError(_ref) {
  var error = _ref.error;
  return error === true || typeof error === 'string' || _typeof(error) === 'object';
};

var getInputVerticalPadding = function getInputVerticalPadding(_ref2) {
  var large = _ref2.large,
      fontSize = _ref2.fontSize;

  if (fontSize === 'f5') {
    return '15px';
  }

  return large ? '10px' : '2px';
};

exports.getInputVerticalPadding = getInputVerticalPadding;

var getInputFontSize = function getInputFontSize(_ref3) {
  var fontSize = _ref3.fontSize,
      large = _ref3.large;

  if (fontSize == null) {
    return large ? 'f3' : 'f1';
  }

  return fontSize;
};

exports.getInputFontSize = getInputFontSize;

var getBackgroundColor = function getBackgroundColor(_ref4) {
  var hideBorder = _ref4.hideBorder,
      disabled = _ref4.disabled;
  return disabled && !hideBorder ? (0, _branding.getPaletteColor)('background') : 'white';
};

exports.getBackgroundColor = getBackgroundColor;

var getInputBorder = function getInputBorder(_ref5) {
  var hideBorder = _ref5.hideBorder;
  return hideBorder ? 'none' : "1px solid";
};

exports.getInputBorder = getInputBorder;

var getInputLeftPadding = function getInputLeftPadding(_ref6) {
  var large = _ref6.large,
      iconType = _ref6.iconType,
      _ref6$iconPosition = _ref6.iconPosition,
      iconPosition = _ref6$iconPosition === void 0 ? 'left' : _ref6$iconPosition,
      _ref6$hideBorder = _ref6.hideBorder,
      hideBorder = _ref6$hideBorder === void 0 ? false : _ref6$hideBorder;

  if (hideBorder) {
    return '0px';
  }

  if (iconType && iconPosition === 'left') {
    return large ? '60px' : '40px';
  }

  return large ? '30px' : '20px';
};

exports.getInputLeftPadding = getInputLeftPadding;

var getInputIconPosition = function getInputIconPosition(_ref7) {
  var _ref7$iconPosition = _ref7.iconPosition,
      iconPosition = _ref7$iconPosition === void 0 ? 'left' : _ref7$iconPosition,
      rest = _objectWithoutProperties(_ref7, ["iconPosition"]);

  var finalIconPosition = iconPosition;

  if (hasError(rest)) {
    finalIconPosition = 'right';
  }

  return "".concat(finalIconPosition, " center");
};

exports.getInputIconPosition = getInputIconPosition;

var getTextColor = function getTextColor(props) {
  return (0, _branding.getPaletteColor)(hasError(props) ? 'error-fg' : 'greyscale-black');
};

exports.getTextColor = getTextColor;

var getPlaceholderColor = function getPlaceholderColor() {
  return (0, _branding.getPaletteColor)('greyscale-dark');
};

exports.getPlaceholderColor = getPlaceholderColor;

var getIcon = function getIcon(props) {
  if (hasError(props)) {
    return /*#__PURE__*/_react["default"].createElement(_icon.ErrorIcon, null);
  }

  switch (props.iconType) {
    case 'error':
      return /*#__PURE__*/_react["default"].createElement(_icon.ErrorIcon, null);

    case 'lock':
      return /*#__PURE__*/_react["default"].createElement(_icon.Lock, {
        disabled: props.disabled
      });

    default:
      return null;
  }
};

var getIconDataUri = function getIconDataUri(props) {
  if (!props.iconType && !hasError(props)) return undefined;

  var themed = /*#__PURE__*/_react["default"].createElement(_styledComponents.ThemeProvider, {
    theme: props.theme
  }, getIcon(props));

  var encodedIcon = encodeURIComponent((0, _server.renderToString)(themed));
  return "url(data:image/svg+xml;utf8,".concat(encodedIcon, ")");
};

exports.getIconDataUri = getIconDataUri;