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); }
import React, { useCallback, useMemo, useState } from "react";
import Box from "../../atoms/box/box";
import { buildFontConfig } from "../../atoms/text/text";
import { useMolecularComponentConfig } from "../../../hooks/useMolecularComponentConfig";
import { useAtomicComponentConfig } from "../../../hooks/useAtomicComponentConfig";
import Icon from "../../atoms/icon/icon";
import { Platform, TextInput } from "react-native";
import { boxStyleFunctions, colorStyleFunction, createStyleFunction, transformColorValue, typographyStyleFunction } from "../../../theme/src/style-functions";
import { useStyleProps } from "../../../hooks/useStyleProps";
import Pressable from "../../atoms/pressable/pressable";
import { useFocusedState } from "../../../hooks/state/useFocusedState";
import { useDisabledState } from "../../../hooks/state/useDisabledState";
import { useInvalidState } from "../../../hooks/state/useInvalidState";
import { HStack } from "../../atoms/stack/stack";
import { useTheme } from "../../../hooks";
// Additional style Functions used for this component
const placeholderTextColorStyleFunction = createStyleFunction({
  property: "placeholderTextColor",
  styleProperty: "placeholderTextColor",
  themeKey: "palette",
  transform: transformColorValue
});
const selectionColorStyleFunction = createStyleFunction({
  property: "selectionColor",
  styleProperty: "selectionColor",
  themeKey: "palette",
  transform: transformColorValue
});
const inputRootStyleFunction = [...boxStyleFunctions, placeholderTextColorStyleFunction, selectionColorStyleFunction];
const inputTextStyleFunction = [colorStyleFunction, typographyStyleFunction];

/** The Input component is a component that is used to get user input in a text field. **/
const Input = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(({
  children,
  onFocus = undefined,
  onBlur = undefined,
  size = "m",
  isDisabled = false,
  isFullWidth = false,
  hasClearButton = false,
  isInvalid = false,
  leftIcon = undefined,
  rightIcon = undefined,
  colorScheme = "primary",
  onChangeText = () => {},
  onChange = () => {},
  ...rest
}, textInputRef) => {
  const {
    theme
  } = useTheme();
  const [isCleared, setIsCleared] = useState(rest.value && rest.value.length > 0 ? false : true);
  let molecularProps = useMolecularComponentConfig("Input", rest, {
    size: size,
    variant: rest["variant"]
  }, colorScheme, boxStyleFunctions, "container", "input", "container");
  const {
    atoms
  } = molecularProps;
  const inputProps = useStyleProps(atoms.input, inputRootStyleFunction);
  const {
    placeholderTextColor,
    selectionColor,
    ...finalInputStyle
  } = inputProps.style;
  const textProps = useAtomicComponentConfig("Text", atoms.text, {
    size: size,
    variant: atoms.text.variant
  }, "primary", inputTextStyleFunction);
  const memoizedBuildFontConfig = useCallback(() => buildFontConfig(textProps.style, atoms.input.allowFontScaling || true, theme), [textProps.style, atoms.input.allowFontScaling]);
  const finalTextStyles = useMemo(() => ({
    ...textProps.style,
    ...memoizedBuildFontConfig()
  }), [textProps.style, memoizedBuildFontConfig]);

  // Use State for dynamic styles
  const {
    focused,
    setFocused,
    propsWithFocusedStyles
  } = useFocusedState(atoms.container, boxStyleFunctions, "molecule");
  atoms.container = propsWithFocusedStyles;
  const {
    propsWithDisabledStyles
  } = useDisabledState(atoms.container, boxStyleFunctions, "molecule", true, isDisabled);
  atoms.container = propsWithDisabledStyles;
  const {
    propsWithInvalidStyles
  } = useInvalidState(atoms.container, boxStyleFunctions, "molecule", true, isInvalid);
  atoms.container = propsWithInvalidStyles;

  // METHODS
  const clearInputHandler = () => {
    textInputRef.current.clear();
    setIsCleared(true);
    onChangeText("");
  };
  const onChangeHandler = event => {
    const {
      text
    } = event.nativeEvent;
    if (text.length > 0) {
      setIsCleared(false);
    }
    onChange(event);
  };
  const onChangeTextHandler = value => {
    if (value.length > 0) {
      setIsCleared(false);
    }
    onChangeText(value);
  };
  const focusInputHandler = event => {
    setFocused(true);
    if (onFocus) {
      onFocus(event);
    }
  };
  const blurInputHandler = event => {
    setFocused(false);
    if (onBlur) {
      onBlur(event);
    }
  };

  // Render Functions
  const renderLeftIcon = useCallback(() => {
    if (leftIcon) {
      return /*#__PURE__*/React.cloneElement(leftIcon, {
        ...atoms.icon,
        ...leftIcon.props
      });
    }
  }, [leftIcon, atoms.icon]);
  const renderRightIcon = useCallback(() => {
    if (rightIcon) {
      return /*#__PURE__*/React.cloneElement(rightIcon, {
        ...atoms.icon,
        ...rightIcon.props
      });
    }
  }, [rightIcon, atoms.icon]);
  const renderClearIcon = useCallback(() => {
    if (hasClearButton && !isCleared) {
      return /*#__PURE__*/React.createElement(Pressable, {
        testID: "clear-icon",
        onPress: clearInputHandler
      }, /*#__PURE__*/React.createElement(Icon, _extends({}, atoms.icon, {
        iconFamily: "Ionicons",
        iconName: "close",
        marginLeft: "2"
      })));
    }
  }, [hasClearButton, isCleared, clearInputHandler, atoms.icon]);
  return /*#__PURE__*/React.createElement(Box, _extends({}, atoms.container, {
    w: isFullWidth ? "100%" : undefined
  }), renderLeftIcon(), /*#__PURE__*/React.createElement(TextInput, _extends({}, inputProps, {
    ref: textInputRef,
    editable: !isDisabled,
    onFocus: focusInputHandler,
    onBlur: blurInputHandler,
    onChange: onChangeHandler,
    onChangeText: onChangeTextHandler,
    allowFontScaling: true,
    placeholderTextColor: placeholderTextColor ?? "#a7a7a7",
    selectionColor: selectionColor ?? null,
    accessibilityLabel: rest.accessibilityLabel ?? rest.placeholder,
    accessibilityState: {
      disabled: isDisabled,
      selected: focused
    },
    style: [finalInputStyle, finalTextStyles, Platform.OS === "web" ? {
      outlineStyle: "none"
    } : {}, {
      flex: 1
    }]
  })), /*#__PURE__*/React.createElement(HStack, {
    spacing: "0",
    alignSelf: "center",
    alignItems: "center"
  }, renderRightIcon(), renderClearIcon()));
}));
Input.displayName = "Input";
export default Input;
//# sourceMappingURL=input.js.map