"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

var _reactNative = require("react-native");

var _hooks = require("../../../hooks");

var _FormControl = require("../../composites/FormControl");

var _useHasResponsiveProps = require("../../../hooks/useHasResponsiveProps");

var _interactions = require("@react-native-aria/interactions");

var _utils = require("../../../theme/tools/utils");

var _useThemeProps = require("../../../hooks/useThemeProps");

var _utils2 = require("../../../utils");

var _Stack = require("../Stack");

var _makeStyledComponent = require("../../../utils/makeStyledComponent");

var _useResolvedFontFamily = require("../../../hooks/useResolvedFontFamily");

var _ResolvedStyleMap = require("../../../core/ResolvedStyleMap");

var _colorMode = require("../../../core/color-mode");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { 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; }

function _extends() { _extends = Object.assign || 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); }

const StyledInput = (0, _makeStyledComponent.makeStyledComponent)(_reactNative.TextInput);

const Input = ({
  isHovered: isHoveredProp,
  isFocused: isFocusedProp,
  onKeyPress,
  ...props
}, ref) => {
  const inputProps = (0, _FormControl.useFormControl)({
    isDisabled: props.isDisabled,
    isInvalid: props.isInvalid,
    isReadOnly: props.isReadOnly,
    isRequired: props.isRequired,
    nativeID: props.nativeID
  });
  const inputThemeProps = {
    isDisabled: inputProps.disabled,
    isInvalid: inputProps.accessibilityInvalid,
    isReadOnly: inputProps.accessibilityReadOnly,
    isRequired: inputProps.required
  };

  const [isFocused, setIsFocused] = _react.default.useState(false);

  const _ref = _react.default.useRef(null);

  const {
    isHovered
  } = (0, _interactions.useHover)({}, _ref);
  const state = {
    isDisabled: inputThemeProps.isDisabled,
    isHovered: isHoveredProp || isHovered,
    isFocused: isFocusedProp || isFocused,
    isInvalid: inputThemeProps.isInvalid,
    isReadOnly: inputThemeProps.isReadOnly
  };

  const handleFocus = (focusState, callback) => {
    setIsFocused(focusState);
    callback();
  };
  /**Converting into Hash Color Code */
  //@ts-ignore


  props.focusOutlineColor = (0, _hooks.useToken)('colors', props.focusOutlineColor); //@ts-ignore

  props.invalidOutlineColor = (0, _hooks.useToken)('colors', props.invalidOutlineColor);
  const {
    ariaLabel,
    accessibilityLabel,
    type,
    isFullWidth,
    isDisabled,
    isReadOnly,
    fontFamily,
    fontWeight,
    fontStyle,
    placeholderTextColor,
    selectionColor,
    underlineColorAndroid,
    InputLeftElement,
    InputRightElement,
    leftElement,
    rightElement,
    onFocus,
    onBlur,
    wrapperRef,
    _stack,
    _input,
    ...resolvedProps
  } = (0, _useThemeProps.usePropsResolution)('Input', props, state); // console.log(
  //   nonLayoutProps.INTERNAL_themeStyle[0].backgroundColor,
  //   _input,
  //   'resolved props 222'
  // );

  const {
    colorMode
  } = (0, _colorMode.useColorMode)();
  const {
    styleFromProps
  } = (0, _ResolvedStyleMap.getThemeProps)('Input', {
    colorMode,
    platform: _reactNative.Platform.OS
  }, state, props);
  const filterProps = [..._utils.stylingProps.margin, ..._utils.stylingProps.border, ..._utils.stylingProps.layout, ..._utils.stylingProps.flexbox, ..._utils.stylingProps.position, ..._utils.stylingProps.background, 'shadow', 'opacity'];
  const [layoutStyles, nonLayoutStyles] = (0, _utils.extractInObject)(styleFromProps, filterProps);
  const [layoutProps, nonLayoutProps] = (0, _utils.extractInObject)(resolvedProps, filterProps); // console.log(layoutProps, nonLayoutProps, 'layout props here');

  const resolvedFontFamily = (0, _useResolvedFontFamily.useResolvedFontFamily)({
    fontFamily,
    fontWeight: fontWeight !== null && fontWeight !== void 0 ? fontWeight : 400,
    fontStyle: fontStyle !== null && fontStyle !== void 0 ? fontStyle : 'normal'
  });
  const resolvedPlaceholderTextColor = (0, _hooks.useToken)('colors', placeholderTextColor);
  const resolvedSelectionColor = (0, _hooks.useToken)('colors', selectionColor);
  const resolvedUnderlineColorAndroid = (0, _hooks.useToken)('colors', underlineColorAndroid); // console.log('INTERNAL_themeStyle', resolvedProps, layoutProps);
  //TODO: refactor for responsive prop

  if ((0, _useHasResponsiveProps.useHasResponsiveProps)(props)) {
    return null;
  } // console.log(
  //   nonLayoutProps.INTERNAL_themeStyle,
  //   INTERNAL_themeStyle,
  //   'layout styles'
  // );


  return /*#__PURE__*/_react.default.createElement(_Stack.Stack, _extends({}, _stack, layoutProps, {
    INTERNAL_themeStyle: [layoutStyles, _stack === null || _stack === void 0 ? void 0 : _stack.INTERNAL_themeStyle],
    ref: (0, _utils2.mergeRefs)([_ref, wrapperRef]),
    isFocused: isFocused
  }), InputLeftElement || leftElement ? InputLeftElement || leftElement : null, /*#__PURE__*/_react.default.createElement(StyledInput, _extends({}, inputProps, {
    secureTextEntry: type === 'password',
    accessible: true,
    accessibilityLabel: ariaLabel || accessibilityLabel,
    editable: isDisabled || isReadOnly ? false : true,
    w: isFullWidth ? '100%' : undefined
  }, nonLayoutProps, resolvedFontFamily, {
    INTERNAL_themeStyle: [nonLayoutStyles],
    placeholderTextColor: resolvedPlaceholderTextColor,
    selectionColor: resolvedSelectionColor,
    underlineColorAndroid: resolvedUnderlineColorAndroid,
    onKeyPress: e => {
      e.persist();
      onKeyPress && onKeyPress(e);
    },
    onFocus: e => {
      handleFocus(true, onFocus ? () => onFocus(e) : () => {});
    },
    onBlur: e => {
      handleFocus(false, onBlur ? () => onBlur(e) : () => {});
    }
  }, _input, {
    ref: (0, _utils2.mergeRefs)([ref, _ref, wrapperRef])
  })), InputRightElement || rightElement ? InputRightElement || rightElement : null);
};

var _default = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(Input));

exports.default = _default;
//# sourceMappingURL=Input.js.map