var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import { jsx } from "react/jsx-runtime";
import React, { useRef, useCallback } from "react";
import BasePhoneInput from "react-phone-input-2";
import PropTypes, { func, string, bool } from "prop-types";
import * as S from "./Phone.style";
import Input from "./Input";
import { useCombinedRefs } from "../../hooks";
const phoneBaseSettings = {
  onlyCountries: [
    "ar",
    "br",
    "cl",
    "de",
    "es",
    "gb",
    "ie",
    "it",
    "mx",
    "pt",
    "us",
    "ca",
    "ro"
  ]
};
const noop = () => {
};
const Phone = React.forwardRef(
  (_a, forwardedRef) => {
    var _b = _a, {
      defaultCountry = "br",
      disabled = false,
      readOnly = false,
      error,
      full = false,
      helper,
      label = "",
      placeholder = "+55 (11) 999999999",
      value = "",
      countries = phoneBaseSettings.onlyCountries,
      onChange = noop,
      cleanable = true
    } = _b, rest = __objRest(_b, [
      "defaultCountry",
      "disabled",
      "readOnly",
      "error",
      "full",
      "helper",
      "label",
      "placeholder",
      "value",
      "countries",
      "onChange",
      "cleanable"
    ]);
    const inputRef = useCombinedRefs(forwardedRef);
    const currentCountry = useRef(defaultCountry);
    const onChangeCountry = useCallback((countryCode) => {
      if (currentCountry.current !== countryCode) {
        inputRef.current.focus();
        currentCountry.current = countryCode;
      }
    }, []);
    const availableCountries = {
      onlyCountries: (countries == null ? void 0 : countries.length) > 0 ? countries : phoneBaseSettings.onlyCountries
    };
    return /* @__PURE__ */ jsx(
      Input,
      __spreadProps(__spreadValues({}, {
        disabled,
        readOnly,
        error,
        full,
        helper,
        value,
        cleanable
      }), {
        ref: inputRef,
        label: "",
        onClean: (phoneNumber) => onChange(phoneNumber),
        children: /* @__PURE__ */ jsx(S.Container, { error, disabled, full, children: /* @__PURE__ */ jsx(
          BasePhoneInput,
          __spreadProps(__spreadValues(__spreadValues({}, availableCountries), rest), {
            ref: (phoneRef) => {
              inputRef.current = phoneRef == null ? void 0 : phoneRef.numberInputRef;
            },
            inputProps: __spreadProps(__spreadValues({}, rest.inputProps), { readOnly }),
            disabled,
            disableDropdown: readOnly,
            country: defaultCountry,
            specialLabel: label,
            placeholder,
            onChange: (phoneNumber, options) => {
              onChange(phoneNumber, options);
              onChangeCountry(options.countryCode);
            },
            value
          })
        ) })
      })
    );
  }
);
Phone.propTypes = {
  className: string,
  /** display a close icon to clean the field */
  cleanable: bool,
  /** default country to be selected on the dropdown */
  defaultCountry: PropTypes.string,
  /** disable the whole input */
  disabled: bool,
  /** display a error message and error style */
  error: string,
  /** should occupy the whole container width */
  full: bool,
  /** a helper text to be displayed below field */
  helper: string,
  /** label for the input */
  label: string,
  /** make the input read only */
  readOnly: bool,
  /** value of the input */
  value: string,
  /** callback invoked when value changes, either by typing of selecting a country */
  onChange: func,
  /** placeholder to show when the input is cleared */
  placeholder: string,
  /** countries for mask field and dropdown, make sure to use ISO 3166-1 alpha-2 lowercase */
  countries: PropTypes.arrayOf(string)
};
var Phone_default = Phone;
export {
  Phone_default as default
};
