"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getIconDataUri = exports.getPlaceholderColor = exports.getTextColor = exports.getInputIconPosition = exports.getInputLeftPadding = exports.getInputBorder = exports.getInputHeight = 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 getInputHeight = function getInputHeight(_ref2) {
  var large = _ref2.large;
  return large ? '60px' : '40px';
};

exports.getInputHeight = getInputHeight;

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

exports.getInputBorder = getInputBorder;

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

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

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

exports.getInputLeftPadding = getInputLeftPadding;

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

  var finalIconPosition = iconPosition;

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

  if (iconType === 'checkmark' && iconPosition === 'right') {
    return "right ".concat(large ? '18px' : '10px', " center");
  }

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

exports.getInputIconPosition = getInputIconPosition;

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

  if (props.disabled) {
    return (0, _branding.getPaletteColor)('greyscale-dark');
  }

  return (0, _branding.getPaletteColor)('greyscale-black');
};

exports.getTextColor = getTextColor;

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

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

    case 'checkmark':
      return /*#__PURE__*/_react["default"].createElement(_icon.Checkmark, {
        inverted: true
      });

    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;