import { Input } from "antd";
import i18n from "i18next";
import parsePhoneNumber from "libphonenumber-js";
import { forwardRef } from "react";
import "./phone-form-item.styles.scss";

function FormItemPhone(props, ref) {
  return <Input ref={ref} {...props} />;
}

export default forwardRef(FormItemPhone);

export const PhoneItemFormatterValidation = () => ({
  async validator(rule, value) {
    if (!value) {
      return Promise.resolve();
    } else {
      const p = parsePhoneNumber(value, "CA");
      if (p && p.isValid()) {
        return Promise.resolve();
      } else {
        return Promise.reject(i18n.t("general.validation.invalidphone"));
      }
    }
  }
});
