"use strict";

import { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState } from 'react';
import { TextInput, TouchableOpacity, View } from 'react-native';
import { ErrorView, FloatingView, getBorderColor, getFontStyle, getIconSize, getSizeStyle } from "./common.js";
import { ApplicationContext } from "../Context/index.js";
import { Icon } from "../Icon/index.js";
import { SizedBox } from "../SizedBox/index.js";
import { Spacing } from "../Consts/index.js";
import { formatMoneyToNumber, formatNumberToMoney } from "./utils.js";
import styles from "./styles.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const InputMoney = /*#__PURE__*/forwardRef(({
  onChangeText,
  floatingValue,
  floatingIcon,
  size = 'small',
  onBlur,
  onFocus,
  error,
  trailing,
  disabled,
  floatingIconColor,
  iconColor,
  required,
  currency = '$',
  defaultValue,
  style,
  ...props
}, ref) => {
  const inputRef = useRef(null);
  const {
    theme
  } = useContext(ApplicationContext);
  const [focused, setFocused] = useState(false);
  const [value, setValue] = useState('');
  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current?.focus();
    },
    blur: () => {
      inputRef.current?.blur();
    },
    clear: onClearText,
    setText: text => {
      inputRef.current?.setNativeProps?.({
        text: text
      });
      _onChangeText(value);
    }
  }));
  useEffect(() => {
    if (defaultValue) {
      const valueFormat = formatMoneyToNumber(defaultValue, currency);
      setValue(formatNumberToMoney(valueFormat, currency));
    }
  }, [currency, defaultValue]);
  const onClearText = () => {
    inputRef.current?.clear();
    setValue('');
    onChangeText?.('');
  };
  const _onChangeText = text => {
    if (text.length < value.length && value.indexOf(text) === 0) {
      text = value.slice(0, -2) + currency;
    }
    const valueFormat = formatMoneyToNumber(text, currency);
    setValue(formatNumberToMoney(valueFormat, currency));
    onChangeText?.(formatMoneyToNumber(text, currency).toString());
  };
  const _onFocus = e => {
    setFocused(true);
    onFocus?.(e);
  };
  const _onBlur = e => {
    setFocused(false);
    onBlur?.(e);
  };
  const renderInputView = () => {
    const disabledColor = theme.colors.text.disable;
    const iconSize = getIconSize(size);
    let textColor = theme.colors.text.default;
    let placeholderColor = theme.colors.text.hint;
    let iconTintColor = iconColor;
    if (disabled) {
      textColor = disabledColor;
      placeholderColor = disabledColor;
      iconTintColor = disabledColor;
    }
    return /*#__PURE__*/_jsxs(View, {
      style: [styles.inputWrapper, {
        backgroundColor: theme.colors.background.surface
      }, !!floatingValue && {
        marginTop: Spacing.S
      }, getSizeStyle(size), getBorderColor(theme, focused, error, disabled)],
      children: [/*#__PURE__*/_jsx(FloatingView, {
        floatingValue: floatingValue,
        floatingIconColor: floatingIconColor,
        disabled: disabled,
        required: required,
        floatingIcon: floatingIcon
      }), /*#__PURE__*/_jsx(View, {
        style: styles.inputView,
        children: /*#__PURE__*/_jsx(TextInput, {
          ref: inputRef,
          ...props,
          editable: !disabled,
          keyboardType: 'number-pad',
          style: [styles.moneyInput, getFontStyle(theme, size), {
            color: textColor,
            lineHeight: undefined
          }, style],
          value: value,
          onChangeText: _onChangeText,
          onFocus: _onFocus,
          onBlur: _onBlur,
          selectionColor: theme.colors.primary.default,
          placeholderTextColor: placeholderColor
        })
      }), /*#__PURE__*/_jsxs(View, {
        style: styles.iconView,
        children: [focused && /*#__PURE__*/_jsxs(_Fragment, {
          children: [/*#__PURE__*/_jsx(SizedBox, {
            width: Spacing.XS
          }), /*#__PURE__*/_jsx(TouchableOpacity, {
            onPress: onClearText,
            children: /*#__PURE__*/_jsx(Icon, {
              name: "close",
              size: iconSize
            })
          })]
        }), !!trailing && /*#__PURE__*/_jsxs(_Fragment, {
          children: [/*#__PURE__*/_jsx(SizedBox, {
            width: Spacing.S
          }), /*#__PURE__*/_jsx(Icon, {
            color: iconTintColor,
            name: trailing,
            size: iconSize
          })]
        })]
      })]
    });
  };
  return /*#__PURE__*/_jsxs(View, {
    style: styles.wrapper,
    children: [renderInputView(), /*#__PURE__*/_jsx(ErrorView, {
      error: error
    })]
  });
});
export default InputMoney;
//# sourceMappingURL=InputMoney.js.map