import axios from 'axios';

export default async function parsePhone(phone, country) {
    if (!phone) return;
    if (phone.startsWith('+')) return `${phone.slice(1)}`;
    if (phone.startsWith('00')) return `${phone.slice(2)}`;

    let prefix;
    try {
        prefix = (await axios.get(`Prefixes/${country.toLowerCase()}`)).data?.prefix;
    } catch (e) {
        prefix = (await axios.get('PbxConfigs/findOne')).data?.defaultPrefix;
    }
    prefix = prefix.replace(/^0+/, '');

    if (phone.startsWith(prefix)) return phone;
    return `${prefix}${phone}`;
}
