// let fullPhone = data.user_phone.replace(/[\s\-\_\(\)\.\+]/g, "");

// International Phone Number Validation
const intellinputShared = document.querySelector(".intelli-shared-phone");
let phoneInputTimeId = null;

const intlPhoneFormatShared = window.intlTelInput(intellinputShared, {
    initialCountry: "auto",
    geoIpLookup: callback => {
        fetch("https://ipapi.co/json")
            .then(res => res.json())
            .then(data => callback(data.country_code))
            .catch(() => callback("us"));
    },
    strictMode: true,
    autoPlaceholder: true,
    formatAsYouType: true,
    formatOnDisplay: true,
    nationalMode: false,
    hiddenInput: () => ({ phone: "full_phone", country: "country_code" }),
    loadUtilsOnInit: "../intlTelInput/utils.js", // just for formatting/placeholders etc
    // loadUtilsOnInit: "https://cdn.jsdelivr.net/npm/intl-tel-input@24.6.0/build/js/utils.js",
});


$(".intelli-shared-phone").keyup(function (e) {

    if (phoneInputTimeId) {
        clearTimeout(phoneInputTimeId);
    }

    if (!intlPhoneFormatShared.isValidNumber()) {
        // console.log("Invalid number. Please try again.");
        return false;
    }
    // Get the formatted phone number
    const fullPhone = intlPhoneFormatShared.getNumber();

    // Get the hidden input value
    phoneInputTimeId = setTimeout(() => {
        if (fullPhone) {
            // console.log(`fullPhone: ${fullPhone}`);
            intlPhoneFormatShared.setNumber(fullPhone);
        }
    }, 100);

});


// International Phone Number Validation
const intellinputWsPhoneMediaMsg = document.querySelector(".intelli-ws-phone-media-message");

const intlPhoneFormatWsPhoneMediaMsg = window.intlTelInput(intellinputWsPhoneMediaMsg, {
    initialCountry: "auto",
    geoIpLookup: callback => {
        fetch("https://ipapi.co/json")
            .then(res => res.json())
            .then(data => callback(data.country_code))
            .catch(() => callback("us"));
    },
    strictMode: true,
    autoPlaceholder: true,
    formatAsYouType: true,
    formatOnDisplay: true,
    nationalMode: false,
    hiddenInput: () => ({ phone: "full_phone", country: "country_code" }),
    loadUtilsOnInit: "../intlTelInput/utils.js", // just for formatting/placeholders etc
    // loadUtilsOnInit: "https://cdn.jsdelivr.net/npm/intl-tel-input@24.6.0/build/js/utils.js",
});


$(".intelli-ws-phone-media-message").keyup(function (e) {

    if (phoneInputTimeId) {
        clearTimeout(phoneInputTimeId);
    }

    if (!intlPhoneFormatWsPhoneMediaMsg.isValidNumber()) {
        // console.log("Invalid number. Please try again.");
        return false;
    }
    // Get the formatted phone number
    const fullPhone = intlPhoneFormatWsPhoneMediaMsg.getNumber();

    // Get the hidden input value
    phoneInputTimeId = setTimeout(() => {
        if (fullPhone) {
            // console.log(`fullPhone: ${fullPhone}`);
            intlPhoneFormatWsPhoneMediaMsg.setNumber(fullPhone);
        }
    }, 100);

});


// International Phone Number Validation
const intellinputWsPhoneMsg = document.querySelector(".intelli-ws-phone-message");

const intlPhoneFormatWsPhoneMsg = window.intlTelInput(intellinputWsPhoneMsg, {
    initialCountry: "auto",
    geoIpLookup: callback => {
        fetch("https://ipapi.co/json")
            .then(res => res.json())
            .then(data => callback(data.country_code))
            .catch(() => callback("us"));
    },
    strictMode: true,
    autoPlaceholder: true,
    formatAsYouType: true,
    formatOnDisplay: true,
    nationalMode: false,
    hiddenInput: () => ({ phone: "full_phone", country: "country_code" }),
    loadUtilsOnInit: "../intlTelInput/utils.js", // just for formatting/placeholders etc
    // loadUtilsOnInit: "https://cdn.jsdelivr.net/npm/intl-tel-input@24.6.0/build/js/utils.js",
});


$(".intelli-ws-phone-message").keyup(function (e) {

    if (phoneInputTimeId) {
        clearTimeout(phoneInputTimeId);
    }

    if (!intlPhoneFormatWsPhoneMsg.isValidNumber()) {
        // console.log("Invalid number. Please try again.");
        return false;
    }
    // Get the formatted phone number
    const fullPhone = intlPhoneFormatWsPhoneMsg.getNumber();

    // Get the hidden input value
    phoneInputTimeId = setTimeout(() => {
        if (fullPhone) {
            // console.log(`fullPhone: ${fullPhone}`);
            intlPhoneFormatWsPhoneMsg.setNumber(fullPhone);
        }
    }, 100);

});