"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _taggedTemplateLiteralLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteralLoose"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _core = require("@emotion/core");

var _forms = require("@rebass/forms");

var _ramdaExtension = require("ramda-extension");

var _useBlockingEffect = _interopRequireDefault(require("../hooks/useBlockingEffect"));

var _SuperField = require("../SuperField");

var _Box = _interopRequireDefault(require("../Box"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _templateObject2() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["\n\tfrom {}\n\tto: {}\n"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["\n\tfrom {}\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

var placeholderHidden = {
  opacity: '0 !important'
};
var placeholderVisible = {
  opacity: 0.5
};

var mergeRefs = function mergeRefs(refs) {
  return function (value) {
    refs.filter(Boolean).forEach(function (ref) {
      return (0, _ramdaExtension.isFunction)(ref) ? ref(value) : ref.current = value;
    });
  };
};

var autofill = (0, _core.keyframes)(_templateObject()); // NOTE: make sure, that contents of `autofill` and `autofillCancel` are not the same. Otherwise emotion actually assings both of them same animation.name.

var autofillCancel = (0, _core.keyframes)(_templateObject2());

var getBorderColor = function getBorderColor(_ref) {
  var hasError = _ref.hasError,
      readOnly = _ref.readOnly,
      disabled = _ref.disabled;

  if (hasError) {
    return 'danger';
  } else if (disabled || readOnly) {
    return 'lightGray';
  } else {
    return 'secondary';
  }
};

var Input = (0, _react.forwardRef)(function (props, ref) {
  var _useSuperFieldContext = (0, _SuperField.useSuperFieldContext)(),
      id = _useSuperFieldContext.id,
      isLabelShrank = _useSuperFieldContext.isLabelShrank,
      hasError = _useSuperFieldContext.hasError,
      onFill = _useSuperFieldContext.onFill,
      onEmpty = _useSuperFieldContext.onEmpty,
      onFocus = _useSuperFieldContext.onFocus,
      onBlur = _useSuperFieldContext.onBlur;

  var inputRef = (0, _react.useRef)();
  var internalInputRef = mergeRefs([inputRef, ref]);
  var value = props.value,
      onChange = props.onChange,
      _props$onFocus = props.onFocus,
      onFocusProp = _props$onFocus === void 0 ? function () {} : _props$onFocus,
      _props$onBlur = props.onBlur,
      onBlurProp = _props$onBlur === void 0 ? function () {} : _props$onBlur,
      placeholder = props.placeholder,
      disabled = props.disabled,
      readOnly = props.readOnly,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["value", "onChange", "onFocus", "onBlur", "placeholder", "disabled", "readOnly"]);
  var checkDirty = (0, _react.useCallback)(function (value) {
    if ((0, _SuperField.isInputValueEmpty)(value)) {
      onEmpty();
    } else {
      onFill();
    }
  }, [onEmpty, onFill]);
  (0, _useBlockingEffect.default)(function () {
    checkDirty(value);
  }, [checkDirty, value]);

  var handleAutoFill = function handleAutoFill(event) {
    // Provide a fake value as Chrome might not let you access it for security reasons.
    checkDirty(event.animationName.indexOf(autofillCancel.name) !== -1 ? inputRef.current.value : 'x');
  };

  var handleFocus = function handleFocus(event) {
    if (disabled) {
      return;
    }

    onFocus();
    return onFocusProp(event);
  };

  var handleBlur = function handleBlur(event) {
    if (disabled) {
      return;
    }

    onBlur();
    return onBlurProp(event);
  };

  var handleChange = disabled ? _ramdaExtension.noop : onChange;
  return _react.default.createElement(_Box.default, {
    sx: {
      position: 'relative',
      '&:before': {
        content: "\"\xA0\"",
        display: 'block',
        position: 'absolute',
        bottom: 0,
        left: 0,
        right: 0,
        borderBottomStyle: 'solid',
        borderBottomWidth: '1px',
        borderBottomColor: 'lightGray',
        pointerEvents: 'none'
      },
      '&:hover:before': {
        borderBottomColor: disabled ? 'lightGray' : 'gray'
      },
      '&:after': {
        content: "\"\xA0\"",
        display: 'block',
        position: 'absolute',
        bottom: 0,
        left: 0,
        right: 0,
        borderBottomStyle: 'solid',
        borderBottomWidth: disabled || readOnly ? '1px' : '2px',
        borderBottomColor: getBorderColor(props),
        pointerEvents: 'none',
        transition: 'transform 200ms cubic-bezier(0.0, 0, 0.2, 1) 0ms',
        transform: !isLabelShrank && !hasError ? 'scaleX(0)' : ''
      }
    }
  }, _react.default.createElement(_forms.Input, (0, _extends2.default)({
    sx: _objectSpread({
      color: 'inherit',
      border: 'none',
      animationDuration: '50000s',
      '&:not(:-webkit-autofill)': {
        animationName: autofillCancel,
        animationDuration: '50000s'
      },
      '&:-webkit-autofill': {
        animationName: autofill,
        animationDuration: '50000s'
      },
      WebkitTapHighlightColor: 'transparent',
      '&::placeholder': placeholder,
      '&::WebkitSearchDecoration': {
        // Remove the padding when type=search.
        WebkitAppearance: 'none'
      }
    }, !isLabelShrank ? {
      '&::placeholder': placeholderHidden
    } : {
      '&:placeholder': placeholderVisible
    }, {
      // FF
      '&:invalid': {
        boxShadow: 'none'
      },
      '&:focus': {
        outline: 0
      },
      width: '100%',
      fontSize: [2, 4]
    }),
    px: 0,
    id: id,
    onAnimationStart: handleAutoFill,
    value: value,
    onChange: handleChange,
    onFocus: handleFocus,
    onBlur: handleBlur,
    placeholder: placeholder,
    disabled: disabled,
    readOnly: readOnly,
    ref: internalInputRef
  }, rest)));
});
Input.displayName = 'Input';
Input.propTypes = {
  disabled: _propTypes.default.bool,
  onBlur: _propTypes.default.func,
  onChange: _propTypes.default.func,
  onFocus: _propTypes.default.func,
  placeholder: _propTypes.default.string,
  readOnly: _propTypes.default.bool,
  value: _propTypes.default.any.isRequired
};
var _default = Input;
exports.default = _default;